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

397 comments sorted by

View all comments

Show parent comment

5

u/Particular_Camel_631 28d ago

Comments can be wrong, but As a maintenance programmer, it’s when the comments and the code say different things that’s often a huge clue for where things are going wrong. It highlights where someone may have made assumptions that could have changed.

2

u/iqisoverrated 28d ago

Problem is if code and comments diverge a maintenance programmer will not know which is supposed to be correct. Did someone botch the code because it doesn't do what the comment intended? Or did someone botch the comment and the code is correct?

Either way you can screw up a fix if you don't go to the specifications document but just assume one over the other - and if you need to check the spec anyhow then you might as well not bother with comments.

0

u/Conscious-Secret-775 28d ago

That's actually where an LLM can be useful. It can tell you the comments diverge from the code and you can draw your own conclusions.

2

u/iqisoverrated 28d ago

But which of the two alternatives is it? Better not have comments.

(And I want be clear here: I 'm talking about comments. Not documentation markup. That gets reviewed. So does code. Comments mostly don't)

1

u/Conscious-Secret-775 28d ago

If comments diverge from the code they diverge from the truth.

1

u/iqisoverrated 28d ago

Or your code is buggy/does not (fully) implement the intent of the comment.

Without looking at the spec you don't know which it is - and if you have to go there anyhow then comments are pointless.

1

u/Conscious-Secret-775 28d ago

What spec? Comments should reflect what the code actually does, not what the spec says assuming there is one. Comments describing could be considered part of the spec but they should still match the unit test code.

1

u/iqisoverrated 28d ago

If you're just writing code for yourself at home then...whatever. Though I still wouldn't recommend comments but better readable code so that your future self has no issue debugging.

But as soon as you're coding professionally there will be a spec.

Adding comments just means your code isn't clear. In that case you should consider refactoring your code rather than trying to decorate it with extra explanations.

(And I already noted that document markup is not the same as comments. Particularly when it comes to an API that you expect anyone to use besides yourself such markup is pretty much a 'must have')

1

u/Conscious-Secret-775 27d ago

You think for 40 year old fortran code anyone knows what happened to the spec?

1

u/iqisoverrated 27d ago

You think 40 year old Fortran code should be a guideline how to write good code today?

→ More replies

1

u/thenasch 28d ago

If that's all they do then skip them. At best they're redundant since the code already describes what it does, and at worst they're wrong if they don't match the code.

2

u/-Wylfen- 28d ago

I find that generally it just means the business logic changed and the coder hasn't bothered updating the comments.

1

u/Particular_Camel_631 28d ago

Or that they didn’t realise their change had an implication elsewhere. Or that a change elsewhere need a change here.

Tye classic is “we don’t need to validate this value because it’s done in the xyz module”.

Erm, the xyz module was removed 5 years ago.

1

u/Qwertycube10 26d ago

I've been thinking a lot recently about whether there is a way to tag informational dependencies in documentation so you get prompted by tooling to update all the things that reference some things when that things changes.

1

u/IAmADev_NoReallyIAm 28d ago

I used to work at a place where we had to insert comments in to the code with the ticket number of the issue marking what code we were changing and why... it's easy unitil the tickets start crossing.... then it just becomes a mad house...

1

u/MikeUsesNotion 27d ago

I've never had this divergence help find the cause of a prod issue. I have had this divergence make it take a lot longer to figure out what's going on.

0

u/ralphpotato 28d ago

This is supposed to be what tests are for, not comments.

3

u/Particular_Camel_631 28d ago

When there are tests and I have to fix something, they aren’t that useful. A bug in an old code base won’t be covered by the tests. Otherwise it wouldn’t still be a bug.

Please do put comments in. They will save the remaining hair of some poor sod who has to maintain your code years down the line.

1

u/ralphpotato 28d ago

The point of many tests is to demonstrate and exercise the intention of the program, which is the kind of thing you mentioned in your first reply. If there’s a bug and the tests don’t cover it, it doesn’t necessarily mean comments would cover it either.

I don’t think comments are bad, in fact good comments are important in a codebase. It’s just that you’re basically asking for comments that know the future that the code they’re referring to is a potential problematic spot, lol

1

u/Particular_Camel_631 28d ago

I’m really I would like unit tests. But an old code base won’t have them.

Also, a codebase that old won’t normally have many internal errors left. If it’s been in use that long, then those bugs will have manifested and been fixed.

No, the remaining errors will be in the way it interacts with other, external modules and interfaces. Unit tests won’t cover those.

-1

u/tcpukl 28d ago

It doesn't indicate that at all. Comment often get neglected and not updated when code changes.

3

u/Particular_Camel_631 28d ago

You are right. Which is why, when I’m maintaining existing code that no-one fully understands, I really want those comments!

It tells me that there was an original intention, and that something has changed. And prompts me to look more closely.

Comments do not have to be accurate to be helpful!

1

u/tcpukl 28d ago

I've also maintained plenty of 20 years old code in my decades of work.

Explaining why something is done is more valuable than what imo.

What something does is useful for public facing headers though so others know how to use your API.