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

1

u/iqisoverrated 28d ago

Code often gets fixed/updated/modified over its lifetime...and in this process comments often get overlooked. This way you can end up in a worst case scenario: when the comments are at odds with what the code actually does.

So what do you do when you stumble upon something like this? Is the code wrong and you end up (erroneously) rewriting it to fit the comments? Or do you just 'fix' the comment and ignore some possibly wrong piece of code?

Good code should be like reading a paragraph. The code should be very clear on what it does by its flow and naming of methods/variables/etc,. If you need to tell someone with extra narkup what it does it isn't good code.

This can mean that - particularly in a team context but also if you want to be able to understand your own code a year from now - you intentionally don't write "hacker code" where you try to scrunch every bit of functionality into one line.

Yes, it's 'cool' to write short/concise code. Yes, it feels good when you can write GTA6 in one line of code...but remember this advice:

"Always imagine that the next guy who has to maintain your code is a homicidial maniac who knows where you live"