idan
idan

Reputation: 43

A better way than a switch case

I have a method that utilizes switch - case blocks similar to the below chunk of code:

    switch ($totalMonthsFromImmigration) {
        case $totalMonthsFromImmigration <= 18:
            $this->clauses->put('immigrant_1', $this->clauses->get('immigrant'));
            $this->clauses->put('immigrant_2', null);
            $this->clauses->put('immigrant_3', null);
            break;
        case $totalMonthsFromImmigration <= 30:
            $this->clauses->put('immigrant_1', null);
            $this->clauses->put('immigrant_2', $this->clauses->get('immigrant'));
            $this->clauses->put('immigrant_3', null);
            break;
        case $totalMonthsFromImmigration <= 42:
            $this->clauses->put('immigrant_1', null);
            $this->clauses->put('immigrant_2', null);
            $this->clauses->put('immigrant_3', $this->clauses->get('immigrant'));
            break;
    }

As it can be seen from the above example the cases make a similar things, and there's a little difference.

Is there a more elegant way to handle this situation? I don't really like the duplication of the code.

Upvotes: 0

Views: 551

Answers (5)

Kamal Maisuriya
Kamal Maisuriya

Reputation: 39

switch ($totalMonthsFromImmigration) {
         case $totalMonthsFromImmigration <= 42:
            $this->clauses->put('immigrant_1', $this->clauses->get('immigrant'));
            $this->clauses->put('immigrant_2', null);
            $this->clauses->put('immigrant_3', null);
        break;
        default:
           echo "Your logic you want greeter then 42"

}

Upvotes: 0

idan
idan

Reputation: 43

I thought there's a math formula to instead of using switch case or if else. But I didn't find one. This is my solution based on the answers I saw here:

    $this->clauses->put('immigrant_1', null);
    $this->clauses->put('immigrant_2', null);
    $this->clauses->put('immigrant_3', null);

    if ($totalMonthsFromImmigration <= 18) {
        $this->clauses->put('immigrant_1', $this->clauses->get('immigrant'));
    } else if ($totalMonthsFromImmigration <= 30) {
        $this->clauses->put('immigrant_2', $this->clauses->get('immigrant'));
    } else if ($totalMonthsFromImmigration <= 42) {
        $this->clauses->put('immigrant_3', $this->clauses->get('immigrant'));
    }

Upvotes: 0

Benni
Benni

Reputation: 1033

If calling the setter twice for the default values like @Jignesh Joisar is not desired:

$this->clauses->put('immigrant_1', ($totalMonthsFromImmigration <= 18) ? $this->clauses->get('immigrant') : null);
$this->clauses->put('immigrant_2', ($totalMonthsFromImmigration > 18 && $totalMonthsFromImmigration <= 30) ? $this->clauses->get('immigrant') : null);
$this->clauses->put('immigrant_3', ($totalMonthsFromImmigration > 30 && $totalMonthsFromImmigration <= 42) ? $this->clauses->get('immigrant') : null);

Upvotes: 1

Lawraoke
Lawraoke

Reputation: 556

Another way for doing this, try IF if you want to change from Switch case

if ($totalMonthsFromImmigration <= 18){
 $this->clauses->put('immigrant_1', $this->clauses->get('immigrant'));
} else if ($totalMonthsFromImmigration <= 30) {
 $this->clauses->put('immigrant_2', $this->clauses->get('immigrant'));
else if ($totalMonthsFromImmigration <= 42) {
 $this->clauses->put('immigrant_3', $this->clauses->get('immigrant'));
}

Upvotes: 2

Jignesh Joisar
Jignesh Joisar

Reputation: 15085

try to set default value then add value based on condition

$this->clauses->put('immigrant_1', null);
$this->clauses->put('immigrant_2', null);
$this->clauses->put('immigrant_3', null);

switch ($totalMonthsFromImmigration) {
            case $totalMonthsFromImmigration <= 18:
                $this->clauses->put('immigrant_1', $this->clauses->get('immigrant'));
                break;
            case $totalMonthsFromImmigration <= 30:
                $this->clauses->put('immigrant_2', $this->clauses->get('immigrant'));
                break;
            case $totalMonthsFromImmigration <= 42:
                $this->clauses->put('immigrant_3', $this->clauses->get('immigrant'));
                break;
 }

Upvotes: 0

Related Questions