I know this is probably very basic, and I googled it and have gone through the search, but I\'m having an issue which a very basic if/else scenario. The first statement produce
If I were to align your code block with how the compiler sees it, this is how it would look. You can see in the code below that since there are no explicit start and ends brackets for your if
blocks that the else
blocks are coupled with the closest previous if
. As Peter said, the white space in C# does not matter like it does in python.
private void button1_Click(object sender, EventArgs e)
{
int season;
int job;
season = Convert.ToInt32(textBox1.Text);
job = Convert.ToInt32(textBox2.Text);
if (season == 1)
if (job == 1)
label3.Text = "There is a 20% discount on the exterior job";
else
if (season == 2)
if (job == 1)
label3.Text = "There is a 20% discount on the exterior job";
else
if (season == 3)
if (job == 2)
label3.Text = "There is a 30% discount on the interior job";
else
label3.Text = "No discount, regular prices apply";
}
Now we could add some curly braces here to fix the issue.
private void button1_Click(object sender, EventArgs e)
{
int season;
int job;
season = Convert.ToInt32(textBox1.Text);
job = Convert.ToInt32(textBox2.Text);
if (season == 1)
{
if (job == 1)
{
label3.Text = "There is a 20% discount on the exterior job";
}
}
else
{
if (season == 2)
{
if (job == 1)
{
label3.Text = "There is a 20% discount on the exterior job";
}
}
}
else
{
if (season == 3)
{
if (job == 2)
{
label3.Text = "There is a 30% discount on the interior job";
}
}
}
else
{
label3.Text = "No discount, regular prices apply";
}
}
The problem with code like this is 2 fold. 1) it is not very readable, and 2) it is not very testable. As Peter also pointed out you can easily reduce the complexity of this code by combining some of the if statements as below.
if ((season == 1 || season == 2) && job == 1)
{
label3.Text = "There is a 20% discount on the exterior job";
}
else if (season == 3 && job == 2)
{
label3.Text = "There is a 30% discount on the interior job";
}
else
{
label3.Text = "No discount, regular prices apply";
}
While this makes the code much easier to understand and reduces the duplication of the string messages, I would not stop here. In order for this code to be testable we need to remove the dependency it has on the fact that there is a button click, and presumably a form component involved (label3
). To do that we can move this block of code to a method that returns a string, rather than sets a string.
private void button1_Click(object sender, EventArgs e)
{
int season = Convert.ToInt32(textBox1.Text);
int job = Convert.ToInt32(textBox2.Text);
label3.Text = GetDiscount(season, job);
}
private String GetDiscount(int season, int job)
{
if ((season == 1 || season == 2) && job == 1)
{
return "There is a 20% discount on the exterior job";
}
if (season == 3 && job == 2)
{
return "There is a 30% discount on the interior job";
}
return "No discount, regular prices apply";
}
In this manner we have decoupled the code from the form that is involved in inputing and displaying the data. We have also reduced the complexity even more by eliminating the need for the else statements. By returning the string from the method we exit the block of code as there is no need to continue performing the if checks.