r/ProgrammerHumor • • Sep 15 '26

Meme iDontCommentMyCodeIfYouDontUnderstandItThatsOkayNeitherDoI

Post image
832 Upvotes

188 comments sorted by

View all comments

65

u/anoppinionatedbunny Sep 15 '26

what a terrible way to misinterpret "good code should be self-commenting"

-26

u/rm-minus-r Sep 15 '26

Not commenting code is the most braindead take ever (for those that believe code can be good enough to not need any comments).

Comments are an abstraction layer on top of code that allow more information than code does by itself.

43

u/Wonderful-Habit-139 Sep 15 '26

No it’s not. Comments can drift and become obsolete, and sometimes just bloat the code and add unnecessary information.

And yes code can be good enough to not need any comments most of the time. Only very complex sections of code would require it. Use proper types and function names.

14

u/EspaaValorum Sep 15 '26

Code tells me what it does, how it does it.

Comments tell me why, provide context.

3

u/Just_Information334 Sep 15 '26

Yup. If you only ever touch new code you may not understand why it is necessary.

Go on some 10+ yo project which got a lot of scope changes over the years. Now you can see that yes, the code does something. But why? Is it still useful? Can you remove it? If you do, will some client come complaining 6 months from now when their very specific report is not generated because some flag is not set correctly so some cron running on a server no one knows about anymore did not find its data?

The documentation? Maybe in some .doc file rotting on an archive server. But if you can't find it you may have more luck finding the printed version which should be in room 6. If it was not binned during the last office move.

On the other hand, let's check postgres code by opening a random file: https://github.com/postgres/postgres/blob/master/src/backend/partitioning/partprune.c

If you praise tailwind for the locality of behavior it gives, then you should also want why-comments next to the related code.

2

u/EspaaValorum Sep 16 '26

That random code - lovely, like a senior dev explaining stuff to a junior dev. Imagine those comments not being there, and needing to onboard a new dev. The existing devs would need to spend a ton of time explaining things. I've seen that happen on poorly or not documented code bases, making it very difficult to expand the team with new devs because it takes away time from the already strained senior dev.

1

u/Oddly_Energy Sep 21 '26

> Maybe in some .doc file rotting on an archive server.

in the bottom of a locked filing cabinet stuck in a disused lavatory with a sign on the door saying 'Beware of the Leopard.'

1

u/ResponsibleWin1765 Sep 15 '26

How do comments drift and become obsolete?

Your entire comment reads like you're thinking of

// This code prints Hello World
int main() { std::cout << "Hello World" << std::endl; }

Obviously that doesn't need a comment. But that's not what most code is and it's also not what the conversation is about.

You do mention that very complex sections of code require comments so I don't really get why you're disagreeing with the person above. They didn't say that you need to comment every single line of code, just that the idea that you don't ever need to comment is braindead.

And then you throw in the "Use proper names" like a boomer telling young people to stop buying coffee if they want to become rich. It just shows again that you assume that others write terrible code and that's the reason they want to use comments.

4

u/developer-mike Sep 15 '26

I believe their point is that:

  • comments can be a code smell
  • names are essentially compiler checked comments

Suppose you have some highly stateful poorly written API that has tons of requirements about how it must be used. In this case you hopefully have lots of comments explaining those nuances in the API itself (documentation and implementation). Those requirements lead to special usage patterns by consumers, which require comments. The requirements likely leak from the consumer and then that requires comments and the cycle continues.

The comments are good and improve the code....but they're also a signal that the API is poorly designed.

Sometimes improving the API is as simple as using better names, like addResult/mergeLastTwoResults explains the requirements much better than add/merge. I'd (often) rather type extra long method names everywhere than write comments everywhere.

And then some code is just truly complex and just requires comments. But this is actually quite rare.

1

u/WormsDelicious286 Sep 15 '26

Comments can drift and become obsolete

If you can change function and parameter names when they no longer represent what they do/are, you can change comments too.  Otherwise it's just laziness.  Using bInitialized to hold a list of enums because the scope creeped up from one true/false flag to dozens is a code smell, and so is an outdated comment.

Anyway, people that write commentless code will soon be jobless, or forced to quit when the AI-based PR review adds comments for them.  They won't be missed.

2

u/developer-mike Sep 15 '26

Comments should usually not describe what the surrounding code does. Comments like "add delta to result" or "call cleanup" are useless. Comments should describe what other code does, e.g., "updateDistance expects a new total distance, not just the new delta" / "bar requires explicit cleanup."

Renaming a parameter is a pretty localized change. Comments about non-local behavior will drift.

-5

u/rm-minus-r Sep 15 '26

Comments can drift and become obsolete, and sometimes just bloat the code and add unnecessary information.

And... Code... Doesn't?

And yes code can be good enough to not need any comments most of the time. Only very complex sections of code would require it. Use proper types and function names.

There are those that have worked on a legacy codebase, and those that haven't. You appear to be among the latter.

5

u/Wonderful-Habit-139 Sep 15 '26

What do you mean code doesn’t? That’s a new one lol. I’m not even trying to be mean.

I’ve had to rewrite codebases before, you can give arguments without accusing the other person of not understanding anything. And yes I ended up deleting 95% of the comments along the process and just use types, well named functions, docstrings, etc. And that code is written idiomatically, and is properly checked by a type system that yells at you if you make changes, instead of letting the old code be obsolete and saying one thing while doing another thing (that’s what happens with comments, not code).

-4

u/rm-minus-r Sep 15 '26

What do you mean code doesn’t? That’s a new one lol. I’m not even trying to be mean.

You're saying comments can drift and become obsolete. Which is no different than code, which can also drift and become obsolete. So why fuss at comments when you wouldn't fuss at code for the same thing?

In my experience - one human being though I may be - people who say code can be written well enough to not need comments tend to be people who are writing things from scratch, or in brand new projects that don't have any legacy cruft to deal with.

I've done this a few times in my career and it was glorious. As it started out, everything was indeed super obvious, and comments would seem superfluous. But ten years later, when the codebase is 1,000x larger and is a product that's bringing in $1.8 billion a year?

When we first wrote it, there were six of us. Two years later when I moved to another company, there were 30 devs. In the eight years after I left, I think there's somewhere around ~2,600 people that work with it on a daily basis and contribute code, and several companies were acquired and their products merged into it. If I had to come back to it today, I don't think much of the code would be terribly clear.

I'm now working on a system that was written over the course of 20 years. I'm sure it was clear and obvious when it started, but now it's very much not. There's load bearing portions of the code that can't even be touched, and the "we'll refactor it all in 12 months!" rallying cry from leadership people from our director on up? Well, we're 18 months in and we've maybe refactored 20% of it. And that was the quick and easy parts.

It just gives you perspective.

These days? I'll still put a 3 to 5 sentence comment in a 80 line utility module that does exactly one thing. It's less for me, and more for the people that will come after me.

2

u/Wonderful-Habit-139 Sep 15 '26

> Which is no different than code, which can also drift and become obsolete. So why fuss at comments when you wouldn't fuss at code for the same thing?

Thanks for clarifying. I don't believe it's the same thing, because when code is obsolete, that means it's not being used anymore. In that case, thanks to having a compiler or type checker, it's very easy to see functions that are not being used anymore, as well as being able to delete lines of code that are not used anymore inside functions because of contracts that you've defined through types. I do this actively all the time, and there are semantics that shield me from the code not working anymore that is not the same with useless comments.

I understand your examples about legacy code, but writing good code that scales regardless of how big the codebase gets is the point. When you write abstractions and modules and separate things that have different concerns, with each abstraction being of a higher and higher level of abstraction, it doesn't matter how big the codebase gets. You always strive to write idiomatic code, you make sure to write good docstrings for APIs that you expose, even internally, and focus on making your APIs as obvious and easy to use as possible without requiring the users of those APIs to understand the inner code. The only people that need to understand it are the ones that are actively working on it.

> I'm sure it was clear and obvious when it started, but now it's very much not

Listen, I agree with this, but this simply means that it was "clear and obvious" because the code was small enough, not because it was well written. I'm saying code can be well written so that it scales well into the future, regardless of how big it gets over the years. But obviously most people don't write good code so you will make the observation that you're making.

But just because most people don't write good code, doesn't mean that good code is impossible and that writing comments is the best thing ever.

→ More replies (5)

2

u/protayne Sep 15 '26

Why on earth would you introduce an abstraction layer if you don't need to, abstraction layers should be avoided unless they provide value, not all comments provide value.

2

u/rm-minus-r Sep 15 '26

Why on earth would you introduce an abstraction layer if you don't need to

Because it is needed.

not all comments provide value.

Sure. Just because some don't, does that mean we should throw the baby out with the bathwater, so to speak?

2

u/protayne Sep 15 '26

I'm not against comments, I'm against comments that don't add value.

If the code is convulated or doesn't make obvious sense, then sure, but that's it, code + tests should be enough to describe behaviour of method calls.

2

u/mesonofgib Sep 15 '26

Code comments are a smell; it is often a symptom of poor design that a code needs comments at all.

Occasionally they really are warranted, but they should be an explanation of why, not what. Use them sparingly, because they can drift from the code over time and we all know that incorrect documentation is worse than no documentation.

1

u/draculadarcula Sep 15 '26

I have never seen a code comment that wasn’t completely redundant and obvious

1

u/rm-minus-r Sep 15 '26

Out of curiosity, how many years have you been doing this professionally?

1

u/draculadarcula Sep 16 '26 edited Sep 16 '26

10, “never” was hyperbole but I do think they are almost entirely and typically useless

It’s literally always

// refreshes the cache
RefreshCache();

or nowadays with AI

// added to refresh stale data. Reported in issue 1234. Per instructions NO redundant comments.
// The implementation is a follow up to PR 14567. Verified no legacy code. Part of spec 24 in ~/Documents/specs… (10 more lines of nonsense)
RefreshCache();

All garbage

1

u/rm-minus-r Sep 16 '26

It sounds like you have been deprived of good comments ☹️

1

u/draculadarcula Sep 16 '26

The only worthwhile comments are doc style comments at function and class definitions because they provide editor help and are usually type checked by your editor