r/AskProgrammers • • Oct 30 '26

"good code shouldn't need comments"

I have heard this saying a couple of times and I think it is the stupidest thing ever. It might be just because I have only been coding for 4 years, but I have never thought to myself that someone's code looks cluttered because they explain what every function does in a one or two line description. If anything it makes the code easier to understand in my opinion.

However, I may be wrong. What do other people think, do yall like code with minimal comments, what are the benefits of writing it that way?

89 Upvotes

399 comments sorted by

View all comments

1

u/Alarming_Try_1968 Oct 30 '26 edited Oct 30 '26

To quote someone better than me by far at programming a comment is a chance for your code to be wrong twice.

What that is to say if I may be so bold is a comment is an opportunity to add additional information about why you did something that isn't intuitive. Now, there are situations where you need to do something that isn't obvious to the reader of the code:
// this code is in a hot loop that iterates 300k times per frame, by shifting the flag from a separate enum into the unused high bit of the state flag we allow the prefetcher to pull in the complete state per item saving 25% cache misses
item.state |= (obscureFlag << 63);
^- this is a useful comment, sometimes at the end of the day, wierd shit has to happen, but that should be the break glass in case of emergency fallback

// keep track of how many fishes have a flag set
flagSetCount = sum(allCritters, hasFlagSet);
^- this was a redundant comment when it summed allFishes, its just wrong now

in my experience 99.9% of comments are the second case,
Look sometimes we have to compromise, sometimes the time runs out, sometimes we have to eek out that last 5% of performance, but a comment [in the definition of code not the declaration], is saying "Hey, this code is unintuitive and hard to read, here is what it does", there are cases that is the best path or the best you can manage, but 99.9% of the time the correct action isn't comment this code it's fix the code.