Program using the minimum of possible ifs

-8

I'm programming object-oriented, I've been studying design patterns for some time, but I can not develop without using a massive rainfall of ifs.

How to do design patterns to avoid ifs ? I have read a lot of articles on the subject but with PHP I can not evolve in that sense.

For example, I'm doing here a CPF validator, standard IRS. Between ifs and elses are more 15 streams. It gets that gigantic and disorganized code, even breaking some commands into sub-functions in the class.

public function is_cpf_strict() {

        // Remoção dos caracteres especiais
        if (isset($this->var['value'])){
            //flag de erro
            $erro8 = 0; 
            $array8 = array();
            $cpf8 = trim($this->var['value']);
            $array8 = explode(".",$cpf8);
            $cpf8 = implode("", $array8);
            $array8 = explode("-",$cpf8);
            $cpf8 = implode("", $array8);

            if (strlen($cpf8)!=11){
                $erro8 = 1;
            }
            // Decompor o CPF no padrão receita
            else{
                $x=10;
                $y=0;
                $tot=0;
                $s=0;
                while($x>=2){
                    $s = $cpf8[$y] * $x;
                    $tot = $tot + $s;
                    $x--;
                    $y++;
                }
                //Obter o primeiro dígito
                $h = $tot%11;
                if ($h>=2){
                    $hi1 = 11 - $h; 
                }
                else {
                    $hi1 = 0; 
                }
                // Valida o primeiro dígito e obtém o segundo
                if ($hi1 == $cpf8[9]){
                    $z = 11;
                    $a = 0;
                    $tot2 = 0;
                    $s2 = 0;
                    while($z>=2){
                        $s2 = $cpf8[$a]*$z;
                        $tot2 = $tot2 + $s2;
                        $z--;
                        $a++;
                    }
                    $h2 = $tot2%11;
                    if ($h2>=2){
                        $hi2= 11 - $h2; 
                    }
                    else{ 
                        $hi2 = 0; 
                    }
                    //Valida segundo dígito
                    if ($hi2 == $cpf8[10]){
                        //echo "segundo digito válido. <br>"; 
                    }
                    else{
                        $erro8 = 1;
                    }
                }
                else {
                    $erro8 = 1;
                }
            }//FIM VALIDAÇÂO CPF

            if ($erro8 == 1){
                array_push($this->err["{$this->var['name']}"], "{$this->var['name']} de inválido");
            }
        }
        return $this;
    }
    
asked by anonymous 01.04.2016 / 16:29

2 answers

0

The simplest way to avoid ifs is to extract the maximum in objects (classes and methods).

What's wrong is the way you think and can not make the method do just one thing.

In this function that you put there are at least 4 different things if you think that each one could be a function of your object would be simpler to organize.

A woman who has a lot of knack for doing this is Sandi Metz's one. link

    
01.04.2016 / 19:01
0

Avoiding ifs is natural because they mess up the code a lot, but it is impossible to program without them and sometimes an ifs nesting is inevitable. there are other links that replace if, as is the case of the switch (I almost never use it). It so happens that we can and must think in a more logical and therefore lazy way. I will give an example of substitur ifs per array.

With ifs

 if($fruta = "jambo"){
      echo "gosto";
    }
if($fruta = "maça"){
      echo "gosto";
    }
if($fruta = "tomate"){
      echo "Não gosto";
    }

Replacing with array

 $array = array(
    "jambo" => "gosto",
    "Maça" => "gosto",
    "Tomate" => "não gosto",
    );
echo $array[$fruta];

If the volume of data is large, use reduced array

$array = array(
"Não gosto" => array("tomate", "Sapoti", "melão"),
"gosto" => array("jambo", "Maça", "uva", "laranja", "banana", "goiaba")
);
foreach($array as $key => $row){
    foreach($row as $value){
        if($value == $fruta){
             echo $key; //$key é a chave do primeiro array pode ser gosto ou não gosto
        }
    }
}
    
01.04.2016 / 23:21