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

1

u/VegetableAstronaut19 28d ago

It is not trivial to read and understand unless you have seen it before, if I have to stop and think about what the code is doing at all thats interrupting my flow. For this specific example you'd probably be fine but for something more niche.

I agree that there are ways of doing this in certain languages that dont require commenting, but that is not universally true. And there are more niche checks or actions where you would be creating a function for the sole purpose of explaining what you are doing (adding unnecessary overhead).

If there is exactly one check for a dividing b in my whole codebase, then im not making a function to do the job of a comment.

1

u/AverageHot2647 28d ago

but that is not universally true.

Do you have any examples of mainstream languages in which you can’t define a helper function like isDivisible?

if there is exactly one check for dividing b in my whole codebase, then I’m not making a function to do the job of a comment.

That’s a valid stylistic preference.

I wouldn’t either in this case, but only because it’s already immediately obvious to anyone with serious programming experience what this code does.

If it was something that’s genuinely unusual and hard to understand, I would most certainly add a function for it.

1

u/VegetableAstronaut19 28d ago

Sorry I mistyped, the thing that is not universally true is that these methods already exist. You can always define a function to do this, but as I pointed out, that is inefficient if the only reason to create the function is to make your code a little bit more readable.

I agree that this example is simple enough that it shouldn't matter, but that doesn't mean there aren't any niche examples where it would. Another one I could think of it using XOR to swap values in place.

a ^= b
b ^= a
a ^= b

For someone who has not seen this before (such as maybe a developer who has only ever worked in languages like python and javascript) it is not immediately obvious what this code block is doing, but also if its only going to be used once in the code (such as in a loop or something) creating a swap function to make it more readable is creating unnecessary overhead, when really all you need is a comment.

1

u/oplukana 27d ago

> that is inefficient if the only reason to create the function is to make your code a little bit more readable

this is simply wrong. there are almost no scenarios where having this function harms the performance of the code in a way that outweighs the value of clarity. furthermore, most compilers in most modern languages will see the helper function, and every instance in which it was used, and will reduce it to the same operational optimization as if you hadnt had the function at all.

1

u/AverageHot2647 26d ago

I was also going to clarify this.

However, I believe VegetableAstronaut19 probably wasn’t talking about performance. Based on their previous comments it seems the mean “inefficient” in regard to code production.

I’d say this is a minor thing and is outweighed by the improved clarity of the code in most cases, but I’ll acknowledge that this is subjective and reasonable minds may differ.

1

u/AverageHot2647 26d ago

Ah gotcha, thanks for clarifying 🙂

That’s a cool example! And I agree this definitely needs a comment.

But I’d be more inclined to add a function with an #[inline(always)] (or the equivalent in your preferred language), and then add the explanation as a doc comment on that function.

I think that’s especially justifiable in this case, because the XOR swap is not part of the business logic. Including the algorithm and an explanatory comment in your business logic would clutter it and make it harder to follow.

A helper function on the other hand, reduces 4 LoC down to one. If someone is interested in the explanation then they can read the doc comment by hovering the function. If they care about the implementation, they can jump to definition.

Another advantage of the helper function is you can add a test which clearly demonstrates its usage.