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
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Oddly enough, out of all of these the one the compiler has the best chance of optimizing out is the last one
Not if Random writes to global state, that's a side effect that must be preserved
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.