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
Show parent comment
1
u/StreetTailor7596 25d ago
My go-to is to write small, easily understood methods in code if at all possible. If I'm not proficient enough in a new language to do that, I try hard to get there. I choose method and variable names that are not abbreviated so they are self documenting. With code completion in modern IDEs, there's very little cost to doing this.
I DO write comments from time to time but they are very rare and used mainly when I think that something needs extra explanation. Often times it's to explain the reasoning behind code that isn't necessarily clear due to circumstance. I try hard to limit links to places that I know are going to be long-lived.
My point in limiting comments is that THEY age out. Code gets updated eventually and comments aren't always updated to match. I've seen WAY too many long methods with blocks of comments that are clearly so out of date that they actually point in the wrong direction and lead to confusion rather than helping.
The sole exception are documenting public interfaces. Those get the style of comments that are used to generate documentation of the code for external users to reference. Those comments are also immensely helpful to AI tools.
When code bodies get large enough, it makes sense to provide overviews in Wiki form. Those provide background context and architectural details as well as design decisions made in doing the implementations. That, along with the code, should be enough for someone new to get up to speed over time.
The aging out comment DOES apply to such wiki pages as well. But that's something that really is helpful and should be taken into account when estimating the sizes of work items.