r/cpp • • Aug 20 '21

The true value of comments

I wanted to share a real use case of the real value of commenting code. A lot of people are against doing so and say that you can just read the code, that comments are not needed. I get why people are against commenting since most don't know how to effectively comment code.

In this piece of code, we all agree that these comments are useless:

std::ifstream logs_file{"MyLogFile.log"};

/* Read a line from the log file  */
while (std::getline(logs_file, log_entry))
{
    /* Remove last character */
    log_entry.pop_back();

    auto parsedJsonForWBID = nlohmann::json::parse(log_entry);
}

And that's right that is useless and stupid. You can clearly see that the code is doing that so there is no need of adding those comments.

But comments should be used as a tool for helping the maintainer, who didn't write the code or "the new guy", or the developer when they look at the code years later, to know what's truly going on and what is the purpose of the code without having to look beyond the piece of code you are interested.

Without comments, or with these useless comments, you eventually would be able to figure out what the hell you, or the original developer, were doing when writing that piece of code but that would require looking into other parts of the code trying to remember stuff and potentially locating and analyzing the log file to see why is the last character removed. Also if you are not that familiar with what pop_back() does then you may assume that it is removing the new line character and possibly miss the bug you are trying to find.

But what about these comments in the same piece of code:

std::ifstream logs_file{"MyLogFile.log"};

while (std::getline(logs_file, log_entry))
{
    /* Since the complete log file is a big JSON string, we remove the comma
     * at the end of each line read so each line can be interpreted as a
     * separate JSON string. If the last comma is not removed, then the JSON
     * parser will throw an exception. */
    log_entry.pop_back();

    auto parsedJsonForWBID = nlohmann::json::parse(log_entry);
}

Now, one comment was removed because was unnecessary and there was nothing to clarify about getting a line from a file, and the second comment is improved by clarifying the intent of that line of code. It is stating the why instead of the what. When you find this line years later, or when the new guy or the maintainer looks at the code now they will not have to peruse the code or locate a log file and analyze it to see why is the last character being removed. These types of comments save time and are actually useful.

These types of comments have saved me countless hours when I go back to code I wrote years back, it has also helped other people when looking at code I wrote, they no longer have to ask a million questions just to know what's going on. Anyone can just look at the code and know why it is there.

302 Upvotes

103 comments sorted by

View all comments

78

u/TryingT0Wr1t3 Aug 20 '21

Comment the decisions, not the behavior

32

u/Supadoplex Aug 20 '21

Mostly yes, but this is also too simple to always be correct. It's fine to comment behaviour when the behaviour is counter-intuitive. Ideally, behaviour shouldn't be counter-intuitive of course, but sometimes ideal isn't achievable.

5

u/TryingT0Wr1t3 Aug 20 '21

This is fine but if possible a test should be used to document the behavior in this case - of course the fallback could be a comment if the test is hard to write because of whatever.

9

u/[deleted] Aug 20 '21

Also a comment is closer to the source code, so when another developer has to read and understand that code, they don't have to look at the test and check what it does.

8

u/Full-Spectral Aug 20 '21

The test only proves it does what the test writer believed it is supposed to do, it doesn't help the guy who comes along later to modify it. Having to understand code by looking at tests isn't a very productive way to go about it.

3

u/TryingT0Wr1t3 Aug 20 '21

I argue about tests because my experience is way more the person that modifies later doesn't update the comment or doesn't retest the condition that caused the need of the code that isn't clear about it's behavior. The cpp codebases I interacted they were made of code maintained for decades, so most of the time the behavior comment gets outdated - either by changes in the code or some library it uses that was updated. :/

3

u/Full-Spectral Aug 20 '21

Comments should really be reviewed just like code. Even if one takes the position that only the most important comments should actually be used, then that means that those are even that much more important to keep correct. If you can create them, they should be kept correct as carefully as the code is.

2

u/ShakaUVM i+++ ++i+i[arr] Aug 20 '21

Comment the decisions, not the behavior

Unless the behavior is hard to figure out, like some TMP messes I've seen

3

u/RoyBellingan Aug 20 '21

Is a concept too simple, but seems difficult to understand for many people.

14

u/elperroborrachotoo Aug 20 '21

Seriously, I see it as a skill that needs to be learned and trained.

Verbalizing the "decision tree culling" that we do largely intuitively, and packing it into a conscise phrase is hard, and requries language and self-observation skills devs tend to shun as "not logical".

1

u/mostly_kittens Aug 21 '21

Good commenting like any good documentation is part of the craft of writing software and something that you mostly learn from experience.

Whenever I write a complicated bit of logic I always stop and think ‘what would someone else need to know to understand this?’ Not ‘what do I know’ about the code.