Your Automatisms Betray You

Yesterday someone dropped on the IRC channel where my fellow programmers, computer enthusiasts, and I hang out to get help to find a bug. He uses one of the paste sites (like pastebin.ca, pastebin.com, or rafb.net), pastes his piece of offending code, and so we get a look at the code. Of course, I go over the short program, notice a mistake in the scanf but it took me a full two minutes to notice the loop:

for (c=0; c++; c<n)
 {
   ...
 }

That kind of bug always takes a while to find because we don’t read what’s actually written, but what we think is written, unless we pay the utmost attention to the code—what we should be doing anyway, but do not always. Usually, you zero in on that kind of bug rapidly, as you guide your search from the bug’s symptoms which leads you to defect’s approximate location. If you’re like me—write a little, test a lot—you find those bugs right away most of the times. However, even if you zero in rapidly, you still get a coarse-grained location: module, class, function.

However, that particular type of error does not happen very often to programmers with a good level of experience with a given programming language (in this case, C) because programmers rapidly develop automatisms that protects them from such mistakes. First, that is because they know the language well enough to know the order of the statements within a for about as well as they now how to ride a bicycle; second, because their automatisms make them write statements in the right order, even though they might not master a particular statement or do not fully understand the semantics.

Such automatisms—surely decried by “hot” programmers—are not bad. They generally help code reliability because the programmers always use the same kind of constructs that they are used of getting right. But automatisms have their down side. Not only may they limit the creativity of programmers by confining them to their own safe Terra Cognita, keeping them from stretching their envelope and getting better, they may also, and maybe more fundamentally, corrupt the way they are thinking about programming.

Indeed, if a programmer brings his functional-language automatisms with him while coding in C, he may be in big trouble. First, many of the natural functional language constructs make no sense whatsoever in C or C++, either because they are against the language general conceptual style, or because they cannot be implemented efficiently 1. In other words, your automatisms, mostly unbeknownst to you, may prevent you from being totally immersed into the current programming language—therefore from writing really good code. You know what I’m talking about. Have you ever seen a C++ program that reeks of minimally translated Fortran but yet was really first written in C++?

Of course you do.

*
* *

You know, your old automatisms may haunt you a long time. I used to program in (Turbo) Pascal quite a lot before I turned to C and eventually C++. The last time I wrote a large Pascal program must have been in 2002 or so, then again it was Delphi, which is still better. Yet, I still surprise myself writing := in a C program, or spend a few minute understanding why the compiler complains about while (cond) do { }.


1 Efficient implementation may mean that the implementation is computationally efficient (it is fast, requires little memory, etc.) or that it requires little code. Fast and frugal code is dogma to C programmers, so a construction that is inherently inefficient will be frown upon even though it may well be what it actually takes. The size of the source code required to implement some functional-style construct is truly enormous in C and C++, which aren’t functionnal languages. Enormous quantity of code to perform tasks that could be performed differently using at lot less code cannot be considered “efficient” programming.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 65 other followers

%d bloggers like this: