What’s Next 783
21
- Higher-level comments are infinitely more important than process details. Add
value; do not merely restate the code.
n++; // n is incremented by one
This comment isn’t worth the time it takes to type it in. Concentrate on the seman-
tics of functions and blocks of code. Say what a function does. Indicate side
effects, types of parameters, and return values. Describe all assumptions that are
made (or not made), such as “assumes nis nonnegative” or “will return –1 if xis
invalid.” Within complex logic, use comments to indicate the conditions that exist
at that point in the code. - Use complete English sentences with appropriate punctuation and capitalization.
The extra typing is worth it. Don’t be overly cryptic and don’t abbreviate. What
seems exceedingly clear to you as you write code will be amazingly obtuse in a
few months. - Use blank lines freely to help the reader understand what is going on. Separate
statements into logical groups.
Setting Up Access ..........................................................................................
The way you access portions of your program should also be consistent. Some tips for
access include the following:
- Always use public:,private:, and protected:labels; don’t rely on the defaults.
- List the public members first, then protected, then private. List the data members in
a group after the methods. - Put the constructor(s) first in the appropriate section, followed by the destructor.
List overloaded methods with the same name adjacent to each other. Group acces-
sor functions together whenever possible. - Consider alphabetizing the method names within each group and alphabetizing the
member variables. Be certain to alphabetize the filenames in includestatements. - Even though the use of the virtualkeyword is optional when overriding, use it
anyway; it helps to remind you that it is virtual, and it also keeps the declaration
consistent.
Class Definitions ............................................................................................
Try to keep the definitions of methods in the same order as the declarations. It makes
things easier to find.
When defining a function, place the return type and all other modifiers on a previous line
so that the class name and function name begin at the left margin. This makes it much
easier to find functions.