Clean Code Function : if statement

Remember that code is really the language in which we express our way of thinking

Any software consists of code, we can create languages that are closer to business needs, we can create tools that help us analyze and translate these requirements, but there will always be code, and more code is clean more the software is maintainable and scalable.

But how should be a clean code? specially a code clean for the “if statement “ function because it’s the aim of this article .

Example

In the specifications, your client requests to create a new web service that can upload files whose extension are .json and .xml and which does not exceed 10 mega bytes. without talking about controller processing, first create a function that verifies these specefications. may be you will proceed as to create a function like the example bellow :

Example 1

    public function verifyFile($file){
        if(isset($file)){
            $size      = $file['size']*1000000;
            $extension =strtolower(end(explode('.',$file['name'])));
            if(in_array($extension , array('json','xml'))){
                if($size <= 10000000){
                   return true;
                }else{
                      return false;
                }
            }
            else{
               return false;
            }
        }
        else{
              return false;
        }
    }

Example 2

    public function verifyFIle($file){
        if(isset($file)){
            $size      = $file['size']*1000000;
            $extension =strtolower(end(explode('.',$file['name'])));
            if(in_array($extension , array('json','xml')) && $size <= 10000000 ) {
                return true; 
            }
            else{
                return false;
            }
        }
        else{
           return false;
        }
    }

May be there is another way to do , but let’s see how can i create this function with a clean code basics .

Rules

To do this , it’s recommended to :

  • Deal with the checks first.
  • Return the expected result at the end.
  • don’t nest the appropriate value

Refactoring

    public function verifyFile($file){

        // managing error
        if(!isset($file)){
            return false;
        }
        $size      = $file['size']*1000000;
        $extension =strtolower(end(explode('.',$file['name'])));

        if(!in_array($extension , array('json','xml')) || !$size <= 10000000 ) {
           return false;
        }
        // the appropriate return
        return true;
    }

Explication

don’t nest the appropriate value  : if you nest the expected value , may be will check another if statement that can return the expected value and here you will see a duplicated code. that why it’s nice to Return the expected result at the end of the method . i mean , since you create the method prototype , go before the close bracket of your method and write the line contains the expected value depend or your method params or may by other thing like calling another method that make same modification of the gven parameters etc … , untill now , you need to Deal with the checks first of the method .

Framework Code Example

here is a code from command class of symfony using the clean code “if statement” :

/**
     * Sets the aliases for the command.
     *
     * @param string[] $aliases An array of aliases for the command
     *
     * @return $this
     *
     * @throws InvalidArgumentException When an alias is invalid
     */
    public function setAliases($aliases)
    {
        if (!is_array($aliases) && !$aliases instanceof \Traversable) {
            throw new InvalidArgumentException('$aliases must be an array or an instance of \Traversable');
        }

        foreach ($aliases as $alias) {
            $this->validateName($alias);
        }

        $this->aliases = $aliases;

        return $this;
    }

Don’t forget that the aim of this article is the ” if statement ” , because there another aspect to verify based by the clean code principale on the methods above .

inferom
web developer passionate about new technologies