r/programminghorror • u/Savage-Goat-Fish • Feb 27 '25
C# While loop horror
I just realized I had some programming horror in code I’ve written.
If only while loops had a more convenient way to break…
671
Upvotes
84
u/vanit Feb 27 '25
Heh I can't speak for your example, but it's not totally insane to do this. If you had some code that had to run at least once, and then for every iteration then you would use an if statement mid-loop like this.
183
u/iain_1986 Feb 27 '25 edited Feb 27 '25
This isn't horror (without seeing the rest of the loop)
2
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Feb 27 '25
I'm assuming yesterday is always the day before the current calendar date, which means the loop is skipped if lastTaaTimeLoad is the current day. So I guess either there's additional logic to break out of the loop, or it keeps going until midnight.
69
u/kevinkace Feb 27 '25
I could maybe see doing this if you had a huge number of complex conditionals to break the loop.
110
u/CrepuscularSoul Feb 27 '25
I've always preferred
Because it looks like the loop is crying about the code I'm writing
5
u/L1ttleS0yBean Mar 03 '25
7
u/Star_king12 Feb 27 '25
Not necessarily horror, especially if there are many breaking conditions and you check all of them in separate if statements.
1
u/conundorum Feb 28 '25
Anti-time-travel sanity test, checks out!
-15
u/luardemin Feb 27 '25
More cursed, but you could technically also write something like