Comment

Don't comment bad code, rewrite it!
  • Too often, the comments get separated from the code they describe.

  • Inaccurate comments are far worse than no comments at all.

  • Minimize it. Clean code don't need the comments in the first place.

Comments Do Not Make Up for Bad Code

"Ooh, I'd better comment that!" No! You'd better clean it!

Explain Yourself in Code

Bad code:

// Check to see if the employee is eligible for full benefits
if ((employee.flags & HOURLY_FLAG) &&
    (employee.age > 65))

Clean code:

if (employee.isEligibleForFullBenefits())

Good Comments

Refer to a standard license or other external document rather than putting all the terms and conditions into the comment.

Informative Comments

Clarification

When its part of the standard library, or in code that you cannot alter, then a helpful clarifying comment can be useful.

Warning of Consequences

TODO Comments

Javadocs in Public APIs

Javadocs is used for generating the document automatically if the developers write the comments following its format.

Bad Comments

Redundant Comments

Noise Comments

Position Markers

Use them very sparingly, or your eyes will get used to ignoring it. Group the code in different files, or just by line breaks.

Closing Brace Comments

Commented-Out Code

Don't do this! You have Git (or other version control tool)! Just delete it!

Others

  • Attributions and Bylines, like: /* Added by Rick */

  • HTML Comments

  • Too Much Information

  • Javadocs in Nonpublic Code

Last updated

Was this helpful?