Search code examples
c++comments

What's the name for this comment structure?


I'm teaching a C++ programming course for the first time in a while and, somewhat based on the elementary book examples, I find that students want all of their comments to be end-of-line like so:

for (int count = 1; count <= days; count++) {   // Loop for each day
    organisms += organisms * increase;          // Compute organisms
    cout << organisms << endl;                  // Print out organisms
}

In contrast, I'm trying to get them to use dedicated comment lines that summarize several lines of code for this purpose:

// Update & display organisms for each day
for (int count = 1; count <= days; count++) { 
    organisms += organisms * increase;          
    cout << organisms << endl;                  
}

Is there a proper name for this latter, not-end-of-line comment styling?


Solution

  • There's not a specific naming convention to the comment structure you ask beyond the // to mean a line comment, and how to comment is something that is usually left to the style guide (if any) of the source in question. That wiki article even states:

    The flexibility provided by comments allows for a wide degree of variability, but formal conventions for their use are commonly part of programming style guides.

    Commenting style does not have a syntax naming convention beyond block comments (sometimes called C-style commenting), line comments and the such, unlike coding syntax conventions (like OOP, functional, lambda expressions, etc.).

    This is because a comment is intended, typically, to annotate something, and not as a function of the code itself.

    I say typically because you can use comments for things such as copyrights, or to help generate documentation (via something like JavaDoc style)

    Again though, these are "styles" of commenting, and not a syntactical structured name.

    Is there a proper name for this latter, not-end-of-line comment styling?

    No. Again this is because comment style, structure and content is left to the author. One could even follow iambic-pentameter in their comments if they so wished.

    If you must give it a "name", you could call it a single line concise functional comment as that what your example is; a single line comment that is concise and describe what the functional part of that code does.


    Ultimately you're trying to teach your students what the purpose of comments really are for, and a career programmer doesn't need every line of their code commented, much like a dictionary doesn't have every part of a definition annotated.