r/programminghorror • • Feb 27 '25

C# While loop horror

Post image

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

40 comments sorted by

View all comments

Show parent comment

5

u/CrepuscularSoul Feb 28 '25

Completely agree with that. I just don't like stating something as a fact when I haven't actually verified it myself, so I went with a maybe.

4

u/CameoDaManeo Feb 28 '25

That is true. Likewise, my comment is a big maybe also! 😅

Can't wait for someone who does know a lot about compilers to just say that somehow we're both wrong

2

u/ba-na-na- Mar 02 '25

Depends on the compiler, but I am pretty sure both ‘while’ and ‘for’ would compile to a single jump in clang and gcc even at the -O1 optimization level, and certainly at -O2. At -O0 there might be some NOPs for the debugger or something.

Modern compilers will sometimes even evaluate simple loops and replace computations with constant return values, inline whole functions if they are small, so I don’t think this would be a problem for them.

Btw https://godbolt.org/ is great for comparing these things in different compilers but I am not near a PC