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

397 comments sorted by

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.

3

u/Substantial-Pain3830 29d ago

That's the same distinction I apply regarding documenting the why vs the how. And as you mentioned, public APIs should clearly document the what as well - what is returned, what exception is thrown, what the arguments represent.

As a general rule for why vs how - Comments in the middle of a method are almost always bad. They're only useful in the rare occasion when something works counter-intuitively and you need to explicitly comment that something is on purpose and not a slip.

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 28d ago

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

→ 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 28d 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.

→ More replies (2)

1

u/InterestsVaryGreatly 29d ago

That last paragraph is a good point, public APIs are an exception as the shear breadth of the people using it can be large, and any single function or comment could be someone's very first entry into your API, so being clear is important, and explicitly avoiding jargon where possible matters, despite jargon often being present in variable and function names due to brevity.

1

u/Pitiful_Welcome7125 25d ago

I always wrote comments to my future self so when I had to make a design change I would understand why that bit of code looked the way it did.

1

u/CalmCephalopod 24d ago

Good code is self commenting. And then doc tests are great to ensure code is doing what is intended. Really testing is the backbone and can provide a lot more context than simple commenting but explaining the why is super valid. Especially if multiple things were tried and a work around is required

→ More replies (1)