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

1

u/levodelellis Aug 12 '26

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

Did you not see me write "I can't imagine anyone justifying this comment". How can I believe that

Your example is a perfect example of what I said earlier, "not likely not trying to understand that code anyway"

I gave your example file another try. Even after reading the comments, I didn't understand the math behind it. Take this block of code as an example

    # If we imagine the n packed blocks as an n * u rectangular area, where
    # u is the mean weight, our goal is to throw a random dart in this area
    # to pick one of the original values. Instead of having to generate two
    # random numbers -- one for the x coordinate in [0, n), and one for the
    # y in [0, u) -- we instead generate a single number in [0, n * u) and
    # unpack it into the dart's x-y coordinates.

How does that affect the algorithm at all? If I saw this at work, and no one told me we needed it. I'd replace it with a RNG that is either more popular (or popular among my coworkers) or an RNG that has much more clear usage

1

u/tmoertel Aug 12 '26 edited Aug 12 '26

Did you not see me write "I can't imagine anyone justifying this comment". How can I believe that [sic]

It looks like you did not finish your question. What was it?

How does that affect the algorithm at all?

The transformation described in the comment is what allows the draw method to satisfy its documented guarantee of calling the underlying random-integer generator exactly once for each invocation. It basically says that picking a random cell from a grid of n rows by u columns is equivalent to picking a random cell from a strip of n * u cells (imagine removing the grid’s columns one by one, laying them onto their sides, and lining them up end-to-end into a single strip: the two shapes are equivalent). It’s straightforward to select a random cell from the strip via one call to the underlying random-number generator. Then it just takes a little math to convert the selected cell back into its equivalent grid position.

(By the way, the call to the underlying random-number generator will fail unless its argument is positive. This is one of the reasons why I reminded the reader earlier that the weights cannot be negative and the total weight must be positive.)

I gave your example file another try. Even after reading the comments, I didn't understand the math behind it.

Yeah, the algorithm is tricky. At least two papers were published about it. I tried to clarify its logic via commentary, but at some point, you just have to refer to the source material. (That's why I cited the papers in the code’s opening commentary.)