Comments in C++ Code
Lines 1, 4, 10 and 13 in Listing 2.3 contain text in a spoken language (English, in this case) yet do not interfere with the ability of the program to compile. They also do not alter the output of the program. Such lines are called comments. Comments are ignored by the compiler and are popularly used by programmers to explain their code—hence, they are written in human- (or geek-) readable language.
C++ supports comments in two styles:
- // indicates that the line is a comment. For example:
// This is a comment
- /* followed by */ indicates the contained text is a comment, even if it spans multiple lines:
/* This is a comment and it spans two lines */
DO |
DON'T |
Do add comments explaining the working of complicated algorithms and complex parts of your program. Do compose comments in a style that fellow programmers can understand. |
Don't use comments to explain or repeat the obvious. Don't forget that adding comments will not justify writing obscure code. Don't forget that when code is modified, comments might need to be updated, too. |