Comments are information intended for people reading the source code and are ignored by the compiler during the compilation process. Programmers typically use comments to explain statements or computations that are not otherwise obvious, describe algorithms, provide references, etc. In short, they provide any information that might help readers understand the code and developers more easily maintain it.
// Comment that continues to the end of the line |
(a) |
/* A comment that continues until closed. This kind of comment can span multiple lines.*/ |
(b) |
int x = y / z /* this assumes that z is not zero */ + w; |
(c) |
//
), but all characters appearing on the right form a comment (i.e., ignored by the compiler). This comment style may appear at the beginning of a line or provide brief information at the end of a statement.Programmers agree on one strict rule about comments: comments must make it easier for a human reader to understand a program and the purpose of any obscure operations..
Too few comments can leave a reader unsure about what a program is doing or how it connects to the original problem. However, too many or inappropriately placed comments can obscure the code and make it more difficult to read. Sadly, no formula tells programmers how to find the Goldilocks balance between too many and too few comments.
In the absence of standards (imposed by employers or instructors), programmers will develop and use a personal commenting style that works for them. I use the /* . . . */
style above a program, function, or a complex block of code to provide an overall description of the following code. I reserve the //
style for placing comments inside the code. This practice generally produces code that is clean and easy to read. It also allows nesting comments when using them as a debugging tool (described next). The following program (described in greater detail in Chapter 2) demonstrates my commenting style.
The column of asterisks on the left edge of the top comment is not required. This commenting style was used extensively by C programmers and used extensively in the Unix source code. It remains popular with many programmers, and some text editors (e.g., gvim) add it automatically.
Here are my four "rules of thumb" for using comments:
While developing or debugging a program, there are times when it is helpful to temporarily remove code from the program, a technique called commenting out the code. We can imagine that a program contains some necessary code. Further, imagine that the programmer wants to try other versions - perhaps to simplify the code or make it more efficient. The programmer reverts to the original code if the new versions fail. Reversion is troublesome if the programmer removed the original code but simple if it's commented out.
The code may be necessary, but what if it contains a bug, especially one that crashes the program? Temporarily removing some code may help identify or locate a bug. Assuming we don't introduce new bugs when we comment out code, we know the bug is in the commented-out code if the program doesn't crash when it runs. Similarly, I also use comments in the demonstration programs and posted solutions to illustrate alternative approaches.
It is relatively easy to comment out code if the only "real" comment characters used are the //-style. Consider the following examples:
Original Code | Commented Out Code |
---|---|
statement1; statement2; /* does something neat */ statement3; statement4; |
/*statement1; statement2; /* does something neat */ statement3; statement4;*/ |
(a) | |
statement1; statement2; // does something neat statement3; statement4; |
//statement1; //statement1v1; statement1v2; statement2; // does something neat statement3; statement4; |
(b) | |
statement1; statement2; // does something neat statement3; statement4; |
/*statement1; statement2; // does something neat statement3; statement4;*/ |
(c) | |
statement1; statement2; // does something neat statement3; statement4; |
statement1;
//statement2; // does something neat
statement3;
statement4;
|
(d) |
The Visual Studio editor has a couple of formatting commands that assist in commenting out code (which attests to the commonness of the technique).
Ctrl-K
followed by Ctrl-C
Ctrl-K
followed by Ctrl-U