Как убрать лишние операторы If из плохого кода - PullRequest
0 голосов
/ 30 марта 2020

Я получил плохой код в существующем проекте. Пожалуйста, посмотрите один раз и скажите мне предложения относительно этого рефакторинга.

if (!(txtSurnameSearch.Text.Length >= 2 && txtInitialsSearch.Text.Length != 0 ||
                      txtSurnameSearch.Text.Length >= 3 && txtInitialsSearch.Text.Length == 0 ||
                      ctrlIDNumberSearch.Text.Length >= 6 || ctrlMPLNumberSearch.Text != "" ||
                      !string.IsNullOrEmpty(txtUcn.Text)))
                {
                    if (txtSurnameSearch.Text.Length < 2 && txtInitialsSearch.Text.Length != 0 && ctrlMPLNumberSearch.Text == "")
                        lsInvalidSearchNotification += "Surname must contain at least 2 characters when initials are not blank OR" + "\r\n";
                    if (txtSurnameSearch.Text.Length < 3 && txtInitialsSearch.Text.Length == 0)
                        lsInvalidSearchNotification += "Surname must contain at least 3 characters when initials are blank OR" + "\r\n";
                    lsInvalidSearchNotification += "ID Number must contain at least 6 characters OR" + "\r\n";
                    if (ctrlMPLNumberSearch.Text == "")
                        lsInvalidSearchNotification += "MPL Number must be valid, 9 characters" + "\r\n";
                    lbComboPassed = false;
                }

1 Ответ

0 голосов
/ 30 марта 2020

Я сделал следующие изменения (первый этап), чтобы удалить несколько if.

                int surnameLen= txtSurnameSearch.Text.Length;
                int initialsLen=txtInitialsSearch.Text.Length;
                int idnumberLen =ctrlIDNumberSearch.Text.Length;
                int mPLNumberLen =ctrlMPLNumberSearch.Text.Length;
                int ucnLength =txtUcn.Text.Length;


                if (surnameLen < 2 && initialsLen != 0 ){
                        lsInvalidSearchNotification += "Surname must contain at least 2 characters when initials are not blank OR" + "\r\n";
                    if (surnameLen < 3 && initialsLen == 0)
                        lsInvalidSearchNotification += "Surname must contain at least 3 characters when initials are blank OR" + "\r\n";
                    lsInvalidSearchNotification += "ID Number must contain at least 6 characters OR" + "\r\n";
                    if (mPLNumberLen != 9)
                        lsInvalidSearchNotification += "MPL Number must be valid, 9 characters" + "\r\n";


                if (!(
                      idnumberLen >= 6 || mPLNumberLen != 0 ||
                      ucnLength!=0 || lsInvalidSearchNotification.Length!=0))
                {

                    lbComboPassed = false;
                }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...