代码注释,可以说是比代码本身更重要。这里有一些方法可以确保你写在代码中的注释是友好的:
不要重复阅读者已经知道的内容
能明确说明代码是做什么的注释对我们是没有帮助的。
// If the color is red, turn it green
if (color.is_red()) {
color.turn_green();
}
要注释说明推理和历史
如果代码中的业务逻辑以后可能需要更新或更改,那就应该留下注释:)
/*
The API currently returns an array of items even though that will change in an upcoming ticket. Therefore, be sure to change the loop style here so that we properly iterate over an object
*/
var api_result = {items: ["one", "two"]},
items = api_result.items,
num_items = items.length;
for(var x = 0; x < num_items; x++) { ... }
同一行的注释不要写得很长
function Person(name) {
this.name = name; this.first_name = name.split(" ")[0];// This is just a shot in the dark here. If we can extract the first name, let's do it
}
要把长注释放在逻辑上面,短注释放在后面
不要为了注释而添加不必要的注释
注释要拼写正确
不要为代码注释中的拼写错误找借口。IDE可以为你检查拼写。如果没有这个功能,那就去下载插件,自己动手!
copy:https://blog.csdn.net/u010828718/article/details/51258743
网友评论