Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this multi line if statement too complex?

I am validating input on a form and attempting to prompt the user of improper input(s) based on the combination of controls used.

For example, I have 2 combo boxes and 3 text boxes. The 2 combo boxes must always have a value other than the first (default) value, but one of three, or two of three, or all text boxes can be filled to make the form valid.

In one such scenario I have a 6 line if statement to try to make the test easily readable:

if ((!String.Equals(ComboBoxA.SelectedValue.ToString(), DEFAULT_COMBO_A_CHOICE.ToString())
    && !String.IsNullOrEmpty(TextBoxA.Text)
    && !String.Equals(ComboBoxB.SelectedValue.ToString(), DEFAULT_COMBO_B_CHOICE.ToString()))        
    ||
    (!String.IsNullOrEmpty(TextBoxB.Text)
    || !String.IsNullOrEmpty(TextBoxC.Text)))
{
    //Do Some Validation
}

I have 2 questions:

  1. Should this type of if statement be avoided at all cost?

  2. Would it be better to enclose this test in another method? (This would be a good choice as this validation will happen in more than one scenario)

Thanks for your input(s)!

like image 358
TheDevOpsGuru Avatar asked Jan 07 '11 16:01

TheDevOpsGuru


People also ask

Can an if statement span multiple lines?

Long if statements may be split onto several lines when the character/line limit would be exceeded. The conditions have to be positioned onto the following line, and indented 4 characters. The logical operators ( && , || , etc.)

How do I format multiple if statements in Python?

The recommended style for multiline if statements in Python is to use parentheses to break up the if statement. The PEP8 style guide recommends the use of parentheses over backslashes and putting line breaks after the boolean and and or operators.

How do you wrap a long if statement in Python?

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line appropriately.


2 Answers

In such a case I find it helps to move some of the logic out of the if statement and into some more meaningfully named booleans. Eg.

bool comboBoxASelected = !String.Equals(ComboBoxA.SelectedValue.ToString(), DEFAULT_COMBO_A_CHOICE.ToString());
bool comboBSelected = !String.Equals(ComboBoxB.SelectedValue.ToString(), DEFAULT_COMBO_B_CHOICE.ToString());
bool textBoxAHasContent = !String.IsNullOrEmpty(TextBoxA.Text);
bool textBoxBHasContent = !String.IsNullOrEmpty(TextBoxB.Text);
bool textBoxCHasContent = !String.IsNullOrEmpty(TextBoxC.Text);

bool primaryInformationEntered = comboBoxASelected && textBoxAHasContent && comboBSelected;
bool alternativeInformationEntered = textBoxBHasContent || textBoxCHasContent;

if (primaryInformationEntered || alternativeInformationEntered)
{
    //Do Some Validation
}

Obviously, name the combo and text boxes to reflect their actual content. When someone has to work their way through the logic several months down the line they'll thank you.

like image 132
Evil Andy Avatar answered Oct 27 '22 01:10

Evil Andy


  • a) Doesn't have to necesarily be avoided at all costs. The code works. But it is certainly messy, confusing and I would say could be difficult to maintain.
  • b) Yes. Give it a relevant name so that the code reader knows what is going on there.
like image 41
Szymon Rozga Avatar answered Oct 27 '22 01:10

Szymon Rozga