Tuesday, July 13, 2010

When good habits turn bad

Programming is a complex task, and we develop habits to make things easier. Habits let us "forget" certain things, by making them second nature. Usually this is a good thing, since we cannot concentrate on every aspect of the programming task.

For example, the keyboard. I have been using keyboards for many years, and my fingers "know" the location of the keys. I do not have to hunt for specific keys. (I do remember the days when I was unfamiliar with the keyboard and had to search for unfamiliar characters such as the "at" sign.)

Some habits are passed from one generation to the next. If you ask a modern-day programmer to write a "for" loop, they will probably write this:

for (int i(0); i < 10; i++) {     ... }

Modern day languages such as C#, Perl, Python, and Ruby allow for more elegant constructs, but let's assume we want an "old style" "for" loop.

Programmers have the habit of using the variable i for simple loops. Why this variable? I've seen lots of code, and almost all of it uses the variable i. There is no reason to use the letter i for a loop counter other than it is short for the word "index"). If programmers picked letters at random, we should see a wide distribution of letters (and possibly longer names) for loop counters. Yet the vast majority use i.

My conjecture is that current-day C# programmers were taught by C++ programmers, who used i. The C++ programmers were taught by C programmers, who were taught by FORTRAN programmers. Here we find something interesting.

In FORTRAN, the name for a variable was significant: it denoted the type of the variable. Variables with names beginning with the letters I, J, K, L, M, or N were integers; the rest were "reals" or floats. The syntax of the language did not allow for the declaration of variables. (At least not FORTRAN IV. Later versions of FORTRAN did allow for declarations.) "DO" loops (the FORTRAN equivalent of "for" loops) required an integer index variable.

Since FORTRAN syntax required specific variable names, programmers developed habits for variable names. The letters I, J, and K were used for loop counters.

The FORTRAN programmers developed the habits and later trained the C programmers. The C programmers adopted the habits and trained the C++ programmers ... and you know the rest. Modern-day programmers use the habits of FORTRAN, without thinking.

Variable names for loops are fairly harmless. But habits can extend beyond names. Yet we keep them in our heads. Here are some habits that we should re-think:

- Memory is expensive. Use the smallest memory structure possible.
- Re-use variables for different purposes.
- I/O is expensive. Cache values when possible.
- Use short names for variables. Shorter names means less to type! (Keypunch?)
- Changing a function is risky. Better to copy it and change the copy.
- Object-oriented programs are slow. Use procedural code.
- Java programs are slow. Stick with C or C++.
- Hardware is expensive and therefore will remain in place for a long time. Don't code for changes.
- Programmers are cheap. Save money by not spending on tools.

Habits can be helpful, but outdated habits can be harmful. It ain't what you don't know that hurts you, it's what you know that ain't so!

Don't get hurt!


No comments: