this post was submitted on 02 Oct 2023
1532 points (98.4% liked)

Programmer Humor

32483 readers
613 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
top 50 comments
sorted by: hot top controversial new old
[–] datendefekt@lemmy.ml 83 points 1 year ago (12 children)

I had so many arguments with my team lead. He thought comments were an antipattern because that meant the code wasn't expressive enough.

I understood where he was coming from, but a little hint here and there why the fuck the code is doing what it's doing would've been nice.

[–] ebc@lemmy.ca 56 points 1 year ago (5 children)

Yeah, good code should explain the "what" without the need for comments. Good comments explain the "why".

[–] SolarMech@slrpnk.net 15 points 1 year ago (1 children)

Generally, you can replace some comments with variable names or comment names. Which means you must already be in the habbit of extracting methods, setting new variables to use appropriate names, and limit context to reduce the name (Smaller classes and methods means shorter names can be just as expressive, because the context is clearer). It lowers the number of wtfs per minute you get reading code before you even need whole sentences to explain why things are done in a certain way, because the names can be a powerful hint.

But realistically, you end up needing comments for some things anyways.

load more comments (1 replies)

Also some parts of code are just going to smell, because of deadlines, other trade offs. For those it helps to have a comment to really highlight that bit of weirdness - the what and the why. If it is weird it should really "pop out" when you're reading it.

load more comments (3 replies)
[–] Anticorp@lemmy.ml 46 points 1 year ago (4 children)

I used to work with a guy who insisted his code was self explanatory, and then he'd nest loops 5 levels deep and give variables names like "thingyOne".

[–] Donkter@lemmy.world 35 points 1 year ago (3 children)

"thingyOne" is self explanatory, it's a thingy.

[–] CanadaPlus@lemmy.sdf.org 10 points 1 year ago (3 children)

Namely, the first one. Next you're going to ask about thingySixtyNine or thingyOneHundredTwentyTwo, I suppose?

load more comments (3 replies)
load more comments (2 replies)
[–] fred@lemmy.ml 9 points 1 year ago (3 children)

That guy wasn't in charge, I hope? That would not have passed code review with me at least.

load more comments (3 replies)
load more comments (2 replies)
[–] punkwalrus@lemmy.world 23 points 1 year ago (5 children)

I had a boss who hated comments because he wanted "clean code," and that comments means you aren't using the wiki. The build approval process actually stripped out all comments via a script.

Then we lost the wiki.

[–] vector_zero@lemmy.world 20 points 1 year ago

The build approval process actually stripped out all comments via a script.

Thanks, Satan.

[–] ladicius@lemmy.world 11 points 1 year ago

I'd watch the movie.

load more comments (3 replies)
[–] CanadaPlus@lemmy.sdf.org 7 points 1 year ago

It kind of feels like engineers look for simple solutions to what are ultimately complicated psychological questions sometimes.

load more comments (8 replies)
[–] peopleproblems@lemmy.world 61 points 1 year ago (7 children)

I am working with an in-house "rapid development team." They have rigorous intake, story and task break down, scheduling of sprints, QA, definition of done, integration test coverage, E2E and min 90% unit test coverage etc. etc.

They have a strict policy of "no code comments, self documenting code only." They will go in and remove comments that my DevOps team puts in there, because it screws up that policy.

Luckily, we adopted the policy of having local branches with these comments in place. Once they move beyond the project, we're putting them in.

[–] TrustingZebra@lemmy.one 38 points 1 year ago (1 children)

The vitriol the "Clean Code" cult has against comments is unbelievable...

load more comments (1 replies)
[–] AA5B@lemmy.world 23 points 1 year ago* (last edited 1 year ago) (2 children)

They do have a point though. When we’ve gone through phases where the industry focuses on commenting code and other documentation, most of it is useless.

Code comments should have a reason, and are no substitute for readable code.

However you’re also right though that people jump onto “all or nothing”. I recently did a code Review that I held up for comments. In this case, they were doing something very atypical to get around a limitation so it was a clear case of comments needed. The next person through will say “what is this shit?” And “fix” it, without knowing there was a good reason

[–] buzziebee@lemmy.world 12 points 1 year ago* (last edited 1 year ago) (1 children)

Yeah "why" comments are absolutely fine, "what" comments are useless at best and can be harmful at worst.

I feel like this constant flood of "write comments" posts are from CS students who are told to comment everything by their lecturers. Descriptive variable and function names help explain the "what" of code pretty well most of the time.

Sure with some old languages like C89 where you are limited in your variable name length you probably do need comments to explain wtf is going on, but most code bases aren't as constrained.

[–] _stranger_@lemmy.world 9 points 1 year ago* (last edited 1 year ago) (1 children)

I have this argument with other devs all the time.

Hey, you know this 200 line nested if statement that suddenly returns in the middle of the condition is "7"? Yeah, that needs a refactor or at least a fucking comment.

I get a pull request that adds a log line that reads something like "special case 7 triggered.".

load more comments (1 replies)
[–] DrQuint@lemm.ee 7 points 1 year ago

Having no comments doesn't force code to be self-documenting. I understand the cult of code extremist, but No-Commenters are just WEAK and PATHETIC wannabes trying to immitate the true, functional extremists, such as the 16-Space-Tabbers

[–] hoodle@programming.dev 12 points 1 year ago

That's silly. I've never seen comments even as a way to explain what your code is doing (the code often does do that anyway). It's important to say WHY you're doing it the way you are.

That way anyone else who comes in there can have the same context you did when you implemented it, which imo is where most bugs occur. People don't realize what assumptions you made when it was first written, and then make their own (which can easily clash).

load more comments (4 replies)
[–] psycho_driver@lemmy.world 58 points 1 year ago (4 children)

My favorite part is looking at my ancient code and thinking "Holy hell I don't remember being that smart. What happened?"

[–] kevincox@lemmy.ml 31 points 1 year ago

Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?

Brian Kernighan - The Elements of Programming Style, 2nd edition, chapter 2

[–] rob64@startrek.website 11 points 1 year ago

A similar phenomenon is knowing you're going to need to go back and update some older section of code and when you finally get around to it, it turns out you wrote it that way to begin with. It's like... I didn't think I knew about this approach before...

[–] Aceticon@lemmy.world 8 points 1 year ago

"I'm dumber but wiser" - me, hopefully

load more comments (1 replies)
[–] gens@programming.dev 44 points 1 year ago (1 children)

I'd like all files to have a big comment on top that says wtf this is, why is it, and roughly how it works. Bonus for ascii art.

[–] Anonymousllama@lemmy.world 17 points 1 year ago (2 children)

My favorite is the 'here be dragons' ASCII art, that's how you know you're in for a fun time

load more comments (2 replies)

When the the documentation is epic, but no one remembers to add why this repo even exists.

[–] ryannathans@aussie.zone 22 points 1 year ago (1 children)

I think the images are inverted

load more comments (1 replies)
[–] Spzi@lemm.ee 19 points 1 year ago (1 children)

The obvious solution is to abandon your project not too late; leave on a high note.

I also found it very useful to document every step of my setup procedures, right after I figured out what works. At least the respective CL.

load more comments (1 replies)
[–] RandomVideos@programming.dev 16 points 1 year ago (4 children)

I have a special skill of writing terrible code, not seeing it for half a year, and then remembering exactly what the code did

load more comments (4 replies)
[–] reverendsteveii@lemm.ee 15 points 1 year ago* (last edited 1 year ago)

`/* Gets CustomerEntity from customer repository by customer ID or throws a customer not found exception */

public CustomerEntity getCustomerEntityById(String customerId){ customerRepository.findById(customerId).orElseThrow(new CustomerNotFoundException()) }`

[–] nosurprises@lemm.ee 13 points 1 year ago* (last edited 1 year ago) (11 children)

Code is the documentation. Any other form of documentation requires additional maintenance and becomes obsolete quickly.

[–] EvilMonkeySlayer@kbin.social 19 points 1 year ago (5 children)

"Code is the documentation"

Is the laziest excuse I've come across. Like, not everyone knows everything and complex code exists, it is not hard to put in some text explaining the basics of what something does.

Like, unless you're dealing with that code everyday it's going to look like double dutch to anyone new coming to it.

I've had people say that to me before, and it is absolutely infuriating in its laziness and cop out excuse.

load more comments (5 replies)
[–] Vlyn@lemmy.zip 12 points 1 year ago (5 children)

The code shows what happens. But comments should explain why it was done this way.

Sometimes the code started simple and readable, till you ran into a weird edge case a year ago. Now the code no longer looks as obvious and another developer might scratch their head when they read over it. A small comment can help out there quite a bit.

Or you're doing something stupid in code not because you want to, but because management forced you to. So you put a comment there that the code isn't wrong, management wanted that behavior.

load more comments (5 replies)
[–] saltesc@lemmy.world 9 points 1 year ago* (last edited 1 year ago)

Ah, I see you don't optimise enough.

It should be...

Past you "I've shed so many lines and boosted processing time. It's a masterpiece. I should document this for future me because it's complex as hell."

Future you "What. The. Hell..." five minutes later "Oh, I see. I'm a fucking narcissist trying to be god of something 10 people run once a week."

load more comments (8 replies)
[–] joneskind@beehaw.org 11 points 1 year ago

Of course we forget how it works.

The whole point of the documentation is to help remembering.

[–] xantoxis@lemmy.world 10 points 1 year ago

An awful lot of people in here talking about automatic comment removal and I ... don't believe it. I think a lot of you are making up stories. Show me proof that a build script removed your comments, because it's not just bad policy, it's literally insane. Nobody would do that. Anyone that thought of doing that would spot the folly of doing it within 5 minutes, and immediately stop. GTFO

[–] Auzy@beehaw.org 7 points 1 year ago* (last edited 1 year ago) (3 children)

Let copilot do it. Junior developers can now efficiently write useless comments like the rest of us

load more comments (3 replies)
load more comments
view more: next ›