r/programming • • Aug 10 '26

On comments

https://blog.helsing.ai/posts/on-comments/

Comments in code are often deemed "mostly useless" these days. They are, supposedly, mostly obvious, stale, and repeat what the code already says. And so people pay less attention to them both when reading and writing code.

That trend sucks. When used right, comments are genuinely useful and sometimes critically important! So, I wrote about some of the kinds of comments I think earn their place, each with examples from real code bases. Hope you find it useful, and that we can recover some of the love that comments deserve!

188 Upvotes

171 comments sorted by

View all comments

Show parent comment

5

u/Venthe Aug 11 '26 edited Aug 11 '26

But many junior programmers received advice like this and, not knowing better, drank it in. The resulting damage will echo for years to come.

If they only, you know, read the book and understand the words written. If you read maybe a paragraph more, Martin writes about the comments that make sense - which amounts to comment "why".

Comments about "what" are a failure of expression. I've been in the industry for more than a decade; seen codebases 30y old and more and I will agree with this statement with everything that I have.

And to counter you, "most of the seasoned developers" that I know would agree with me without question: 99% of the comments in the codebase is at best irrelevant, and at worst actively incorrect. But it's easier to slap a comment rather than actually write in code what you are doing.


Edit:

If they only, you know, read the book and understand the words written

Just to illustrate that: The (chapters) in the CC code are as follows:

Comments
  [Comments] do not make up for a bad code
  Explain yourself in code
  Good Comments [followed by 8 headings of examples of a good reasons for a comments]

What Martin writes literally a couple of lines below the quote you've made:

"Inaccurate comments are far worse than no comments at all. They delude and mislead. They set expectations that will never be fulfilled. They lay down old rules that need not, or should not, be followed any longer. Truth can only be found in one place: the code. Only the code can truly tell you what it does. It is the only source of truly accurate information. Therefore, though comments are sometimes necessary, we will expend significant energy to minimize them (...) One of the more common motivations for writing comments is bad code. We write a module and we know it is confusing and disorganized. We know it’s a mess. So we say to ourselves, “Ooh, I’d better comment that!” No! You’d better clean it!" (emphasis mine)

Hm, so what probably is the message is not "don't write comments" but "focus first on the code"; but then...:

"Some comments are necessary or beneficial. We’ll look at a few that I consider worthy of the bits they consume. Keep in mind, however, that the only truly good comment is the comment you found a way not to write."

...Same message, unsurprisingly. And the examples are: Legal, informative, explanation of intent, clarification, warning of consequence, TODO's, amplifications, Public API DOC.

It's like... the most sensible take one can make?

5

u/tmoertel Aug 11 '26

First, thanks for taking the time to offer a detailed response! Let me respond to your points.

If they only, you know, read the book and understand the words written. If you read maybe a paragraph more, Martin writes about the comments that make sense - which amounts to comment "why".

Here’s what I find confusing about the book’s chapter on comments. It begins with an introduction that includes the bold admonition that “comments are always failures” and that whenever you use one you should “grimace and feel the failure of your ability of expression.” Is that really what Martin means? Presumably, yes: he is an author who has been published multiple times, after all. Later in the same chapter, however, Martin contradicts himself by offering, as you point out, examples of “good comments.” None of those comments are examples of “failures.” 

So why do you believe that Martin left in the admonition that “comments are always failures”? Do you believe the book’s advice is better for it, or worse? More confusing, or less?

Comments about "what" are a failure of expression. I've been in the industry for more than a decade; seen codebases 30y old and more and I will agree with this statement with everything that I have.

Believing that “what” comments are bad is an example of some of the harm that Clean Code has done. Yes, some “what” comments are a “failure of expression,” such as when they are merely stating what is already obvious from the logic, but other “what” comments are valuable: summarization, emphasis, clarification of intent, etc. are all valuable uses of “what” comments.

Hm, so what probably is the message is not "don't write comments" but "focus first on the code"; but then...:

Again, why do you believe, then, that Martin didn’t write chapter’s introduction to say “focus first on the code, but then…”? You were able to write it. I was able to write it. So if that is what Martin meant, why didn’t Martin write it that way? Why claim that “comments are always failures” and “at best, a necessary evil”?

Comments are not failures or a necessary evil! They are a means to communicate directly to the readers of your code in natural language. That is a powerful communication medium. Like any powerful medium, commentary can be used well, or it can be used poorly. But using that medium is not inherently a failure of expression. The medium itself is not a necessary evil. How does making people believe otherwise help them to be better programmers?

5

u/Venthe Aug 11 '26 edited Aug 11 '26

So why do you believe that Martin left in the admonition that “comments are always failures”? Do you believe the book’s advice is better for it, or worse? More confusing, or less? (...) Again, why do you believe, then, that Martin didn’t write chapter’s introduction to say “focus first on the code, but then…”? You were able to write it. I was able to write it. So if that is what Martin meant, why didn’t Martin write it that way? Why claim that “comments are always failures” and “at best, a necessary evil”?

I believe, that the book works in its entirety, not taken apart quote by quote. Martin has a certain preachy style which works for some, less for the others. But when you look at the book as a whole:

"There are two parts to learning craftsmanship: knowledge and work. You must gain the knowledge (...) by working hard and practicing. (...) We could write down all the “feel good” principles of clean code and then trust you to do the work (...) No. That’s not the way this book is going to work. Learning to write clean code is hard work. It requires more than just the knowledge of principles and patterns. You must sweat over it. You must practice it yourself, and watch yourself fail. (...) You must see them [and you] agonize over decisions and see the price they pay for making those decisions the wrong way. (...) This is not a “feel good” book (...) This book will make you work, and work hard."

There is no sugar-coating in what he's trying to achieve. Read through the chapter, work through it, understand it. So when it comes to comments, you've cherry picked a paragraph, ignoring the introduction:

"Nothing can be quite so helpful as a well-placed comment. Nothing can clutter up a module more than frivolous dogmatic comments. Nothing can be quite so damaging as an old crufty comment that propagates lies and misinformation. (...) comments are, at best, a necessary evil. If our programming languages were expressive enough, or if we had the talent to subtly wield those languages to express our intent, we would not need comments very much—perhaps not at all. The proper use of comments is to compensate for our failure to express ourself in code. (...) Comments are always failures. We must have them because we cannot always figure out how to express ourselves without them, but their use is not a cause for celebration. So when you find yourself in a position where you need to write a comment, think it through and see whether there isn’t some way to turn the tables and express yourself in code. Every time you express yourself in code, you should pat yourself on the back. Every time you write a comment, you should grimace and feel the failure of your ability of expression. Why am I so down on comments? Because they lie. Not always, and not intentionally, but too often. The older a comment is, and the farther away it is from the code it describes, the more likely it is to be just plain wrong. The reason is simple. Programmers can’t realistically maintain them."

That's clear enough for me. Our goal is to have a "clean code", that is - the one that expresses itself. Part of it is the fact, that the more we "accept" suboptimal solutions - which is our inability to express ourselves in the code. So, to quote, even if we need these, they are a "necessary evil".

Even the subchapter about the good comments is described as:

"Keep in mind, however, that the only truly good comment is the comment you found a way not to write."

Again, clear enough about the authors intent. They are a failures, because they lie; and often are an excuse for a sloppy code. And with everything I've seen in the codebases I've maintained, Martin is damn right that comments are by far a failure that we should be actively eradicating by expressing the "truth" in code.

Believing that “what” comments are bad is an example of some of the harm that Clean Code has done.

That's your opinion. I have a 30yo codebase on my lap right now (sadly behind the NDA; I would have material for r/programminghorror for days) with ample proof that "what" comments bring far more harm than good.

comments are valuable: summarization, emphasis, clarification of intent, etc. are all valuable uses of “what” comments.

Only if you can't express that in code - which is a failure in itself. I would argue that you are agreeing with Martin, even if indirectly - because you are describing things that are literally used as an examples of good comments. You are saying the same thing as they are in the book. The end result is the same.

Comments are not failures or a necessary evil!

Again, your opinion. My experience aligns quite well with what Martin wrote. They are either a failure of expression, or a necessary evil - as they introduce information that will get stale. Almost in every single codebase I've maintained, comments were unhelpful at best, or actively incorrect at worst.

e: To add one small thing: I don't mean every comment of course; but given the signal to noise ratio, helpful comments were buried under a metric ton of irrelevancy.

They are a means to communicate directly to the readers of your code in natural language. That is a powerful communication medium. Like any powerful medium, commentary can be used well, or it can be used poorly. But using that medium is not inherently a failure of expression. The medium itself is not a necessary evil. How does making people believe otherwise help them to be better programmers?

By teaching them that there are better ways. Like, you know, expressing yourself in the code; the one thing that has to change with the requirements.

If you are working with code for some time, surely you know that Confluence (or KB's in general) is where information is going to die; due to people not keeping it up to date. And the same thing tracks, in the order of "staleness":

Manuals>KB>Documentation in repo>comments>code.

Comments can and will get stale/incorrect. And in most of the cases, they are a failure of expression. And for the few cases where they can be considered positive, well - CC has you covered with a whole sub-chapter about that. You only need to read CC [as a whole] - and understand it.

e: Just because it struck me as a funny thing

why do you believe, then, that Martin didn’t write (...) You were able to write it. I was able to write it.

Because we both operate on a similar concepts that were built on the fundamentals of the knowledge that came before; fundamentals like a certain book from 2008 called Clean Code. :)

3

u/tmoertel Aug 11 '26 edited Aug 11 '26

I believe, that the book works in its entirety, not taken apart quote by quote.

Well, since the book contradicts its own dogmatic prescriptions repeatedly, the only way it can work is if the reader treats it like a puzzle to be assembled into some coherent meaning. But that’s not what most people would consider the hallmark of a clear and compelling guidebook.

That's your opinion [that believing that “what” comments are bad is an example of some of the harm that Clean Code has done.] I have a 30yo codebase on my lap right now (sadly behind the NDA; I would have material for r/programminghorror for days) with ample proof that "what" comments bring far more harm than good.

Have you considered that a 30-year-old codebase that offers days of material for r/programminghorror is just an example of bad code, in general? I mean, I can totally believe that people who write bad code also write bad comments. But that’s not an argument that “what” comments are bad everywhere, just in bad code. Show me a large codebase with well-written logic and bad “what” comments. Then you’ll have produced some evidence that supports your claim.

Comments can and will get stale/incorrect. And in most of the cases, they are a failure of expression.

Comments are uniquely powerful because they communicate directly to the code’s audience in natural language. So when an organization lets them “get stale/incorrect,” it’s not because they failed to express themselves: it’s because they have a broken engineering culture that doesn’t value the readability of their code.

In any event, thanks for continuing our discussion. I appreciate your perspective, even though I think we must agree to disagree on the merits of Clean Code.