I have code similar to this one and itβs a little difficult for me to read:
if( (expensiveOperation1() && otherOperation() && foo())
|| (expensiveOperation2() && bar() && baz()) {
}
I simply changed it to the following to make it more readable:
const bool expr1 = expensiveOperation1() && otherOperation() && foo();
const bool expr2 = expensiveOperation2() && bar() && baz();
if(expr1 || expr2){
}
But should I worry about efficiency now?
I mean that in code1, if the first conjunctive sentence is fulfilled, it will not even look at the second, because it is already clear that the statement will be true.
But in my more readable example, it is necessary to calculate both cond1, and cond2. Or will the compiler be smart enough to change mine code2to code1if expr2 is no longer in use?
Frank