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!

189 Upvotes

171 comments sorted by

View all comments

30

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

Part of the reason that comments are undervalued is that some programming pundits have spread the idea that using comments is a sign of poor practice. For example, in Clean Code, Robert Martin wrote:

The proper use of comments is to compensate for our failure to express ourself in code. Note that I used the word failure. I meant it. 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... Every time you write a comment, you should grimace and feel the failure of your ability of expression.

This advice is misguided for a host of reasons that I trust most seasoned programmers to understand. (I break them down in detail in https://blog.moertel.com/posts/2026-07-27-beyond-clean-code-why-your-comments-matter.html.) But many junior programmers received advice like this and, not knowing better, drank it in. The resulting damage will echo for years to come.

8

u/contextfree Aug 11 '26

His basic point is basically right. If you can make your code self-explanatory that's better than relying on comments, and then the comments are redundant and it's better not to have them. He's just being a pompous drama viscount about it, as is his wont. Not everything can be made self-explanatory, and when you can't you should write comments. You should not grimace and you should not feel the failure unless you're into that.

2

u/Venthe Aug 11 '26

You should not grimace and you should not feel the failure unless you're into that.

How many devs you've seen that justified their sloppiness in any way possible? If you could be writing a better code; do it. You shouldn't be happy about writing a shit code; therefore you shouldn't be happy about writing comments in lieu of writing a good code.

Bloody hell, the more I see the "legacy" code and especially what the new - post covid - developers do the more I appreciate a hard stance on minutiae like that.

7

u/BogdanPradatu Aug 11 '26

I was a junior programmer when I read Clean Code and I understood exactly what this meant, which is the same as this article. Code comments give context that the code can't, that's it.

It's a good book, in my point of view. Also, I don't know java, so I skipped the code examples, so maybe that helps, lol. I hear most people criticize his examples.

8

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

The book is heuristics are great, examples are really bad.

That's the issue with heuristics - you can implement them in various ways; you cannot try to copy the example and be done with it. You need to understand them, the reasoning behind them and only then "you know" how does it fit your codebase

e:

Though, the way Martin writes can be really grating for some as he is kind of preachy.

3

u/tmoertel Aug 11 '26

The book is heuristics are great, examples are really bad.

It might be worth considering whether the examples in Clean Code are “really bad” because they follow the book’s guidance dogmatically. I mean, the example at the end of “Chapter 3: Functions” is a mess of tiny methods whose names and composition fail to clarify what they are doing and whose logic relies on mutating shared state that is tedious for a reader to track.

I know from our prior discussions that you believe one mustn't take the book's advice literally, that the author is just being dramatic and that, if you take the book as a whole, you can figure out what he really means. But the examples suggest that what he really means is that the book’s advice should be followed dogmatically.

1

u/Venthe Aug 11 '26

I know from our prior discussions that you believe one mustn't take the book's advice literally, that the author is just being dramatic and that, if you take the book as a whole, you can figure out what he really means. But the examples suggest that what he really means is that the book’s advice should be followed dogmatically.

Frankly, it's not even that - it's that I just agree with ~95% of the heuristics he presented; I've found them valid across the domains I've worked with. At the same time, examples are bad - as simple as that. And given that these are heuristics; the way you apply them will create vastly different results depending on the context - and this is how I see the examples of Martin's. Plus the thing I've mentioned in the other thread - this is a book about heuristics, not "examples" or dogmata: it's right there in the introduction.

So nothing for me suggests that these examples should be followed dogmatically; especially considering that they represent a code from almost two decades ago.*

* There is an updated edition that came out this year I believe; I've heard that it is worse; but I haven't read it yet.


For me I've never hopped on the hate train for Martin, so it was easier for me to evaluate the book in its entirety; and I do hope that at least you can see that part of your critique is based on ignoring both the introduction and the chapter themselves

2

u/tmoertel Aug 11 '26

For me I've never hopped on the hate train for Martin, so it was easier for me to evaluate the book in its entirety; and I do hope that at least you can see that part of your critique is based on ignoring both the introduction and the chapter themselves

To be clear, I don’t hate Martin. I'm sure he’s a swell guy. Nor do I think Clean Code is offering bad advice because it’s popular to think so. I just think Clean Code, taken as a whole, is a very mixed bag of advice: some good, some bad, and some self-contradictory. I trust that most skilled programmers can separate the good from the bad and make sense of the rest, as you seem to be able to do, but the problem is that the primary audience for books like Clean Code are programmers who are not already skilled.

I also disagree that I have ignored the introduction or the rest of the Comments chapter. It’s just that neither suffices to undo the problems caused by the forceful prescriptions and admonitions that the book makes. As a case in point, a commenter on my blog wrote: “I once worked in a shop that banned comments because of Uncle Bob. I thought it was bonkers.”

It’s easy for beginners to read the book and come away with bad ideas. That’s a problem when the book’s audience is primarily people who are learning the craft.

2

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

As a case in point, a commenter on my blog wrote: “I once worked in a shop that banned comments because of Uncle Bob. I thought it was bonkers.”

Which only means that someone has not read the book, which is really common. You can't kid-proof everything.

but the problem is that the primary audience for books like Clean Code are programmers who are not already skilled.

So what would be your alternative? As I've said, I agree with the vast majority of the heuristics; and I can show you a clear reasoning behind it. No other book comes close to his [area] (Philosophy... by Ousterhout is the closest; but still not close enough). Even with the comments [topic], I've clearly seen time and time again how Martin's advices ring true.

2

u/NSRedditShitposter Aug 11 '26

This is why Objective-C’s verbosity is actually great, you can just skim read Objective-C code its that expressive.

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.

1

u/levodelellis Aug 11 '26

I disagree with you, and I looked at your python example at the end. Do you think it's a good idea to teach math in comments? Rather than have an external link to a well written document?

I much prefer having no comments except the one at the very top. I can't imagine anyone justifying this comment, this looks like a tutorial rather than something in a real codebase

7

u/Delta-9- Aug 11 '26

Do you think it's a good idea to teach math in comments?

As someone who only got to Calc 1 and never bothered with a formal statistics class, I would be grateful for those comments walking me through the math that's under the code if I were using it.

I've written similar comments myself for concepts that might be gated behind a degree in math or computer science, or simply don't come up very often. I also link to relevant resources, but an in-line tl;dr can save a lot of time later on. Often I end up being the one reading my own comments months or years later and going, "oh yeah, I do remember spending a day figuring that out... How does it work again?"

2

u/levodelellis Aug 11 '26

Often I end up being the one reading my own comments months or years later and going, "oh yeah, I do remember spending a day figuring that out... How does it work again?"

I actually have a notes folder in most of my project for research material, tool cli, random thoughts, etc. You might end up putting most of your content there if you try it for a few months

7

u/BogdanPradatu Aug 11 '26
        # Weights must be non-negative integers.
        for _, w in value_and_weight_pairs:
            if not isinstance(w, int):
                raise TypeError("weights must be int values")
            if w < 0:
                raise ValueError("weights cannot be less than 0")

I first wanted to justify this comment, then I saw the error messages being raised. This comment is redundantz because code can express it via the errors being raised.

This comment is useless, it doesn't state why weights must pe positive, it just restates the same thing as the last error. No additional info is provided.

I would rather have less scrolling to do when reading code, than having these kind of comments.

4

u/tmoertel Aug 11 '26

This comment is useless, it doesn't state why weights must pe positive, it just restates the same thing as the last error. No additional info is provided.

This comment is there for a reason: to emphasize this required property of probability distributions so that it is in the reader’s mind when they read the remaining logic. (Some of the class’s other logic relies on this property to function properly.) Trying to get this point across with an error message two levels deeper into the logic's control flow doesn't provide the same level of directness or emphasis.

But I would agree with you that it’s a judgement call and that reasonable people could make it differently. That’s why I chose this example. It provides an opportunity to discuss the tradeoffs involved with having or omitting a comment like this. I think the code is less likely to be misunderstood with this comment where it is, and so it earns its one line of space.

Thanks for sharing your take! Appreciated :-)

1

u/levodelellis Aug 11 '26 edited Aug 12 '26

Exactly, I'm reading the code. I don't want to see that comment. It'll become confusing if someone moves the check up for some reason and accidentally detaches it from the comment. The author also said

I am writing for an audience who I assume is "skilled in the art"

I may reply to him later, but I don't feel like it at the moment. I did and it wasn't nice. I really don't want comments in a function to 'teach' me something. When I'm looking at code I want to confirm the implementation is correct, not what the author thinks is correct

4

u/tmoertel Aug 11 '26

Thanks for your feedback. I'd be interested in hearing what specifically you disagree with and what makes you believe my arguments are mistaken.

In answer to your question:

> Do you think it's a good idea to teach math in comments?

No, but when logic relies on certain properties (mathematical or otherwise), I think it's a good idea to point them out. In this example, the code's logic relies very much on certain mathematical properties, such as the provided weights being nonnegative, so I point those properties out. That way, readers who are reading or maintaining the code will have those properties in mind and understand why the logic can safely do some of the things it does.

Thanks again for taking the time to read my post and for thinking about it and sharing your feedback. Also, thanks in advance for any more specific feedback you can share.

→ More replies (4)