Search code examples
c++if-statementshort-circuitingside-effects

if statement - short circuit evaluation vs readability


Sometimes, an if statement can be rather complicated or long, so for the sake of readability it is better to extract complicated calls before the if.

e.g. this:

if (SomeComplicatedFunctionCall() || OtherComplicatedFunctionCall())
{
    // do stuff
}

into this

bool b1 = SomeComplicatedFunctionCall();
bool b2 = OtherComplicatedFunctionCall();

if (b1 || b2)
{
    //do stuff
}

(provided example is not that bad, it's just for illustration... imagine other calls with multiple arguments, etc.)

But with this extraction I lost the short circuit evaluation (SCE).

  1. Do I really lose SCE every time? Is there some scenario where the compiler is allowed to "optimize it" and still provide SCE?
  2. Are there ways of keeping the improved readability of the second snippet without losing SCE?

Solution

  • One natural solution would look like this:

    bool b1 = SomeCondition();
    bool b2 = b1 || SomeOtherCondition();
    bool b3 = b2 || SomeThirdCondition();
    // any other condition
    bool bn = bn_1 || SomeFinalCondition();
    
    if (bn)
    {
      // do stuff
    }
    

    This has the benefits of being easy to understand, being applicable to all cases and having short circuit behaviour.


    This was my initial solution: A good pattern in method calls and for-loop bodies is the following:

    if (!SomeComplicatedFunctionCall())
       return; // or continue
    
    if (!SomeOtherComplicatedFunctionCall())
       return; // or continue
    
    // do stuff
    

    One gets the same nice performance benefits of shortcircuit evaluation, but the code looks more readable.