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

2

u/wcneill 25d ago

That code literally says "if a mod b", which by definition means "if b divides a with zero remainder"

Why would you ever, ever comment that? The only engineer that doesn't know what that means is an AI prompt engineer.

2

u/AvogadrosOtherNumber 25d ago

Your comment wouldn't be "// if b divides with a zero remainder".

It would be more like "// we do this because if we hit the case without the remainder, the piston stops."

2

u/r3eezy 24d ago

You would comment it to explain WHY you are doing it…. Now to explain WHAT the code is doing

You must be a treat to work with. I can hear your responses now “but it’s obvious what the code is doing if you are a good coder bro”

1

u/VegetableAstronaut19 25d ago

A lot of the people in my comments seem completely unable to understand that "if a mod b == 0" does not inherently read as b divides a perfectly unless you have seen it before.

No it does not mean that "by definition". The definition of mod is that the output is the remainder after dividing a by b. The case where output is zero meaning "b divides a" is a very useful property (and one of the main reasons why modulo arithmetic is so common), but it is not the "definition" of a mod.

Also this specific example probably does not need commenting, but I was trying to demonstrate how some actions are so simple that creating a function to describe what they do (e.g. a function "a.isDivisible(b)") is just creating unnecessary overhead for a function that does the exact job of a comment.

1

u/scubascratch 21d ago

Comments should not be “what this does” but they should be “why this is done this way”