This lesson will look closer at the related topics of code blocks and nested statements.
Let’s start by creating a new ASP.NET project called “CS-ASP_025” with the following Controls and programmatic IDs:
In the Default.aspx.cs file, write the following code:
Here we see seven separate code blocks. The outer-most code block (1) is the namespace, which immediately holds (2) a single block of code – a class – which is called Default. The class level then immediately holds two method code blocks named (3) Page_Load and (4) addButton_Click. Finally, addButton_Click code block then holds a series of nested if() statements (5), (6) and (7). Notice, also, how you can expand and collapse the individual blocks/nested statements by clicking on the boxes with dashes in them (highlighted in green).
Up to this point, we have been referring to the named code blocks that determine behavior for Server Controls as events. However, technically these code blocks are methods, and methods can subscribe to special kinds of properties called events. All of the methods we have been writing code into, thus far, happen to subscribe to events. This will be clarified in further lessons covering methods and events.
Nested if() statements are somewhat different from the containment relationships seen in the other code blocks. They are simply used to perform logical branching in your code – as seen in previous lessons – and should be nested no more than a few levels deep. The reason for this is that it can make code look very messy and difficult to read, and there are often ways to solve the problem with other techniques. For instance, here is another way of determining whether or not all of the CheckBoxes are checked, and displaying the resultLabel only when they are:
This solves the problem in a much cleaner way since the return keyword effectively exits out of further execution for this method when any of the CheckBoxes are not checked. This provides the same functionality as the nested if() statements displaying the resultLabel message only when all CheckBoxes are checked.
Another, simpler, way of solving this problem is to make a compound expression within a single if() statement, like this:
Although this compound expression within a single if() statement is logically equivalent to the nested if() statements, there are some cases where you can’t simplify in such a way. For example, if you needed different code to execute for each condition (or, each CheckBox in this example), you would definitely need those contingencies to exist on their own “branch” of code.
In upcoming lessons we will look at looping conditional statements that can often take on deep nesting structures. However even in this case there are ways to reduce the visual impact of deep nesting by breaking up nested code and putting it into helper methods, for example.
The main takeaway from this lesson is this: Understanding the relationship between code blocks, specifically the namespace, class method level, and the nature of nested code blocks. Nested code serves a purpose, but deeply nested code should be avoided as much as possible to ensure readability. Whenever possible, simplify your code to help yourself and others when reading it. Upcoming lessons will detail different ways to simplify your code without sacrificing functionality.
Lesson 25 - Code Blocks and Nested If Statements