cleancode.in

Comments that say why

Say why, not what source. A comment that disagrees with the code is worse than none source. Rename first; comment what the code cannot say.

Should a comment say what the code does?

  • MDN: restating the code in prose is not a good use of comments source
  • ABAP: Nobody needs repeating the code in natural language source
  • PEP 8: Inline comments are unnecessary and in fact distracting if they state the obvious. source

When does a comment help?

  • MDN: If the purpose or logic of the code isn't obvious, add a comment with your intention source

How does a comment stay true?

  • PEP 8: Comments that contradict the code are worse than no comments. Always make a priority of keeping the comments up-to-date when the code changes! source

What about code you commented out?

  • ABAP: Deleted code can be reproduced from the version history later on. source

Can a better name replace a comment?

  • ABAP: Improve your names instead of explaining what they really mean or why you chose bad ones. source
  • MDN: Comments are also not necessary when functions have explicit names that describe what they're doing. source

How should a comment read?

  • PEP 8: Comments should be complete sentences. source
  • Rust: Comments should usually be complete sentences. Start with a capital letter, end with a period (.). source

How does a comment start and end?

  • C#: Begin comment text with an uppercase letter. End comment text with a period. source

Do parameters need tags?

  • Kotlin: Generally, avoid using @param and @return tags. Instead, incorporate the description of parameters and return values directly into the documentation comment, and add links to parameters wherever they are mentioned. source

Where does a comment go?

  • C#: Place the comment on a separate line, not at the end of a line of code. source
  • Rust: Prefer line comments (//) to block comments (/* ... */). source
  • Go: Comments that appear before top-level declarations, with no intervening newlines, are considered to document the declaration itself. source

What do cluttered comments look like?

Before, in JavaScript
// get the user
const u = await getUser(id);
// check if u is null
if (u === null) {
  return;
}
// loop over the items and add up
let t = 0;
for (const i of u.cart) {
  t += i.p; // add price
}
// let t2 = t * 1.18;

What does the code look like cleaned up?

After
const user = await getUser(id);
if (user === null) {
  return;
}

// Prices are whole paise, not rupees with decimals,
// so adding them up never rounds.
let cartTotalPaise = 0;
for (const item of user.cart) {
  cartTotalPaise += item.pricePaise;
}

What changed?

  • Comments that retold the next line: gone. source source
  • Names say what they said: user, item, cartTotalPaise. source
  • Commented out code: deleted; the history keeps it. source
  • The one comment left says why: paise keep the sum exact. source
  • Full sentences, on lines of their own. source source