Clean code comments
Clean code comments
How should be pretty straightforward from the code. If not, the code
needs to be refactored.
Of course, there are exceptions. If the code uses an algorithm that is not
common knowledge (for example calculating a pair of public - private
keys), then it might be useful to document how the code is achieving the
result.
Comments Do Not Make Up for Bad
Code
Comments in the code should be used only when naming
fails to explain thoroughly the code.
If a comment is needed, the first option should be to try to
improve the code so it does not need an explanation.
vs
“Don’t comment bad code—rewrite it.”
Good Comments
Some comments are necessary or
beneficial. However the only truly good
comment is the comment you found a
way not to write.
Legal Comment
Sometimes our corporate coding standards force us to write certain
comments for legal reasons.
For example, copyright and authorship statements are necessary and
reasonable things to put into a comment at the start of each source file
Informative Comments
It is sometimes useful to provide basic information with a
comment.
Explanation of Intent Comments
Sometimes a comment goes beyond just useful information
about the implementation and provides the intent behind a
decision.
Clarification
Sometimes it is just helpful to translate the meaning of some
obscure argument or return value into something that's
readable.
In general it is better to
find a way to make that
argument or return value
clear in its own right; but
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
Sometimes it is useful to warn other programmers about
certain consequences.
TODO / FIXME Comment
It is sometimes reasonable to leave “To do” notes in the form
of //TODO comments.
In the following case, the TODO comment explains why the
function has a degenerate implementation and what that
function's future should be.
TODO / FIXME Comment
TODOs are jobs that the programmer thinks should be done.
Example: The method does not return when this.closed becomes true. It
returns if this.closed is true; otherwise, it waits for a blind time-out and
then throws an exception if this.closed is still not true.
Mandated Comments
It is just plain silly to have a rule that says that every function must
have a javadoc, or every variable must have a comment.
Comments like this just clutter up the code, propagate lies, and
lend to general confusion and disorganization. –Good only on
Public API-
Journal Comments / History Tracker
Comments
Do not document the history in the comments area. That is what
source control is for.
Attributions and Bylines
/* Added by Rick */
Commented-Out Code
Do not comment junk
If you don't need anymore, please delete it, you can back later
with your VCS if you need it again.
Do not keep the code for the future, by commenting it.
Today we have source code control
systems, we don't need this type of
logs.
Noise Comments
The comments in the follow examples doesn't provides new
information.
VS
Position Markers
Function Headers
Short functions don’t need much description. A well-chosen name
for a small function that does one thing is usually better than a
comment header.