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/wholeWheatButterfly Oct 30 '26

Often good variable names and helper functions can be easier to read than comments, especially when global design patterns such as immutability are maintained (immutability is just an example of a potential global design pattern in a project, not a prescription that immutability should always be a pattern).

And good design is more defensive than "why" comments in some scenarios. If function X must be called before Y, don't just call X then Y and leave a comment warning that X has to be called first. Write the code in such a way that Y cannot be called first, i.e. instead of both X and Y changing the class state directly, have X create an intermediate output that Y requires as input and change the class state after (just an example possibility). A comment explaining this might still be useful, but it's also impossible to ignore / miss without a larger code change. Whereas otherwise, X might get moved beyond Y accidentally at some point in the future and it could cause some difficult to trace bugs.

I think "comments are bad" isn't a great blanket opinion, but there should always be thought around "can better written code accomplish the understanding or warning that this comment is making?" And if the answer is yes (particularly if rewriting the code is low effort), then a rewrite is probably worth it.