Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested 'if'-'else' statements

I have code that's very messy with the if - else if checks it is doing. The amount of branching and nested branching is quite big (over 20 if - else if and nested too). It's making my code harder to read and will probably be a performance hog. My application checks for a lot of conditions it gets from the user and so the application must check all the time for different situations, for example:

If the textbox text is not 0, continue with the next...

if ((StartInt != 0) && (EndInt != 0))   
{

And then here it checks to see whether the user have choosen dates:

if ((datePickerStart.SelectedDate == null) || (datePickerEnd.SelectedDate == null)) 
{
    MessageBox.Show("Please Choose Dates");
}

Here, if the datepickers aren't null it continues with code...

else if ((datePickerStart.SelectedDate != null) && (datePickerEnd.SelectedDate != null))
{
    // CONDITIONS FOR SAME STARTING DAY AND ENDING DAY.
    if (datePickerStart.SelectedDate == datePickerEnd.SelectedDate)
    {
        if (index1 == index2)
        {
            if (StartInt == EndInt)
            {
                if (radioButton1.IsChecked == true)
                {
                    printTime3();
                }
                else
                {
                    printTime();
                }
            }

This is just a small part of the checks being made. Some of them are functionals and some are for input validation stuff.

Is there any way to make it more readable and less of a performance hog?

like image 389
Yosi199 Avatar asked Sep 08 '11 13:09

Yosi199


People also ask

What is a nested IF ELSE statement?

A nested if statement is an if-else statement with another if statement as the if body or the else body. Here's an example: if ( num > 0 ) // Outer if if ( num < 10 ) // Inner if System. out.

What is nested IF statement with example?

If the Test Score (in cell D2) is greater than 89, then the student gets an A. If the Test Score is greater than 79, then the student gets a B. If the Test Score is greater than 69, then the student gets a C. If the Test Score is greater than 59, then the student gets a D.

What is nested IF ELSE statement in C++?

nested-if in C/C++ A nested if in C is an if statement that is the target of another if statement. Nested if statements mean an if statement inside another if statement. Yes, both C and C++ allow us to nested if statements within if statements, i.e, we can place an if statement inside another if statement.

What is nested IF ELSE statement in Java?

nested-if: A nested if is an if statement that is the target of another if or else. Nested if statements mean an if statement inside an if statement. Yes, java allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.


2 Answers

It's not that of a performance hog. A great blog post on how to fix those common problems is Flattening Arrow Code.

like image 92
Daniel A. White Avatar answered Nov 03 '22 02:11

Daniel A. White


I see here some mix in validation. Try to move one fields from others, and validate them separately, something like this:

if (StartInt == 0 || EndInt == 0)
{
    MessageBox.Show("Please Choose Ints");
    return;
}
if (datePickerStart.SelectedDate == null || datePickerEnd.SelectedDate == null)
{
    MessageBox.Show("Please Choose Dates");
    return;
}

In this approach you'll always say to the user what he did wrong, and your code is much simpler.

More information from Jeff's blog

like image 29
VMAtm Avatar answered Nov 03 '22 04:11

VMAtm