Home > Articles

  • Print
  • + Share This
From the author of

Evaluating C++

So what about C++? The reason I use C++ at all falls into the category of Test 1: It fits the purpose. If you want the freedom to use all of the facilities available from the Windows operating system, you don't have much choice. But on all the other tests, in my humble opinion, C and C++ are a diseased mess. Take lucidity: C and C++ are concise but poorly readable. In C and C++, this can be hard even if it's your own code. For instance, I've read the manual a million times, but I still can't remember whether int* a[20]; is an array of pointers or a pointer to an array. And take the C notion of null-terminated character strings. This allows you to write "really neat" code such as this:

while (*p++ = *q++);  /* copies the contents of q into p */

This stops copying because, when it hits the null at the end of the string, the expression evaluates as null, which is interpreted as false. Look at this code; it has an assignment where you would naturally expect an equals sign (the == in C) to occur and relies on the convention that null equals zero equals false. If you were reading this code for the first time, it would take some time to mentally unravel and figure out what was happening. Worse than that, if the null is missing, then the copy does not terminate and awful things happen. In summary, the code is concise, but at the cost of increasing the effort to read it, and leaving a trap that punishes any slight infringement elsewhere in the program. It fails on the measure of lucidity and accident proneness. The prevalence of C has encouraged a generation of programmers to think that coding this way is good idea. Of course, you don't have to write code like this. I expect that most so-called C programming experts would sneer at the naivety of the code that I write.

C++ is basically C with object-oriented and other extensions. Objects are great; I use them because I design with objects in mind. Objects help languages meet the tests outlined earlier. They particularly help with the third test because superclasses allow you to express logic once that would otherwise be highly dispersed. The major downside to C++ is that it is incredibly complex. More than any language I know, to use C++ effectively, you don't just learn the language; you have to develop a style—choosing a subset of the language to use, deciding on naming conventions, and determining indentation conventions and standard patterns for common phrases. This takes time—lots of time.

But although there is plenty wrong with the language, it is possible to write good, readable C++. This is not so remarkable; in my experience, it is possible to write good, effective programs in almost any language, including COBOL. The quality of the programmer, the choice of algorithms and the large-scale structure of the program are much more important than the syntax of the language. The difference between writing in a good language and a bad language is mainly that, in a bad language, you have to be much more cautious.

  • + Share This
  • 🔖 Save To Your Account