This comment was made on a question i asked about optimisation. “Note that cheap() && expensive() is not an optimisation of expensive () && cheap() in a language with short-circuit evaluation unless you can guarantee that both expensive() and cheap() are side effect free”
what does this mean?
Due to short circuit evaluation, when the expression
expensive () && cheap()runs,cheap()will only run ifexpensive()returns true. In the case where both methods are side effect free, which means they are just returning a boolean and not making any changes application state, then the expressions can be reversed tocheap() && expensive(), which will be faster assumingcheap()is not always true.However, in the case where either method modifies the state of the application then the reversed expression may not be functionally equivalent.