Skip to content
>GLB_
Go back

The elements of programming style: Expressions

Chapter 2: Expressions

Writing a computer program eventually boils down to wanting a sequence of statements in the language at hand. How each of those statements is expressed determines in large measure the intelligibility of the whole; no amount of commenting, formatting, or supplementary documentation can entirely replace well expressed statements. After all, they determine what the program actually does.

The fewer temporary variables in a program, the less chance there is that one will not be properly initialized, or that one will be altered unexpectedly before it is used. “Temporary” is a dirty word in programming - it suggests that a variable can be used with less thought than a “normal” (permanent?) one, and it encourages the use of one variable for several unrelated calculations. Both are dangerous practices.

Even if the comment about efficiency were true in a particular environment, there is still little justification for using the more obscure mode of expression. (…), we observe simply that a program usually has to be read several times in the process of getting it debugged. The harder it is for people to grasp the intent of any given section, the longer it will be before the program becomes operational. Trying to outsmart a compiler defeats much of the purpose of using one.

Repeated patterns of code catch the eye when scanning listings. Since the computer is a tool for handling repetitive operations, we should be alerted by such patterns to look for oversights - why didn’t the programmer let the computer do the repeating?

The ease of later comprehending, debugging, and changing the program will more than compensate for any overhead caused by adding the extra modules.

If someone could understand your code when read aloud over the telephone, it’s clear enough. If not, then it needs rewriting.

Key points:

  1. Write Clearly. Try not using “branches” In your code.
  2. Be sparing with temporary variables.
  3. Be unambiguous. Add parentheses and alter too-similar identifiers to avoid any possibility of misunderstanding
  4. Don’t build all of your own tools: use standard library functions. Be sufficient general that your routine can be used in future applications and by other people
  5. Make sure the conditional test reads clearly.

Share this post:

Previous Post
The elements of programming style: Epilogue
Next Post
The elements of programming style: Input and output