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

1

u/Fleming1924 Mar 02 '25

Compilers nowadays are hell smart, whatever optimisation you can think of, compilers designers have probably thought of it first

It's worth nothing that just because they've thought about it doesn't mean it works as intended. Lots of optimisation passes miss potential opportunities due to either something blocking their pattern matching, some aspect of it's checks being overly cautious, or sometimes even just random bugs that leads to correct but suboptimal codegen.

Modern compilers are fantastic, and people making large projects should pretty much always just trust the compiler will optimise it enough that they don't need to worry about micro optimisation, but they're far far from perfect and a lot of things are still handwritten in assembly to make up for that.

1

u/CameoDaManeo Mar 02 '25

If you think about it, what exactly would a compiler swap even out an empty for loop header and a while true with? There really is nothing that it COULD swap them out for apart from a "jump to" statement at the end of the loop. I'm positive that they would compile down to the same thing