Proper use of comments

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值