—[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.

    1. #
    2. # This list is made with
    3. # lib/tasks/list_american_to_british_spelling_variants.rake.
    4. #
    5. # It contains words with general spelling variation patterns:
    6. # [trave]led/lled, [real]ize/ise, [flav]or/our, [cent]er/re, plus
    7. # and these extras:
    8. # learned/learnt, practices/practises, airplane/aeroplane, ...
    9. sectarianizes: sectarianises
    10. 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.

    1. def fallbacks_for(the_locale, opts = {})
    2. # dup() to produce an array that we can mutate.
    3. ret = @fallbacks[the_locale].dup
    4. # We make two assumptions here:
    5. # 1) There is only one default locale (that is, it has no less-specific
    6. # children).
    7. # 2) The default locale is just a language. (Like :en, and not :"en-US".)
    8. if opts[:exclude_default] &&
    9. ret.last != language_from_locale(the_locale)
    10. end
    11. ret
    12. 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.

    1. # bad
    2. # TODO(RS): Use proper namespacing for this constant.
    3. # bad
    4. # TODO(drumm3rz4lyfe): Use proper namespacing for this constant.
    5. # good

    Commented-out code

    • Never leave commented-out code in our codebase.
      []