r/programming • • 1d ago

Who are code comments for?

https://sheets.works/data-viz/who-are-code-comments-for

I've had my share of swear words, usually every time I missed a semicolon. And that sent me down a rabbit hole: what about everyone else? What do other programmers write when the code fights back?

So we pulled every comment out of 64 big projects on GitHub. That's 295,820 comments. Then we went through them and asked one question about each: wh

0 Upvotes

5 comments sorted by

10

u/Downtown_Cat_7425 1d ago

The best test I know: delete the comment and see whether a competent teammate can still answer *why* the code is the way it is. Comments that restate the code are noise; comments that record a constraint, a workaround, or a decision the code cannot express are valuable. I also write comments for my future self when the reason is not visible in the diff - "we tried X and it failed because Y" saves more time than any docstring. Rule of thumb: if a comment has to be updated every time the code changes, it is a liability.

6

u/addmoreice 1d ago

Business reasons are also a great one, especially if it leaks into industry/domain issues which might not be obvious.

Example:

"We support two machines for the demo here, not one, since we have had customers ask if this monitoring system works on 'dual headed' machines which under the hood act like two machines even though it's a single controller for two machines."

2

u/jhartikainen 1d ago

if a comment has to be updated every time the code changes, it is a liability.

This seems like a pretty solid smell test, good idea.

5

u/CrabHuntLover 1d ago

Comments are for yourself when you come back to read that code in 3 months.

3

u/JadisGod 13h ago

Sometimes I add comments just because the different syntax coloring turns them into an aesthetic separator. It makes group sections feel better and enables visual skimming.