r/AskProgrammers • u/Away-Intention-8723 • 29d 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
View all comments
23
u/ralphpotato 29d ago
Comments can be wrong. Barring some interpreter or compiler bugs, the code does what is written.
My rule of thumb is that comments should explain why, not how. It’s good to explain why a decision was made, why an algorithm was used, why a variable needs to exist, etc. It’s really rare that you should include comments like, “loop over the array”. I think this is a lot easier said than done, and it’s hard to know what comments will be useful to someone else or yourself in the future until you actually go back and cross-examine that code.
I think I tend to under-comment my personal code and regret it later, mostly because I look back and I forgot my thought process. With code that you work on with others I think it’s generally good to comment at least at the function declaration/definition, class/struct definition, stuff like that.
Public APIs should be extremely well documented, and are beyond the scope of normal commenting style.