// 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 */
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
}
四、要把长注释放在逻辑上面,短注释放在后面(√)
注释如果不超过120个字符那可以放在代码旁边。否则,就应该直接把注释放到语句上面。
if (person.age < 21) {
person.can_drink = false; // 21 drinking age
/* Fees are given to those under 25, but only in
some states. */
person.has_car_rental_fee = function(state) {
if (state === "MI") {
return true;
}
};
}
if (person.age >= 21) {
person.can_drink = true; // A person can drink at 21
person.can_smoke = true; // A person can smoke at 18
person.can_wed = true; // A person can get married at 18
person.can_see_all_movies = true; // A person can see all movies at 17
//I hate babies and children and all things pure because I comment too much
}