this post was submitted on 13 Dec 2025
277 points (97.9% liked)

Programmer Humor

40880 readers
28 users here now

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

Rules:

founded 6 years ago
MODERATORS
top 25 comments
sorted by: hot top controversial new old
[–] hddsx@lemmy.ca 31 points 1 month ago (2 children)
[–] Axolotl_cpp@feddit.it 9 points 1 month ago

Musk forgot a thing: programmers can learn how to make code longer than it should be

[–] Witchfire@lemmy.world 4 points 1 month ago

...or my manager

[–] ferric_carcinization@lemmy.ml 18 points 1 month ago* (last edited 1 month ago) (3 children)

The compiler should be able to optimize all of them to the same machine code.

  1. This is already good.
  2. Easily optimized by constant folding.
  3. ~~This one depends on the semantics of signed underflow, so it may not do what you want.~~
  4. ~~The loop can only exit if x==10, so as long as the nextInt() method doesn't have side effects, the loop should be eliminated. But, again, language semantics can affect this.~~

Edit: Very wrong for 3 & 4, see replies.

[–] ryannathans@aussie.zone 17 points 1 month ago (2 children)

I'd be shocked if 4 got optimised out

[–] Mio@feddit.nu 10 points 1 month ago (1 children)
  1. It is used to warm up the house.
[–] MangoCats@feddit.it 4 points 1 month ago

4 is used for non-deterministic delay - - - is Random.nextInt() also cryptographically secure?

[–] ferric_carcinization@lemmy.ml 2 points 1 month ago (1 children)

I feel like an idiot. Also, in the "Good" example, no underflow occurs. i goes from 0 to -10, and x is assigned to -i every loop.

It might still be possible to optimize away the random number example, if the random function were made a magic language item, but it would not be even remotely close to being worth the effort.

[–] MangoCats@feddit.it 2 points 1 month ago (2 children)

The question the optimizer can't really answer is: will Random.nextInt() ever return 10? If that's a 64 bit integer it could be a LOOOOOONG time before 10 ever shows up.

[–] ryannathans@aussie.zone 1 points 1 month ago

Ah yes the halting problem

[–] ferric_carcinization@lemmy.ml 1 points 1 month ago

If it were a magic lang item, you could treat the resulting value in a special way. Then, you could create an optimization pass for this situation: if a variable is assigned random in a loop and the loop can only be exited with a certain value, the compiler can coerce the magic rand value to it.

[–] MimicJar@lemmy.world 5 points 1 month ago (1 children)

For #4 if the Random instance weren't "new", then calling the nextInt() function would definitely have side effects, since the next integer would pull one away from the random stack.

However unlike the first three which will run within a consistent amount of time, #4 will take an unknown amount of time to run, so you can't just collapse it and eliminate the loop.

For example a very simple race game where a participant moves a random number of steps each turn, we may want to time how long that race takes. We can't just say that they will reach the end immediately. In fact technically we don't know that they will ever finish the race... But that's the halting problem and a whole other issue.

[–] ferric_carcinization@lemmy.ml 3 points 1 month ago

I don't know what I was thinking.

But, if you borrow C's semantics, you are allowed to "optimize" away side-effect-less loops, even if they would never terminate. But that would require the random method to be pure.

[–] pineapple@lemmy.ml 2 points 1 month ago (1 children)

Still a mess to read though, anyone revewing the code will be like wtf?

[–] ferric_carcinization@lemmy.ml 1 points 1 month ago

You could do a lot worse. If the type of i was an object, you could overload the negation operation to have side-effects for the third snippet, for example.

[–] billwashere@lemmy.world 8 points 1 month ago

And now this will be in someone’s vibe coded project…

[–] davel@lemmy.ml 8 points 1 month ago
[–] Tolookah@discuss.tchncs.de 7 points 1 month ago (1 children)

No

i= 0;

do{ i++; } while (10 != *(U32 *) i );

?

Gotta make it hard to read as well

[–] anton@lemmy.blahaj.zone 3 points 1 month ago (1 children)

Segmentation fault, you forgot to take the reference and derefenced a null pointer.

[–] Tolookah@discuss.tchncs.de 1 points 1 month ago

i is my address. The bigger problem is that I should have increased i by sizeof(U32)

(This works on bare metal, mostly, not on an os)

[–] sepi@piefed.social 6 points 1 month ago

Genius MIT Hacker

[–] mcv@lemmy.zip 5 points 1 month ago (1 children)

What the hell kind of magic number is this? Define that stuff in an environment variable, and write a singleton to make the value transparently available where you need it.

[–] anton@lemmy.blahaj.zone 1 points 1 month ago

Exactly, you never know when you want to change the base for printing out from under someone.

[–] Buffalox@lemmy.world 2 points 1 month ago

That actually made me laugh out loud. 👍 🤣

[–] sp3ctr4l@lemmy.dbzer0.com 2 points 1 month ago

gotta somehow also make it involve matrix transforms, maybe use quaternions, and randomly compare a random array of them, until they result they result in a 10 degree rotation in one realspace vector.