dimanche 23 février 2020

Multiple If statements versus single statement with conditions evaluated using logical operators

I am writing a function that checks several conditions before actually executing its task. This is done by a number of if statements. Like so:

bool foo()
{
    if(invalid())
        return false;
    if(dont_execute())
        return false;
    // .. etc
    // Actual execution here
    return true;
}

In this function is there any benefits by changing the multiple conditions to :

bool foo()
{
    if(invalid() || dont_execute() /* || .. etc */)
        return false;
    // Actual execution here
    return true;
}

I feel that the first style is more readable. What I want to know is, if there is any performance impact in using multiple if statements rather than combining using logical operators.

Aucun commentaire:

Enregistrer un commentaire