DEV Community

Paul Edward
Paul Edward

Posted on

Good Looking IF Statement

Good Looking IF Statement

Nested IF

IF statement are very big part of development, the essence of the write up is to avoid deep nested if statements, although there are no fixed rules for every situation.

There are couples of advise i am going to give you and when you apply this you will find out your code looks a lot cleaner , Just to start with, Let's look at a basic example

*<?php



class *user
{
    *public function Location*(){
        *if*($this->longitude && $this->latitude){
            return $this->longitude . ' '.$this->latitude;
        }

        *return null*;
    }

}
Enter fullscreen mode Exit fullscreen mode

now we want to check if they have the Longitude, Latitude or either then concatenate the longitude, a space and the latitude, to avoid us repeating codes in our application…

Generally what i found when i develop is always return what you are expecting last, do the checks you need first and then dont nest the return of the value you expect to see

what do i mean?

*<?php



class *user
{
   *public function *Location(){
        *if*(!$this->longitude || !$this->latitude){
            *return null*;
        }
        //Other checks can come here
        *return *$this->longitude . ' '.$this->latitude;
    }
}
Enter fullscreen mode Exit fullscreen mode

The way i look at this is perhaps, we are doing other checks, if we are to do it the first way and we have to return another checks that means we may have to duplicate codes, so i add the expected return value last and i can do as many checks as i want returning NULL, throwing Exceptions or whatever you need it to do. I hope you get the idea?

Now Let's look at a slightly more complex examples and this involve using Nesting IF and also using IF ELSE, see what i meant below

*<?php

if *() {
    *if*(){
        *if *(){

        }
    }
} *else *{

}
Enter fullscreen mode Exit fullscreen mode

if you look at the above, this could be much pain to bear when you trying to debug or trying to go back to change things and tend to get really messy too quickly, i have been there in the pass and i guess you too.

Now let's look at an example

*<?php*

*if*(*ISSET*($_POST['file'])) {
    *if*(*in_array*($fileExtension, $allowExtention)) {
        *//Upload Files
    *} *else *{
     //Another Set Of Errors

    }
} *else *{
    *//display errrors
    return*;
}
Enter fullscreen mode Exit fullscreen mode

now am going to tell you, you can do all of the thing you have done above without using else and without nesting.. now lets tidy this up

Firstly Think About Things That Are Negative

*<?php*

*if*(!*isset*($_POST['file'])){
    *return*;
    *//Error here
*}
*//At This Point You are breaking the flow of the application here and you might be redirecting the user here

if*(*in_array*($fileExtension, $allowExtention)){
    *return *;
    *//Checking for Faulty Value
*}

*//Upload The file, and write other codes*
Enter fullscreen mode Exit fullscreen mode

If you think about it this way, you will end up with a much leaner and cleaner code. Easy to read, Easy to Maintain

Lastly, Improving code readability is an art, not a science. Sometimes it’s best to avoid negations in if statements, and sometimes it’s best to handle the most common case first and deal with errors later. But it’s always best to avoid nesting logic too deeply.

Top comments (0)