—[Google C++ Style Guide][google-c++]
Portions of this section borrow heavily from the Google
[C++][google-c++-comments] and [Python][google-python-comments] style guides.
Every class definition should have an accompanying comment that describes what
it is for and how it should be used.
A file that contains zero classes or more than one class should have a comment
at the top describing its contents.
#
# This list is made with
# lib/tasks/list_american_to_british_spelling_variants.rake.
#
# It contains words with general spelling variation patterns:
# [trave]led/lled, [real]ize/ise, [flav]or/our, [cent]er/re, plus
# and these extras:
# learned/learnt, practices/practises, airplane/aeroplane, ...
sectarianizes: sectarianises
neutralization: neutralisation
Function comments
Every function declaration should have comments immediately preceding it that
describe what the function does and how to use it. These comments should be
descriptive (“Opens the file”) rather than imperative (“Open the file”); the
comment describes the function, it does not tell the function what to do. In
general, these comments do not describe how the function performs its task.
Instead, that should be left to comments interspersed in the function’s code.
Every function should mention what the inputs and outputs are, unless it meets
all of the following criteria:
- not externally visible
- very short
- obvious
You may use whatever format you wish. In Ruby, two popular function
documentation schemes are TomDoc and
. You can also
just write things out concisely:
The final place to have comments is in tricky parts of the code. If you’re
going to have to explain it at the next code review, you should comment it now.
Complicated operations get a few lines of comments before the operations
commence. Non-obvious ones get comments at the end of the line.
def fallbacks_for(the_locale, opts = {})
# dup() to produce an array that we can mutate.
ret = @fallbacks[the_locale].dup
# We make two assumptions here:
# 1) There is only one default locale (that is, it has no less-specific
# children).
# 2) The default locale is just a language. (Like :en, and not :"en-US".)
if opts[:exclude_default] &&
ret.last != language_from_locale(the_locale)
end
ret
end
On the other hand, never describe the code. Assume the person reading the code
knows the language (though not what you’re trying to do) better than you do.
Punctuation, spelling and grammar
Pay attention to punctuation, spelling, and grammar; it is easier to read
well-written comments than badly written ones.
Comments should be as readable as narrative text, with proper capitalization
and punctuation. In many cases, complete sentences are more readable than
sentence fragments. Shorter comments, such as comments at the end of a line of
code, can sometimes be less formal, but you should be consistent with your
style.
Although it can be frustrating to have a code reviewer point out that you are
using a comma when you should be using a semicolon, it is very important that
source code maintain a high level of clarity and readability. Proper
punctuation, spelling, and grammar help with that goal.
Use TODO comments for code that is temporary, a short-term solution, or
good-enough but not perfect.
TODOs should include the string TODO in all caps, followed by the full name
of the person who can best provide context about the problem referenced by the
TODO, in parentheses. A colon is optional. A comment explaining what there is
to do is required. The main purpose is to have a consistent TODO format that
can be searched to find the person who can provide more details upon request.
A TODO is not a commitment that the person referenced will fix the problem.
Thus when you create a TODO, it is almost always your name that is given.
# bad
# TODO(RS): Use proper namespacing for this constant.
# bad
# TODO(drumm3rz4lyfe): Use proper namespacing for this constant.
# good