To Comment or Not to Comment
2017-07-17
Comments are not always helpful.
Bad
// Subtract discount from final price. final_price = (num_items * item_price) - std::min(5, num_items) * item_price * 0.1; // Filter offensive words. for (std::string word : words) { ... } int width = ...; // Width in pixels. // Safe since height is always > 0. return width / height;
It's often better to make your code self-explanatory.
Good
price = num_items * item_price; discount = std::min(5, num_items) * item_price * 0.1; final_price = price - discount; FilterOffensiveWords(words); Pixels width = ...; CheckArgument(height > 0); return width / height;
Avoid using comments to explain what code does. Use comments to explain why code does something.