r/AskProgrammers • • 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?

91 Upvotes

399 comments sorted by

View all comments

1

u/guywithknife Oct 30 '26 edited Oct 30 '26

It’s bullshit.

“Self documenting code” also doesn’t actually exist. So called self documenting code rarely does. It’s what people who have never had to maintain code over a long period of time call their bad slop code because they’re too lazy to document it.

It doesn’t matter how well your functions and variables are named, unless your functions are trivial, they’re not going to explain the intent behind the code.

Comments should say why, not how. Sometimes what, rarely how. The code already tells you how. 

That doesn’t mean you need 50 lines of comments for every 2 lines of code. A lot of code doesn’t need comments, but when you do need to explain why some code exists or does it the way it does, don’t be shy about using comments.

2

u/TheLurkingMenace Oct 31 '26

Exactly. I don't write comments so I know WHAT the function does, I write them so I know WHY the function exists.

1

u/BabblingTower Oct 30 '26

Mmm no, self documenting code is an art. It also shouldn't be devoid of comments, just kept minimal

1

u/guywithknife Oct 30 '26

I’ve had coworkers ask to remove comments because “the code should be self documenting. Instead you end up withStupidlyLongFunctionNamesAsPoorSubstitutesForComments. For some reason they were ok with docstrings, though, so I got around their stupid rule.

I've been programming for a long time and it’s incredibly rare that code is actually self documenting, unless it’s trivial. You can kind of make it work by making all your functions tiny and giving them detailed names, but then you lose clarity because you lose locality (having to jump through lots of functions to read the flow makes it harder to read), when a quick comment would be much more readable.

But I agree, comments should be used tastefully when they aid clarity, not “just because”.

1

u/BabblingTower Oct 30 '26

Like I said, it's an art

1

u/guywithknife Oct 30 '26

That’s not very useful of most people can’t do it. Especially with how prevalent the sentiment that you shouldn’t comment and should write self documenting code instead is.

1

u/BabblingTower Oct 30 '26

The people I work with do fine. Maybe it's your company?

1

u/guywithknife Oct 30 '26

I don’t work there anymore. It was just an extreme example though, I’ve seen enough trash “self documenting code” online too, though.

1

u/BabblingTower Oct 30 '26

Ok. I've probably been working longer than you, and I disagree with your assessment.

1

u/guywithknife Oct 31 '26

I’ve been programming since 1999

1

u/BabblingTower Oct 31 '26 edited Oct 31 '26

94 for me

1

u/oplukana Oct 31 '26

it sounds like you have found a coworker who was rather not good at making their code clear, and also really liked not spending effort to make it clear, and just harmed your understanding and perspective of that phrase.

1

u/guywithknife Oct 31 '26

No, I’ve seen plenty of people claiming they practice self documenting code, and even articles saying so. They’re not very good. They assume you already know what the code is doing and then claim “see, it’s perfectly understandable”. That’s fine in the moment, but useless five or ten years later.

A function name dan also go out of date if not maintained, so similarly, maintain your comments. Have keeping comments up to date be part of code review (nowadays you can ask an AI reviewer to catch stale comments). At least a comment can use proper punctuation, unlike function names. Docstrings are fine too, they’re just comments in another form.

And document why (maybe what) not how. Or have comments link to issues in your issue tracker, or design documents, or ADR’s.

You mentioned ADR’s, but if they’re not called out in a comment and linked, you’re not going to know about them while reading unfamiliar code. Proximity matters. Something that’s right there in the code you’re reading is low effort to read, something that’s in a different file, perhaps in a different directory or even repo or tool, that’s high effort to find and read.

1

u/oplukana Oct 31 '26

i dont care if the function name is long, id rather read that then have to find the comment which is going to be longer than the function name anyway. and if the function name is too long, then it should be broken into smaller functions with less scope and smaller names which are composed together to serve the higher purpose.

1

u/Relevant-Register-69 Oct 30 '26

For the most part, you really should be aiming for a composition of many trivial functions. It's why many languages have at least two visibility scopes.

1

u/guywithknife Oct 30 '26

It depends.

Reading code linearly is generally easier than having to weave in and out of functions.

Some code can naturally be split into nameable things and in those cases, yes, absolutely do split them. But often you have an algorithm where splitting it doesn’t buy you much other than to give you a nameable block of code that now lives elsewhere.

Most of the time you should err on the side of splitting, though.

1

u/St33lbutcher Oct 30 '26

If your functions are named well you will be reading more linearly without needing to jump into every function

1

u/oplukana Oct 31 '26

you should learn about and use ADRs and keep them in a separate place from the code. its much better than putting the comments in the code.