Manuel Fernández
Manuel Fernández

Reputation: 483

Nested if statements, any possible way of cleaning?

I have checked a few other questions but they don't really give me the answer I expect..

My code is a like this..

private function handle()
{
    if()
    {
        if(!condition)
        {
            if(!condition)
            {
                if(!condition)
                {
                    if(!condition))
                    {
                        if(!condition)
                        {
                            if(!condition)
                            {
                                if(!condition)
                                {
                                    if(!condition)
                                    {
                                        if(!condition)
                                        {
                                            code
                                        }

                                        return;
                                    }

                                    return;
                                }

                                return;
                            }

                            return;
                        }

                        return;
                    }

                    return;
                }

                return;
            }


            return;
        }

        return;
    }
}

In my opinion it is readable but messy, sadly I haven't found really a way of making it look 'pretty'. Any ideas?

EDIT: Each return is different. EDIT2: Gave an answer of my own, thanks everybody!

Upvotes: 2

Views: 532

Answers (4)

Rajat Singhal
Rajat Singhal

Reputation: 11264

Conditions can be merged by a && operator..It works form left to right, which means, as soon as the any one starting from left fails, it stops evaluating the condition..

if($a) {

    if($b) {
    }

}

can be replaced by

if($a && $b) {
}

Upvotes: 3

Manuel Fernández
Manuel Fernández

Reputation: 483

I thought it out and have found a nice way of doing it, basically I'll make a method for each basic condition, and I'll call them in an if statement with the bitwise AND operator (&), which don't short-circuit.

/**
 * nonsql_condition - It means it doesn't check with the database
 *
 * sql_condition - It means it does check with the database.
 */
if(!$this->nonsql_condition() & !$this->nonsql_condition() & !$this->nonsql_condition() & !$this->nonsql_condition())
{
    if(!$this->sql_condition())
    {
       return error;
    }

    if(!$this->sql_condition())
    {
       return error;
    }

    code;
}

This allows me to use fewer lines of code in my method, plus also not doing unnecessary queries to my database.

Upvotes: 0

geekman
geekman

Reputation: 2244

Like already posted use

if(condition1&&condition2){}

or if this will not work, you can also use function which stops as soon as a condition is true

function some(){
if(!conditon 1){return 0;}
if(condition 2) {return 1;}
}

this provides more power as second if works only if first doesn't satisfy. You must choose based on your requirements. Sometimes though nested loops are unavoidable.

Upvotes: 0

David
David

Reputation: 2065

Use a variable check, or combine the conditions into fewer IF statements.

Variable check like so:

$execute = TRUE;
// Opposite of what you want, e.g. if you want $a only to be TRUE, do $a !== TRUE
if (condition) {
  $execute = FALSE;
}

...
// If all the conditions were met, then everything is OK
if($execute === TRUE) {
  // code
}else {
  // return
}

Edit: Variable check can be preferably to combining IF statements if you want more control on what returns, e.g. something specific happens if a certain condition fails, which combining conditions can not always allow for.

Upvotes: 0

Related Questions