IN THE EARLY DAYS, Cobol programmers were judged on how efficiently their programs ran rather than on how easy the programs were to maintain. Today, the same is true for those C and C++ programmers who religiously pursue tight, efficient code at the expense of readability.
As new programmers will learn, there is a natural trade-off between programming for readability and programming for efficiency and performance. When a language was designed to be programmer-friendly, such as a fourth-generation language or Microsoft Corp.'s Visual Basic, the syntax is easy to read and the programs are easy to maintain.
Languages such as C, on the other hand, were designed to emphasize performance and are generally much harder to decipher. Efficient code is achieved by nesting placing multiple functions into a single line of code. Nesting reduces the number of program variables, but it also affects readability. And it is very different from the program structure enforced by Cobol and Pascal.
Some programming purists revel in C's ability to recursively nest functions within functions. Some even say that deeply nested functions improve the code's maintainability. Unfortunately, the goal of programming with the absolute minimum amount of programming statements is at odds with the goal of maintainable code.
``There is a fine line between making C code readable and making it efficient,'' says Kevin Zimmerman, a C++ expert at Lawyers Cooperative Publishing in Rochester, N.Y. ``To fully exploit the power of C, you need to be able to nest functions so that you can reduce the number of variables required. But declaring and using many variables can detract from code readability just as much as nest functions,'' Zimmerman says.
C programmers are often taught to emphasize efficiency, especially when writing systems software. For example, database management systems contain functions that may be called dozens of times each second. Even the smallest improvement becomes important to overall database performance. When developing business applications such as payroll or inventory systems, however, efficient code should take a backseat to maintainable code.
Benefiting from C++
No one advocates writing C like a Cobol program in which code is carefully crafted and names assigned to numeric variables, for example: Add vermouth to gin giving martini. Instead, programmers should take advantage of powerful facilities such as C++s encapsulation and information-hiding. For instance, proper use of encapsulation ensures that all code modules are self-contained and function independently.
But whatever your coding style, take heart that eventually, generally accepted coding styles will emerge. Until then, take stock in how readable your own code is and remember that adding a few redundant variables has a small impact on performance, and the code will be far easier to maintain.
Choose to reuse
Eventually, the C programmer's job will completely change from handcrafting each program to assembling reusable parts. No longer will programmers need to know how a function works but rather how to assemble the functions into a cohesive program.
Every properly designed C program will be assembled from pre-existing and pretested library routines, and the programs will become little more than a shell for calls to the prewritten C functions. Productive C programmers will purchase libraries of C functions and focus on the interface rules for calling these functions.
Computerworld Article February 14, 1994: