Bibliography
Andrei Alexandrescu. Modern C++ Design , Addison-Wesley, 2001.
Association for Computing Machinery. ACM Code of Ethics and Professional Conduct , www.acm.org/constitution/code.html.
. Software Engineering Code of Ethics and Professional Practice , www.acm.org/serving/se/code.htm.
Marshall P. Cline, Greg A. Lomow, and Mike Girou. C++ FAQs, Second Edition , Addison-Wesley, 1999.
Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns , Addison-Wesley, 1995.
Nicolai Josuttis. The C++ Standard Library , Addison-Wesley, 1999.
Robert Martin. Agile Software Development , 2nd ed., Prentice Hall, 2003.
Scott Meyers. Effective C++ , 2nd ed. Addison-Wesley, 1998.
. Effective STL , Addison-Wesley, 2001.
. More Effective C++ , Addison-Wesley, 1996.
Stephen C. Dewhurst and Kathy T. Stark. Programming in C++ , 2nd ed., Prentice-Hall, 1995.
William Strunk and E. B. White. The Elements of Style , 3d ed., Macmillan, 1979.
Herb Sutter. More Exceptional C++ , Addison-Wesley, 2002.
E. B. White. Writings from The New Yorker , HarperCollins, 1990.
Chapter 1. Basics
That a problem is basic does not mean it isn't severe or common. In fact, the common presence of the basic problems discussed in this chapter is perhaps more cause for alarm than the more technically advanced problems we discuss in later chapters. The basic nature of the problems discussed here implies that they may be present, to some extent, in almost all C++ code.
Gotcha #1: Excessive Commenting
Many comments are unnecessary. They generally make source code hard to read and maintain, and frequently lead maintainers astray. Consider the following simple statement:
a = b; // assign b to a
The comment cannot communicate the meaning of the statement more clearly than the code itself, and so is useless. Actually, it's worse than useless. It's deadly. First, the comment distracts the reader from the code, increasing the volume of text the reader has to wade through in order to extract its meaning. Second, there is more source text to maintain, since comments must be maintained as the program text they describe is modified. Third, this necessary maintenance is often not performed.
c = b; // assign b to a
A careful maintainer cannot simply assume the comment is in error and is obliged to trace through the program to determine whether the comment is erroneous, officious ( c is a reference to a ), or subtle (assigning to c will later cause the same assignment to be propagated to a somehow). The line should originally have been written without a comment:
a = b;
The code is maximally clear as it stands, with no comment to be incorrectly maintained. This is similar in spirit to the well-worn observation that the most efficient code is code that doesn't exist. The same applies to comments: the best comment is one that didn't have to be written, because the code it would otherwise have described is self-documenting.
Other common examples of unnecessary comments frequently occur in class definitions, either as the result of an ill-conceived coding standard or as the work of a C++ novice:
class C { // Public Interface public: C(); // default constructor ~C(); // destructor //... };
You get the feeling you're reading someone's crib notes. If a maintainer has to be reminded of the meaning of the public: label, you don't want that person maintaining your code. None of these comments does anything for an experienced C++ programmer except clutter the code and provide more source text to be improperly maintained.
class C { // Public Interface protected: C( int ); // default constructor public: virtual ~C(); // destructor //... };
Programmers also have a strong incentive not to "waste" lines of source text. Anecdotally, if a construct (function, public interface of a class, and so on) can be presented in a conventional and rational format on a single "page" of about 30-40 lines, it will be easy to understand. If it goes on to a second page, it will be about twice as hard to understand. If it goes onto a third page, it will be approximately four times as hard to understand.
A particularly odious practice is that of inserting change logs as comments at the head or tail of source code files:
/* 6/17/02 SCD fixed the gaforniflat bug */
Is this useful information, or is the maintainer just bragging? This comment is unlikely to be of any use whatever within a week or two of its insertion, but it will hang on grimly for years, distracting generations of maintainers. A much better alternative is to cede these commenting tasks to your version control software; a C++ source code file is no place to leave a laundry list.
One of the best ways to avoid comments and make code clear and maintainable is to follow a simple, well-defined naming convention and choose clear names that reflect the abstract meaning of the entity (function, class, variable, and so on) you're naming. Formal argument names in declarations are particularly important. Consider a function that takes three arguments of identical type:
/* Perform the action from the source to the destination. Arg1 is action code, arg2 is source, and arg3 is destination.*/void perform( int, int, int );
Not too terrible, but think what it would look like with seven or eight arguments instead of three. We can do better:
void perform( int actionCode, int source, int destination );
Better, though we should probably still have a one-liner that tells us what the function does (though not how it does it). One of the most attractive things about formal argument names in declarations is that they, unlike comments, are generally maintained along with the rest of the code, even though they have no effect on the code's meaning. I can't think of a single programmer who would switch the meanings of the second and third arguments of the perform function without also changing their names, but I can identify legions of programmers who would make the change without maintaining the comment.
Kathy Stark may have said it best in Programming in C++ : "If meaningful and mnemonic names are used in a program, there is often only occasional need for additional comments. If meaningful names are not used, it is unlikely that any added comments will make the code easy to understand."
Another way to minimize comments is to employ standard or well-known components:
printf( "Hello, World!" ); // print "Hello, World" to the screen
This comment is both useless and only occasionally correct. It's not that standard components are necessarily self-documenting; it's that they're already well documented and well known.
swap( a, a+1 ); sort( a, a+max );copy( a, a+max, ostream_iterator(cout,"\n") );
Because swap , sort , and copy are standard components, additional comments inserted above can only clutter the source and introduce imprecision in the description of the standard operations.
Comments are not inherently harmfuland are often necessarybut they must be maintained, and they're typically harder to maintain than the code they document. Comments should not state the obvious or provide information better maintained elsewhere. The goal is not to eliminate comments at any cost but to employ the minimal volume of comments that permits the code to be readily understood and maintained.
Gotcha #10: Ignorance of Idiom
It is an old observation that the best writers sometimes disregard the rules of rhetoric. When they do so, however, the reader will usually find in the sentence some compensating merit, attained at the cost of the violation. Unless he is certain of doing as well, he will probably do best to follow the rules. (Strunk and White, The Elements of Style )
[1] The author of this quote is actually William Strunk, since it appeared in the original volume before the book's resurrection by White in 1959.