4 ms·
I understand your "invalid" point. Your example dereferences a potentially null pointer. But I don't understand your "inefficient" point. At the end of the day
by cbarrick 4y ago
I understand your "invalid" point. Your example dereferences a potentially null pointer.
But I don't understand your "inefficient" point. At the end of the day, everything gets turned into SSA. So expression-per-line and nested-expressions generates the same code, right?
- maccard 4y agoIt's the same idea, that the && short circuits. bool dirty = ...; bool success = doExpensiveCalculation(...); vs if (dirty && doExpensiveCalculation) {
- saghm 4y agoNot necessarily; in first case, the stuff after the `&&` will definitively not be run. In the second case, it depends on how the code is optimized, and there's no guarantee that things moved to intermediate variables won't still be evaluated even if they're never used. Since the compiler presumably can't know the value of the boolean at runtime, it would need to reorder things to avoid doing the unnecessary work, and I wouldn't think it's something that you could just assume.