Proper use of comments

来源:互联网 发布:360软件清理大师 编辑:程序博客网 时间:2024/05/22 01:44
1
cout << "Hello world!" << endl; // Everything from here to the right is ignored.

Typically, the single line comment is used to make a quick comment about a single line of code.

1
2
3
cout << "Hello world!" << endl; // cout and endl live in the iostream library
cout << "It is very nice to meet you!" << endl; // these comments make the code hard to read
cout << "Yeah!"<< endl; // especially when lines are different lengths

Having comments to the right of a line can make the both the code and the comment hard to read, particularly if the line is long. Consequently, the // comment is often placed above the line it is commenting.

1
2
3
4
5
6
7
8
// cout and endl live in the iostream library
cout << "Hello world!" << endl;
 
// this is much easier to read
cout << "It is very nice to meet you!" << endl;
 
// don't you think so?
cout << "Yeah!"<< endl;

The /* and */ pair of symbols denotes a C-style multi-line comment. Everything in between the symbols is ignored.

1
2
3
/* This is a multi-line comment.
   This line will be ignored.
   So will this one. */

Since everything between the symbols is ignored, you will sometimes see programmers “beautify” their multiline comments:

1
2
3
4
/* This is a multi-line comment.
 * the matching asterisks to the left
 * can make this easier to read
 */

Multi-line style comments do not nest. Consequently, the following will have unexpected results:

1
2
/* This is a multiline /* comment */ this is not inside the comment */
//                                ^ comment ends here

Rule: Never nest comments inside of other comments.

Proper use of comments

Typically, comments should be used for three things. At the library, program, or function level, comments should be used to describewhat the library, program, or function, does. For example:

1
// This program calculate the student's final grade based on his test and homework scores.
1
// This function uses newton's method to approximate the root of a given equation.
1
// The following lines generate a random item based on rarity, level, and a weight factor.

All of these comments give the reader a good idea of what the program is trying to accomplish without having to look at the actual code. The user (possibly someone else, or you if you’re trying to reuse code you’ve already written in the future) can tell at a glance whether the code is relevant to what he or she is trying to accomplish. This is particularly important when working as part of a team, where not everybody will be familiar with all of the code.

Second, within the library, program, or function described above, comments should be used to describe how the code is going to accomplish it’s goal.

1
2
3
/* To calculate the final grade, we sum all the weighted midterm and homework scores
    and then divide by the number of scores to assign a percentage.  This percentage is
    used to calculate a letter grade. */
1
2
3
4
5
6
// To generate a random item, we're going to do the following:
// 1) Put all of the items of the desired rarity on a list
// 2) Calculate a probability for each item based on level and weight factor
// 3) Choose a random number
// 4) Figure out which item that random number corresponds to
// 5) Return the appropriate item

These comments give the user an idea of how the code is going to accomplish it’s goal without going into too much detail.

At the statement level, comments should be used to describe why the code is doing something. A bad statement comment explainswhat the code is doing. If you ever write code that is so complex that needs a comment to explain what a statement is doing, you probably need to rewrite your code, not comment it.

Here are some examples of bad line comments and good statement comments.

0 0
原创粉丝点击