Nested, Nested, Nested, Nested If Else Statements

When I started programming in college the professor hardly checked the source code of my program. In their mind as long as it worked and did what the assignment said I got an A on the assignment. This lead to some very bad programming habits, one of which was nested if else statements to handle errors.

When you start nesting too many conditional statements together at once source code becomes difficult to maintain and even more difficult to add new functionality to the program.

Take for example this program:

private void btnCalc_Click(object sender, EventArgs e) {
		if(VerifyInput(txtNum1.Text)) {
			number1 = Int32.Parse(txtNum1.Text);
			if(VerifyInput(txtNum2.Text)) {
				number2 = Int32.Parse(txtNum2.Text);
				if(VerifyInput(txtNum3.Text)) {
					number3 = Int32.Parse(txtNum3.Text);
					average = (number1 + number2 + number3) / 3.0;
					txtAverage.Text = average.ToString();
				} else { 
					MessageBox.Show("Error: Enter an integer."); 
				}
			} else {
				MessageBox.Show("Error: Enter an integer.");
			}
		} else {
			MessageBox.Show("Error: Enter an integer.");
		}
	}

So in this example I nested three if statements together checking for errors. While it is stressful to navigate the if statements in this simple program it is possible. But, what if we had to add an extra number to the program? Then, we would have to navigate the if statements again and make sure we put them in the right spot. This becomes harder as we add more numbers to the program.

For this example this is only a simple program but, what if it had been something more complex like adding a new customer account. Then, you would need their name, address, city, state, zip, phone number, e-mail address, method of payment, and payment infomation. Would nesting nine if statements together be the most efficient way to check for input errors from the user? Not really, it would be too difficult to code and even more difficult for the next programmer to come in behind to read it and make alterations to the program.

So is there a better way to code this? Yes, this below is much better code.

private void btnCalc_Click(object sender, EventArgs e) {
		if(!VerifyInput(txtNum1.Text)) {
			MessageBox.Show("Error: Enter an integer.");
			return;
		}
		if(!VerifyInput(txtNum2.Text)) {
			MessageBox.Show("Error: Enter an integer.");
			return;
		}
		if(!VerifyInput(txtNum3.Text)) {
			MessageBox.Show("Error: Enter an integer.");
			return;
		}

		number1 = Int32.Parse(txtNum1.Text);
		number2 = Int32.Parse(txtNum2.Text);
		number3 = Int32.Parse(txtNum3.Text);
		average = (number1 + number2 + number3) / 3.0;
		txtAverage.Text = average.ToString();
	}

See how much more readable that code is and the best part is if we had to add more numbers we can just copy and paste the if statement and edit it to fit the new textbox. Then, update the code at the bottom by adding another Int32Parse statement and editing what the average variable would be.

So a good rule of thumb to use when programming is not to nest if and else or try and catch statements more than two layers deep. However this is not an absolute rule and there are some instances where you need to nest if more than two layers deep. But, if you are it is a good idea to try to take a step back and look at your code and see if it could be programmed differently by reversing the condition and adding a return statement.