Code comments

You should have learned different formats for code comments

// a one line comment
std::cout << "Hello C++!" << std::endl;  // this is a comment
puts("Hello C!");
/*
a block comment
printf("Hello Alice!\n");
printf("Hello %s!\n", "Bob");
*/

What you may not have have learned is when to use comments. This is partly a stylistic discussion, and some people have very strong feelings about the use of comments in software. These opinions range from “never use them, ever”, to enormous comment blocks at the top of every source file and before each function. Others expect a line of code at the end of nearly every line of source.

I am not any of these people.

The primary focus of this course is on clarity.

Comments should always and only state things that cannot be captured well in regular code. For example:

One notable exception to the ‘keep comments to a minimum’ rule is if you are commenting a public API for a library and most readers will only see generated documentation and not the source code.

In that case, you are generally adding comments to be read by a documentation parser/generator such as Doxygen. For example:

/**
 * <A short one line description>
 *
 * <Longer description>
 * <May span multiple lines or paragraphs as needed>
 *
 * @param  Description of method's or function's input parameter
 * @param  ...
 * @return Description of the return value
 */

Comments in assignments

In this course, I also need everyone to assert that their work is their own. For that reason, the top of every source file should contain your name and student ID:

// file.cpp
// Dave Parillo, 123456789

Commenting “anti-patterns”

An anti-pattern is a common response to a recurring problem that is ineffective. Anti-patterns represent examples that you should not copy! As bad as they are, they can still be instructive.

The preceding advice may conflict with what you have been told in the past about commenting your code. Don’t worry too much about that for now. Remember the focus is on clarity, not how many comments you write. Eventually some future employer will require you to (hopefully) adhere to some coding standard and you should follow that guidance when you encounter it.

Commenting Dos and Don’ts

I spent a lot of time saying how not to write comments. How should you?

First, avoid the four common excuses related to comments. In other words, I don’t need to write comments because:

  • Good code is self-documenting.

    There are many things that can’t be expressed in code.

    • Why a particular feature was implemented.

    • What information callers need beyond what is available in the function signature.

    If you never write comments, then you can never explain these important details.

  • I don’t have time to write comments.

    Writing good comments takes far less time then writing good code. Writing comments first can help make your designs better and prevent rework. With that in mind, comments are a net time-saver.

  • Comments get out of date and become misleading

    This often happens when people write documentation ‘far’ away from the code being documented. If the comments go stale it is because the code changed, but the comments did not. Keeping code and comments close minimizes this problem.

  • Comments are worthless, why bother?

    Arguably, the most valid excuse. However, just because other comments are bad does not mean you should not write comments yourself.

The overall idea behind comments is to capture information that was in the mind of the designer but couldn’t be captured or expressed in code.

  • Comments should describe things that aren’t obvious from code.

  • Pick a commenting convention and use it consistently. In this course, I generally use the conventions from the tool Doxygen.

  • Don’t repeat what code says in comments.

    • Use different words in the comments from the name of the things being described.

  • Focus on what and why, not how

    The code says how an entity is defined or performed. Comments help readers understand what the code is doing.

  • Write comments before writing code. Use comments as part of the design process when writing code.

  • Avoid duplicate comments in the same way we strive to avoid duplicate code.


More to Explore

You have attempted of activities on this page