this post was submitted on 13 Dec 2025
644 points (97.6% liked)

Programmer Humor

27913 readers
1845 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] Atlas_@lemmy.world 9 points 4 days ago (3 children)

Oddly enough, out of all of these the one the compiler has the best chance of optimizing out is the last one

[–] zea_64@lemmy.blahaj.zone 4 points 4 days ago

Not if Random writes to global state, that's a side effect that must be preserved

[–] LeFantome@programming.dev 2 points 4 days ago

What?

First one is optimized obvious.

Second one optimizes to x = 10 via constant propagation.

Third one first unrolls the loop, propagates constants including booleans, and then eliminates dead code to arrive at x = 10.

The last one cannot be optimized as “new” created objects that get used, nextInt() changes the state of those objects, and the global state of the random number system is impacted.