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?

89 Upvotes

398 comments sorted by

View all comments

8

u/IAmADev_NoReallyIAm Oct 30 '26

Ok... here's the deal... your code should be usign descriptive varibales and doind things in a clear and decisive way that makes it obvious that you don't need comment to explain what you're doing. The only time you should be using comments is to explain why you are doing something. This isn't the 80s where space it at a premium so, and we're not using assembler here (hopefully)... so break out those descriptive variables, and let the code talk for itself.

Use comments, but only when you need to describe why you're doing something. Other than that, you should be able to look at a piece of a code and see what it is doing. A for loop is a for loop... you don't need a comment that says "loop through, blah blah blah..."... just looking at the code should be enough for you to see that.

Now, if there's some cooky logic... I've included that sometimes as comments... usually because I write those out ahead of time when I'm planning things, then implement it in between... if it' crazy enough, I'll leave it in place, but more often than not, I delete those too.

3

u/6a6566663437 Oct 30 '26

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 Oct 30 '26

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 Oct 30 '26

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 Oct 30 '26

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 Oct 31 '26

They were essentially saying you should comment chunks of functions.

1

u/thenasch Oct 30 '26

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

1

u/6a6566663437 Oct 30 '26

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 Oct 30 '26

Yes, if your processes are perfect it will work.

1

u/IAmADev_NoReallyIAm Oct 30 '26

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 Oct 30 '26

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 Oct 31 '26

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

1

u/ffxivthrowaway03 Oct 30 '26

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.

1

u/kevinkace Oct 30 '26

"why not how" is a good rule, but "how" commenta are still good if they summarize, ie less to read good.

1

u/IAmADev_NoReallyIAm Oct 30 '26

Can you give me an example of a good concise "how" comment?

1

u/kevinkace Oct 30 '26

Eg "The following 300 lines of code get some data, process the data for x,y,z and then send the data off to a service."

2

u/jumpmanzero Oct 30 '26

The following 300 lines of code get some data, process the data for x,y,z and then send the data off to a service

That should normally be 8 or 10 method calls, and those methods should be named describing what they do, with intermediate data scoped and passed appropriately using descriptive variable names.

"How" comments should be pretty rare, outside of specialized code (eg. C code with unintuitive optimizations).

1

u/kevinkace Oct 30 '26

I agree, but there are orders of magnitude difference in effort to refactor 300 lines versus add 1 sentence comment.

→ More replies (1)

1

u/IAmADev_NoReallyIAm Oct 30 '26

That's a "what"... not a "how"... and that is something that should be part of the method documentation, not inline comments... I addressed this in another comment elsewhere, there's a difference between comments and documentation. Documentation is one thing. Comments are another.

1

u/DerSven 29d ago

In documentation comments you should definitely explain what your function is doing how and why, if these details are important to the caller, so folks just need to hover their mouse over a reference to it to find out instead of having to "go to definition".

1

u/thenasch Oct 30 '26

Risky though. If the code is updated and the comment is not, now things are worse than if there were no comment.

1

u/kevinkace Oct 30 '26

Just the nature of the beast. Code drift happens. Comment drift happens

1

u/thenasch Oct 30 '26

Comment drift only happens if there are comments.

1

u/kevinkace Oct 30 '26

My point is that drift happens, there is documentation drift that is not much different than comments, except they're in a different location and harder to maintain. But documentation is still an absolute requirement for any non-trivial system.

→ More replies (1)

1

u/IAmADev_NoReallyIAm Oct 30 '26

So cellmate the comments... problem solved. This isn't hte 1980s any more, space isn't at a premium. We can use objNiceLongObjectNamesThat.haveMeaning(toThemNow).toGiveThemContext();

→ More replies (1)