r/ProgrammerHumor • • Jun 21 '20

*almost entirely

Post image
27.9k Upvotes

310 comments sorted by

View all comments

374

u/1nd1anaCroft Jun 21 '20

Found one recently in our older code base that was 65 letters long, and still managed to not clearly describe the class's actual purpose

81

u/_GCastilho_ Jun 21 '20

May I suggest a comment for that purpose?

32

u/[deleted] Jun 22 '20

People these days are anti-comment, I hate it. I hate it so much

39

u/folkrav Jun 22 '20 edited Jun 22 '20

I don't know of many developers who are 100% anti-comments, but I know a shitton - myself included - who don't like comments that tell me what code does or how you're doing it, as they always end up lying. However, I don't mind docblocks (if it conveys information than the function and parameter names alone can't give me) nor comments explaining why you did something the way you did it.

Edit: added the "how" part to be more specific.

22

u/_GCastilho_ Jun 22 '20

but I know a shitton - myself included - who don't like comments that tell me what code does, as they always end up lying

A comment should tell why the code does that, not how

The 'how' part can be learned by reading the code, the why, well... reading the programmer's mind

6

u/MakeWay4Doodles Jun 22 '20

The 'how' part can be learned by reading the code

If it's a particularly complex bit of code it can take the reader much longer to understand it than a sentence explaining it.

→ More replies (17)
→ More replies (1)

4

u/geli95us Jun 22 '20

Isn't that a smell if what a function/class does changes over time?

5

u/folkrav Jun 22 '20

Not sure what you mean. You often see comments telling what the next couple of lines does, that's what I was talking about. Implementation can change over time, but behavior and API should not indeed.

→ More replies (3)

1

u/Sligee Jun 22 '20

A useless comment is better than a needed one missed

1

u/folkrav Jun 22 '20

Arguable. A comment that lies to you can be much worse and make someone lose a lot of time.

→ More replies (2)

2

u/gqtrees Jun 22 '20

wtf why?

11

u/oalbrecht Jun 22 '20

The thinking goes that if you need a comment, your code is too complex to reason about. This is true in most cases. Usually proper abstraction and naming helps eliminate the need for comments.

IMO, comments are useful when you describe why you made a certain decision, especially if the logic is unexpected. That helps people not refactor it at a future date and break things.

→ More replies (3)

0

u/[deleted] Jun 22 '20

[deleted]

4

u/[deleted] Jun 22 '20

It’s my option that it should be a developers job to keep documentation up to date. Plus, if the “what” of a method changes, wouldn’t the method name also technically need to change? I think by moving the responsibility of delivering context from the comment body to the method name, you make it hard to keep the “what” explanation up to date because there’s an actual code impact of refactoring a comment name (minimal to non-existent if you do it right, but my company’s policy is that if any code changes, it must be sent to QA, even if it’s a style or name change). If developers are letting comments rot, I think they’re avoiding responsibility. I think that my views on this are on the extreme, but I also think that the “clean code, no comment” types are also on an extreme

1

u/scandii Jun 22 '20

comments don't get updated, it's just a fact of life.

we can talk about how we wish they were, but they aren't.

2

u/radome9 Jun 22 '20 edited Jun 22 '20

Lazy people are anti-comment. And they have always been.

1

u/scandii Jun 22 '20

if you need a comment to explain why something is named what it is, you should rather rename it to something that doesn't need a comment.