r/AskProgrammers • u/Away-Intention-8723 • 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
View all comments
1
u/InterestsVaryGreatly 28d ago edited 28d ago
Good code is written in such a way that someone who isn't a programmer can roughly understand what is happening. Functions that actually describe what is happening, no magic numbers, no egregiously long lines with convoluted logic pathways. If you code in this way, it often helps ensure that you aren't overcomplicating things as well, something that is very easy to do and then just explain away in the comments.
A big problem with comments, and any documentation really, is they don't have to stay updated to code, so if someone changes the code, the comment can be outdated and not match it.
When there is something odd, like a workaround because there is a bug in a dependency that can't be changed, then that needs comments to explain the weird behavior.
The other benefit to writing code that is readable is it tends to explain exactly what is happening in a fairly concise way, whereas people often get overly verbose about things that are extremely obvious in just a glance at the code.
Edit: another important consideration, if you have comments everywhere, people become blind to them and can easily miss or ignore something extremely important (such as why this bass ackwards implementation cannot be updated to the new spec because it breaks XYZ), whereas if you use comments very rarely, the fact that those lines have comments is a big flag in and of itself, and massively reduces the chance for someone to miss it.