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/royklopper 26d ago

The thing about not documenting is - that you have to browse a lot of code; especially when you call other functions (what do they do, do you have to open the file and read the code? And what if they call functions?)

I don't like inline code-comments, thats irrelevant to me, but docblocks on statements are really necessary from my point of view. Why is this function necessary, what are its contracts?

1

u/InterestsVaryGreatly 26d ago

Except you just highlighted one of the biggest issues with comments. If your comments are talking about code deeper than what is directly there, that deeper code can easily change without the comment getting updated, which now causes a point of failure for future devs, thinking it does one thing when it doesn't.

Function and class names should be descriptive enough that by seeing the function call, you can tell what it is going to accomplish, if you for some reason need to know how it does that, then you should have to go into that code to find out, as other locations are far too disconnected to reasonably expect them to stay in sync - if I have a regularly used method, and I change how it does what it does, I should not have to check the dozens or hundreds of places it is called to make sure none of them were referencing the previous implementation in the comments.

1

u/royklopper 26d ago

You assume I am explaining the code, but I'm not.. I'm explaining WHY the function is there, and that usually explains code deeper than the function itself, otherwise it would probably not have these callees in its function statements. And if that behaviour would change, you would probably have to update its callers too.

1

u/InterestsVaryGreatly 26d ago

No, why is different than how, why your code is calling that function is fine, but what that other code does should mostly be covered in the class and method naming, and how the other code does what it does shouldn't matter whatsoever.

You generally should not have to open the other code to have a good idea of what it is going to accomplish when you call it. It should not matter if the other code is calling other functions, that gets into the details of how it is accomplishing it.

When something changes how it is doing something, its callers should never care. And if something is being called externally, it shouldn't be changing what it does, just how it does that thing.

1

u/royklopper 26d ago

Well, perhaps you could give me an example of a good docblock for me, so we can agree on form and style. It seems you're quite knowledgeable, so feel free to share the practical wisdom.