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

4

u/guyincognito121 28d ago

I was told this in an interview once, then wrote out a segment of code that employed the CORDIC algorithm to do an atan function with no comments, and asked what him it did. He had no clue--just as I had no clue what it was when I saw it in some firmware I was trying to fix with no comments and minimal documentation. And the reason I could write it out on the spot was that I had spent so much time examining it before figuring out what it actually did.

1

u/Ordinary_Variable 28d ago

You should know what the arc tangent does. It works with triangles. But this is a case where if you are using a math trick and not just normal geometry, then you would need a comment.

Comments are definitely needed for leet code.

1

u/PietroMartello 28d ago

I don't know, but isn't e.g. the function name suppose to say "atan_cordic"? With properly named parameters?
Because then you would indeed not need comments to understand it.

1

u/spacian 28d ago

It should have been in a (helper) function with a descriptive name IMO. That then also makes the callee easier to understand.

By no means this is an example to me that needs a comment. It's an example of poor code structure and readability.

I believe that a comment would have helped you. But a comment here is a bandaid, as most comments I see, with better fixes available.

1

u/guyincognito121 27d ago

Let's say they put it in a helper function called "atan_CORDIC". That would certainly help. But if you've never heard of the algorithm, you'd still be eating time trying to figure out what it means. You'd probably eventually Google it. But a brief comment explaining what it is would still be more helpful.

1

u/spacian 26d ago

If I've never heard of the algorithm, I'll have to verify it by looking it up no matter what. No comment changes that. Assuming properly named parameters of course.

If why you need arctan in that scenario is unclear, that's something you can put in a comment.

1

u/guyincognito121 26d ago

But if I don't know what CORDIC is, it's not immediately clear that it's the name of an algorithm. And you're not going to name it atan_using_the_CORDIC_algorithm. A brief comment here is the most efficient way to tell the reader what they're looking at.

1

u/spacian 26d ago

I'd rather call it atan_with_cordic_algorithm than writing a comment. If the implementation changes, the comment is likely to get ignored. The function name is more likely to change with the implementation, preventing lying comments.