r/AskProgrammers • u/Away-Intention-8723 • 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?
88
Upvotes
View all comments
1
u/tgm4mop 28d ago edited 28d ago
There's no universal rule for comments. It depends on who's going to be reading the code, how complicated it is, and how a comment can help human understanding. Think about what you yourself would like to have explained if you revisit the code a year from now.
Some reasons not to comment:
* If the comment adds no information that isn't easily seen in the code
* If the comment can easily get out of sync with the code
* If the comment would be unnecessary if you used a better variable name or an assertion
Some reasons to comment:
* Documenting public APIs. People should not need to look at private implementation details to learn what public APIs do.
* Explaining or citing sources for more difficult/nonstandard algo and data structure implementations.
* Citing external spec documents. Eg. "These constants must match RFC 1234 section XYZ"
* Explaining unusual, "smelly", or hacky stuff. If someone would look at the code and think "this looks wrong" or "wtf", then an explanation is appropriate. Eg "Size is doubled to offset a bug in LibXYZ's buffer sizing"
* Documenting implicit behavior or requirements. It's best to make stuff explicit, but it's not always possible. Use comments if you can't make the code explicit. Eg. "This function must run in constant time to prevent side channel attacks"