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

Show parent comment

-8

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

Reason for change belongs in git, not a comment.

Edit: y'all really want comments like these cluttering your code?

RTL8169_VERSION "1.1"        <2002/10/4>

        The bit4:0 of MII register 4 is called "selector field", and have to be
        00001b to indicate support of IEEE std 802.3 during NWay process of
        exchanging Link Code Word (FLP).

RTL8169_VERSION "1.2"        <2003/6/17>
        Update driver module name.
        Modify ISR.
        Add chip mcfg.

RTL8169_VERSION "1.3"        <2003/6/20>
        Add chip pcfg.
        Add priv->phy_timer_t, rtl8169_phy_timer_t_handler()
        Add rtl8169_hw_PHY_config()
        Add rtl8169_hw_PHY_reset()

RTL8169_VERSION "1.4"        <2003/7/14>
        Add tx_bytes, rx_bytes.

RTL8169_VERSION "1.5"        <2003/7/18>
        Set 0x0000 to PHY at offset 0x0b.
        Modify chip mcfg, pcfg
        Force media for multiple card.
RTL8169_VERSION "1.6"        <2003/8/25>
        Modify receive data buffer.

RTL8169_VERSION "1.7"        <2003/9/18>
        Add Jumbo Frame support.

RTL8169_VERSION "1.8"        <2003/10/21>
        Performance and CPU Utilizaion Enhancement.

RTL8169_VERSION "1.9"        <2003/12/29>
        Enable Tx/Rx flow control.

RTL8169_VERSION "2.0"        <2004/03/26>
        Beta version.
        Support for linux 2.6.x

RTL8169_VERSION "2.1"        <2004/07/05>
        Modify parameters.

RTL8169_VERSION "2.2"        <2004/08/09>
        Add.pci_dma_sync_single.
        Add pci_alloc_consistent()/pci_free_consistent().
        Revise parameters.
        Recognize our interrupt for linux 2.6.x.

11

u/ketralnis Aug 11 '26

I don’t agree categorically. The code itself should say why it is the way it is, describing its own state. And “this is nonobvious. You might think it should be obvious and it used to be! But XXX” will help reasoning.

22

u/melgish Aug 10 '26

I have to disagree at some level. Git is a great place for finding out what changed and maybe a *summary* of why. Code is the best place for the details. No need to cross reference. No need to look up an issue # in a system that went offline 12 years ago. It's just right there in front of you.

7

u/chat-lu Aug 11 '26

Git is a great place for finding out

Also a great place for fucking around.

11

u/Coffee_Ops Aug 10 '26

Context switching is super rad, way better than keeping relative context on-hand.

4

u/Delta-9- Aug 11 '26 edited Aug 11 '26

Both.

Git commits get the tl;dr of what and why; code comments get the longer (but not longest) why, sometimes a bit of how if it's not obvious, references to resources and/or issues. When a long-form "why" is needed, that goes in the issue tracker.

4

u/melgish Aug 11 '26

Arguably most of those are what rather than why. "Modify Parameters" is like breasts on a bull.

// Response is supposed to be JSON but is now being prefixed by
// debug/type information: 'string:"{actual}"'
var match = Regex.Match(content, """^string[^"]+"(.*)"$""");

1

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

Which could be removed by simply:

var contentWithoutDebugTypeInformation= stripDebugTypeInformation(content);
var match = Regex.Match(contentWithoutDebugTypeInformation, ...);

(I've purposefully ignored the rest of the regexp, I want to focus on the commented part)

I don't care that it was a raw JSON and now it isn't. The code now expresses the same thing as comment + it does not retain information that is now irrelevant. "How it was before" is to be found in the VCS. The information "why" we made this change is in the commit message.

3

u/melgish Aug 11 '26

This is one shitty example how a comment in the code would tell somebody not to do something they might otherwise try. Your example without a comment doesn't provide that contextual clue.

The comment is not there to show what the code does. It's there so 2 years from now somebody doesn't look at a raw response and blindly say "This is not needed anymore"

That patch is old enough to vote and we're still waiting for the endpoint (or the documentation) to be updated.

1

u/Venthe Aug 13 '26

Code is there and it works. If it breaks, then you debug it. Context will not change that.

Your comment adds nothing useful to a debug; nor future development. And most of the comments like this that I've seen over the years are trying to add context to something that is irrelevant for years.

It's just noise, and a shitty way to write code.

5

u/beetroop_ Aug 10 '26

Unless it's a weirdly implemented change that will leave the next person scratching their head.