O
O
Orbite2018-10-25 14:04:45
Perl
Orbite, 2018-10-25 14:04:45

How to reduce a large if condition?

Good day to all!
There is a big condition, you need to reduce it to an acceptable size, I have been sitting on it for several hours. Can anyone help shorten it while retaining all the logic?

if ($contr_type == 0) {
            if ($contr_doc_id != 4) {
                if ($contr_doc_id == 1 && ($contr_ser_doc =~ /\D/g || $contr_num_doc =~ /\D/g)) {
                    if (!$contr_bday) {
                        $personal_data = '1. паспорт и дата рождения';
                    } else {
                        $personal_data = '2. паспорт';
                    }
                } else {
                    if ($contr_ser_doc && $contr_num_doc && $contr_bday) {
                        $personal_data = 'Ок';
                    } elsif ((!$contr_ser_doc || !$contr_num_doc) && !$contr_bday) {
                        $personal_data = '1. паспорт и дата рождения';
                    } elsif ((!$contr_ser_doc || !$contr_num_doc) && $contr_bday) {
                        $personal_data = '2. паспорт';
                    } elsif (($contr_ser_doc && $contr_num_doc) && !$contr_bday) {
                        $personal_data = '3. дата рождения';
                    }
                }
            } else {
                if (($contr_ser_doc || $contr_num_doc) && $contr_bday) {
                    $personal_data = 'ОК';
                } elsif (!$contr_ser_doc && !$contr_num_doc && !$contr_bday) {
                    $personal_data = '1. паспорт и дата рождения';
                } elsif ((!$contr_ser_doc && !$contr_num_doc) && $contr_bday) {
                    $personal_data = '2. паспорт';
                } elsif (($contr_ser_doc || $contr_num_doc) && !$contr_bday) {
                    $personal_data = '3. дата рождения';
                }
            }
        } elsif ($contr_type == 1) {
            if ($contr_ip == 1) {
                if ($contr_inn) {
                    $personal_data = 'Ок';
                } else {
                    $personal_data = '2. Нет ИНН';
                }
            } else {
                if ($contr_inn && $contr_kpp) {
                    $personal_data = 'Ок';
                } elsif (!$contr_inn && $contr_kpp) {
                    $personal_data = '2. Нет ИНН';
                } elsif ($contr_inn && !$contr_kpp) {
                    $personal_data = '3. Нет КПП';
                } else {
                    $personal_data = '1. Нет ИНН и КПП';
                }
            }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pcdesign, 2018-10-25
@pcdesign

1) You can try to choose one of the Switch modules that seems most convenient:
https://metacpan.org/search?q=module%3ASwitch
2) Convert all logic to hashes, like this:

my %password = (
    'tom' => '123!',
    'frank' => '321!',
);

if ( exists $password{$name} && $password eq $password{$name} ) {
# что-то делаем
}

3) Instead of magic numbers $contr_doc_id != 4 , you can use constants
use constant {
    FOO => 4,
    BAR => 5
};

https://perldoc.perl.org/constant.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question