![](https://csdnimg.cn/release/download_crawler_static/4053468/bg11.jpg)
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