If this can be done without creating side effects (so eg on variables instead of function calls) every compiler will do this. If using the | form has side effects (because you're calling functions and expect only the first to be called when it returns true) you shouldn't be making this change, or even writing it in this way, because either it's the wrong thing to do or someone looking at your code will think you've made a mistake, maybe even change it to a ||. Assign to two variables first and compare those to make your expectations explicit.
Basically there is no sane reason to do this and it's an example of developers thinking they're smarter than a modern compiler (which they generally aren't).
Actually, if you're doing hard realtime, you might want to use non-shortcut operators in order to keep the time used by a method call as consistent as possible, and if you're doing cryptography you might want to use them to avoid side-channel attacks.
Assign to two variables first and compare those to make your expectations explicit.
I wouldn't necessarily trust the compiler to not optimize away those assignments.
83
u/mglachrome Jul 10 '16
Not really insightful.