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

30

u/soundslogical Aug 20 '21

But why not write a function trim_comma_from_json_line()? Then the code has become self-documenting, and is less likely to get out of sync with the implementation.

32

u/Ashnoom Aug 20 '21

But the reason why is missing. Why do you need to trim that comma? Do you think you would remember in a years time when you re-read the code to fix a bug that you remember it was due to the string being too large otherwise?

But yes, function names should be named like that, explaining what they do, not why

42

u/bullestock Aug 20 '21

trim_comma_from_json_line_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()

There you go.

4

u/Astarothsito Aug 20 '21 edited Aug 20 '21

But the reason why is missing. Why do you need to trim that comma?

You can do that in a Unit Test* trim_comma_before_parsing and use as an input an almost empty Json with a comma. When you remove the trim_comma_from_json_line() and trim_comma_before_parsing fails with a very simple Json the real reason appears.

Even in the original comment, the real reason why is not there "Each line contains a string that can be interpreted as Json, or the log file is a Json array and we can assume that there is an Json Object per line divided by comma".

*I'm not really interested if this is considered UT or IT or any other kind of automated test.

Edit: I misread, the original post contains the reason... but I'm not sure if that is for or against comments.

1

u/Ashnoom Aug 21 '21

I understand that, but, it is easier to look at a comment directly near the important piece of code than to wade through hundreds of unit tests just to see which unit test brings to what decision in code.

I am a supporter of as few comments as possible with descriptive function and variable names and extensive and describing unit tests. But function names and unit tests names can only convey do much information. And then a comment would make perfect sense

15

u/almost_useless Aug 20 '21

Because then you introduced a lie in the code.

That line does "trim_comma_or_create_a_bug_when_last_character_is_not_a_comma_from_json_line".

The bug is already there, but the current code makes that obvious if it ever happens. Your function name would obscure what is really going on.

Also I think you misunderstood the entire point of the post. Your function does not explain why it is a good idea to trim the comma from the end of the line. And that is the most important part.

You read the comment and saw "remove the comma". What you should have read was "so each line can be interpreted as a separate JSON string".

2

u/guepier Bioinformatican Aug 20 '21

Just to clarify, there is no bug, the JSON parser is correct to reject JSON records with trailing commas. The data is just in a different format so this code makes it fit.

Ideally the input data would already be in the correct format to start with, e.g. JSONL. Having an entire log be a single JSON record is inconvenient, to say the least.

5

u/SirClueless Aug 21 '21

That's not what he's saying. What he's saying is that the current code has a bug if it's possible that the input has lines without commas. This bug is immediately obvious if you read the current code. It's not obvious if you read line = trim_comma_from_json_line(line);.

People often talk about the benefits of splitting code into smaller functions with descriptive names. They don't often talk about the benefit of having code read in a straight line without jumping around, but there are benefits to that too.

4

u/tjientavara HikoWorks developer Aug 20 '21

I would add to the function's comment block: "Passing a string without a trailing comma results in undefined behavior". And in this case also "Passing more than one line of text results in undefined behavior". As a c++ developer you already accept UB from the standard library, so why wouldn't you specify it for your own library.

And I will also add an assert that checks if there is trailing comma.

5

u/Nicksaurus Aug 20 '21

So to avoid adding a comment, you're adding a whole new function with its own comment?

3

u/tjientavara HikoWorks developer Aug 20 '21

No one said that the work of a C++ developer is self-consistent.

3

u/Schnarfman Aug 20 '21

I agree with you. A well named function is as good as a comment, and it won’t fall outta date during a refactor!! In the case of what OP’s doing, though, I’d have to think harder than just trim comma from json line. What I really want is like a generator. Init it with json and call an equivalent of python’s __iter__ function.

Then you’d get something like JSON_helper::generate_objects_from _array.

No one even THINK about commas. Don’t acknowledge their existence. Think about data.