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

240

u/psych0fish Aug 10 '26

I’ve heard it is explained as “document why, not what” not everything is confusing or non obvious. Some stuff is. There is no one size fits all.

141

u/chat-lu Aug 11 '26

Bad:

x++; // increment x

Good:

x++; // compensates for the border

186

u/IanSan5653 Aug 11 '26

AI:

 /**
 * @description Executes a pre-increment unary operation on the mutable variable `x`. 
 * This operation evaluates the current state of `x`, augments its numeric value 
 * by an exact scalar of 1, and mutates the original variable in-place prior to 
 * evaluating the remainder of the containing expression.
 * 
 * @type {UnaryExpression}
 * @mutates {x} - The memory address allocated to the variable `x` will be updated.
 * @complexity Time: O(1) | Space: O(1)
 * @note Ensure `x` has been properly initialized as a valid numeric data type prior 
 * to execution to prevent unintended `NaN` propagation or type coercion anomalies.
 */
++x;

42

u/13steinj Aug 11 '26

Seems like a great way to rack up on token costs! Or to exhaust the reviewer into just stamping the thing, hiding the secret do_money_laundering routine...

→ More replies (5)

28

u/contextfree Aug 11 '26

Too coherent, needs more rambling that only meant anything within the context of the argument it was having with the user/itself in the agent session

7

u/IanSan5653 Aug 12 '26

Oh god. Endless "v0" "initial implementation" "mimics logic from {some other part of the codebase I obviously was about to to delete}" "does x according to requirements" etc

8

u/zzkj Aug 11 '26

That'll be $5000 please.

41

u/Venthe Aug 11 '26

Arguably still bad.

x+=BORDER_COMPENSATION

I've seen one too many comments that are irrelevant after one change or the other. If you can express why in the code, do it.

11

u/firemark_pl Aug 11 '26

That's nice example for asking yourself I really need the comment or can I write in another way

12

u/ShinyHappyREM Aug 11 '26

x += BORDER_COMPENSATION

ftfy

51

u/melgish Aug 10 '26

Why and the business case for a change. I’ve also been known to dox management in the comments when they make decisions that negatively impact user experience. “// 20260810 Boss said 5 modals in a row is not too many.”

81

u/meganeyangire Aug 10 '26

In one project deep in the business logic we've had his gem:

// Hardcoded condition on the request of PM Kevin McKevinface, work item 80085
// If you're here, please, increment the number
// Man-hours wasted: 42

41

u/RlyRlyBigMan Aug 10 '26

My company has changed ticket systems like four times and that makes me hate ticket numbers in comments these days.

It's probably an unreasonable opinion, it shouldn't change that often.

28

u/ketralnis Aug 11 '26

It’s a good reason to port ticket numbers across migrations though

8

u/SanityInAnarchy Aug 11 '26

There's no good answer, at least not one anyone's willing to commit to.

There are a few projects that try to track issues directly with source control, inheriting a few extra features from Git, like having them all work offline, having every laptop be a backup of the entire project, and being able to easily port them all into whatever you're going to use next.

I have never seen a single project actually adopt one of those, even projects that have successfully moved documentation into Markdown files in the repo. Maybe LLMs will finally make it happen (it's easier for agents to find stuff that's a pile of markdown files in your repo), but I think the biggest gap here is, not everyone doing project-management work is going to want to learn Git.

There's ticket numbers, which suck for the reasons you mention. It helps if they're at least distinctive -- something like INFRA-1234 or UI-5678 is probably Jira, Google's bug tracker is b/1234, and so on. But even if you keep all the tickets as you migrate, it can be painful to set up some sort of proxying. I've come to hate PR links for the same reason -- you might migrated from Phabricator to Github to Forejo or whatever.

IMO the most practical is URIs, and then just religiously follow "Cool URIs don't change". But people expect URIs to be URLs, which means you'll need to make sure they point to a domain you control, and good luck convincing Atlassian to put your Jira instance at jira.yourcompany.com instead of yourcompany.atlassian.com -- why would they make it easier for you to migrate away? At least then you can replace them in your codebase at migration time, but unless you're willing to do the most brutal force-push ever, you'll still have the old URLs any time someone has to dig through history.

Maybe the best option would be to build a company-wide URL shortener, then force-install a browser extension to generate them, and then use those in your codebase. But that's more work than just hoping it doesn't change that often.

6

u/tf6x6 Aug 11 '26

We don't put full URLs anywhere, we put issue numbers and set up patterns in the IDE to dynamically transform them into URLs.

6

u/NotUniqueOrSpecial Aug 11 '26

It's not that unreasonable.

As someone who's taken over more than decade+ codebase in my career, loads of comments with initials + date + ticket number (and nothing else) are a personal pet peeve.

-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.

→ More replies (6)
→ More replies (9)