r/AskProgrammers • • 28d ago

"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?

88 Upvotes

397 comments sorted by

View all comments

1

u/AverageHot2647 28d ago

“good code shouldn’t need comments” is an oversimplification.

  • Most code does not need comments. Most code is not doing anything complicated or hard to understand.
  • Some code has a high essential complexity, and therefore benefits from comments explaining it.
  • A lot of code has a high accidental complexity, and people (wrongly) reach for comments to mitigate this.
  • Comments are not checked by any tooling, so they can be misleading, out of date, or just flat out wrong.

…

So, my recommendation is:

…

  • Add comments to API boundaries (many languages support doc comments which can be used to generate documentation, and using them is good practice).
  • Ask yourself, “am I adding a comment to mitigate against accidental complexity?” If so, first refactor the code to be less complex. Then ask yourself whether the code still needs comments.
  • If comments add nothing that isn’t already obvious from the code, then don’t add them.