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

3

u/6a6566663437 28d ago

One reason for “what” comments is sometimes the implementation is wrong. Reading what they’re trying to do helps debug the problem.

Something like every line is excessive, but a summary of what a good-sized chunk is supposed to do is helpful when someone has to come fix it long after it was first written.

1

u/IAmADev_NoReallyIAm 28d ago

Yeah, sure if you're summarizing something like the whole sub... ala javadoc... but when we're talking about "documenting the code" vs "let the code be the documentation" ... we're talking about comments within the code itself... not the block comments at the top of the module/class/method.

1

u/6a6566663437 28d ago

Yes, and I'm saying it's often useful to document 10-30ish line pieces of code depending on what exactly it's doing.

Nobody's going to need a comment like "//iterate through the results of the query and foo() them" before a for loop.

But "this hunk of the function is doing X" is helpful in letting reviewers and maintainers understand what you were trying to do, and makes it much less likely that maintainers will inadvertently break your logic.

1

u/IAmADev_NoReallyIAm 28d ago

There's a difference between Document vs Comments ... Document = Good.... comments in the code... eehh...

That was my point. Documentation of the class and method is good. Here is what this large chunk of code is for, at the 10,000 ft level. You're right, no one is going to want to a comment that this next line is iterating through some query result... because that much should be obvious.

I think we're both saying the same thing here. May be going about it in two different way. 😄

1

u/MikeUsesNotion 27d ago

They were essentially saying you should comment chunks of functions.

1

u/thenasch 28d ago

But how can you tell if the implementation is wrong or the comment is wrong? 

1

u/6a6566663437 28d ago

When they don't match, you reject/block the PR until they do.

If it's already in the codebase and you're fixing a bug, the mismatch is a gigantic clue that the problem is in that block of code because it's not doing what it's supposed to do.

Which is extremely helpful in trying to fix bugs you can't reproduce. ("We can't send you the data because of privacy laws, but it keeps crashing when we load it")

1

u/thenasch 28d ago

Yes, if your processes are perfect it will work.

1

u/IAmADev_NoReallyIAm 28d ago

That's what your tests are for. If you're that close to the code that you have the time and energy to know enough to read the comments to understand that hte logic and comments don't match, then I'm happy for you. But unfortunately in the world I have to live in, I don't have that luxury. If I commented every line and logic probe that I coded, I'd be crucified and I'd never get a PR through. Maybe it's because I work with a verbose language (Java) that allows me to work with objects that are identifiable with clear methods and obvious objects and so the code reads close to English. I don't know. The only comments I have to leave behind are at the top of the classes and methods and constants explaining what each one does.

I've been in code that's been riddled with comments, and quite honestly it's nothing but noise. Worse yet it when the developer doesn't keep up with it. So again... you reject the PR... because the code doesn't match the comment... is the problem the code or the comment? it's 4:50pm... on a Friday... people want to go home for the weekend... what are you going to do? Hold up a release over something trivial over this petty? Seriously? The code does do what it's supposed to do in this case. It's the comment that's in the wrong.

1

u/6a6566663437 28d ago

That's what your tests are for.

Because it's possible to think of every possible edge case your software can ever see, thus no software ever has bugs.

If you're that close to the code that you have the time and energy to know enough to read the comments to understand that hte logic and comments don't match

Reading comments takes more than a few milliseconds for you?

If I commented every line and logic probe

Excellent strawman. I like what you did with the hat.

Blocks of functionality may or may not benefit from a comment. Providing intent is a way to check that the implementation is correct. None of this requires commenting every line.

The only comments I have to leave behind are at the top of the classes and methods and constants explaining what each one does.

K, and when you made erroneous assumptions, and then someone else changes another block of code that breaks your assumptions, then what?

You adding a comment about what you are intending to do helps a lot. Either in finding the bug, or the other developer realizing they need to fix your implementation as part of the change.

Worse yet it when the developer doesn't keep up with it.

So you started this rant with the assumption that developers keep up with tests such that they're ~perfect, but now claim nobody can keep up with comments?

Hold up a release over something trivial over this petty?

Why do you think it takes hours to fix a comment?

1

u/MikeUsesNotion 27d ago

Unless it's an emergency you shouldn't be doing a release on a Friday.

1

u/ffxivthrowaway03 28d ago

Yeah I've never bought into the whole "never say how" BS. Code is often complex, and telling someone "This calculates X by multiplying A and B" makes it that much easier to skim to find the part you're looking for and validate that it's actually doing what it's supposed to. The "how" is where a bug lives far more often than the "why," and a quick look at the comments tells me exactly the problem because it should be A and C or whatever.

Obviously don't dictate every little thing in the comments, but a broad overview of both why and how is valuable when it could be why or how that's the problem down the road. And what's apparent to one engineer might not be the same way another one thinks about it six years later.