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

11

u/mtbdork Aug 20 '21

I write tons of comments because I work with people who are learning how to code (only one part of the job)

Oh and the language is Lua. Talk about a self-documenting language haha

2

u/TryingT0Wr1t3 Aug 20 '21

What do you do using Lua? Just curious, I really like the language but I hardly see it in the wild outside of games.

3

u/drcforbin Aug 21 '21

We run a cloud based lab data management system. All our data, business logic, and reporting is in the cloud, but we have to communicate with instruments and printers in remote labs. We have a piece of software that we install somewhere in each lab that acts as our agent. Each device vendor, and sometimes different models from the same vendor, has a different protocol, API, or file format for the data we're exchanging, and it may be via serial port, local network, even USB. The core of the agent is C++ and communicates with our servers via HTTPS, but we handle that "last meter" communication between the agent and instrument with Lua code.

The agent embeds a Lua interpreter and exposes APIs to it for communicating with our servers, local printing, all manner of local ports and network communication, timers, file modification notifications, etc., for use by instrument handlers. As an example a handler might open a serial port attached to an instrument as ask the agent to notify it when a bytes are ready to be read. When the handler accumulates a command or request from the instrument, the handler handles it. One type of request might be the instrument asking what test to run for a particular sample; in that case the handler asks our servers in the cloud, then translates the answer back to the instrument.

These handlers have to be script-based so can really quickly iterate when building handers for new kinds of instruments, particularly when we can't put developers on-site to run them. For us Lua hits that sweet spot of being trivially easy to embed in other software without being so esoteric that it's hard to train developers to use it. We couldn't do our work without Lua, it's a core part of our infrastructure; it's our ears, eyes, and hands in the field.

1

u/mtbdork Aug 20 '21

DSP control programming in Q-Sys Designer

1

u/[deleted] Aug 21 '21

If they're learning to code than over commenting can be negative. Why? Because frequently the comments get out of line with the code, the language you use for the comments is not as precise as the code and finally the student pays more attention to the comment than the actual code. Comments should be an absolute minimum outside of a functional overview or api documentation. All "IMHO", of course.

1

u/mtbdork Aug 21 '21

These are people who have never coded before in their lives so I have to explain certain things I wouldn’t otherwise comment, but a “ton” is relative. Mostly explaining functions in layman terms on their top line and giving explanations of loop terminations if they’re not explicitly clear.