Structuring Your Code Logically

function OpenWin(url, w, h) { if(!w) w = 400; if(!h) h = 300; window.open(url, "_new", "width=" + w + ",height=" + h + ",menubar=no,toobar=no,scrollbars=yes", true); } function Print() { window.focus(); if(window.print) { window.print(); window.setTimeout('window.close();',5000); } }
Team-Fly    

Special Edition Using Microsoft® Visual Basic® .NET
By Brian Siler, Jeff Spotts
Table of Contents
Chapter 7.  Controlling the Flow of Your Program


Before we leave the subject of control-flow statements, we need to mention something about the influence of programming style on your code. You have an enormous amount of freedom when writing VB code, but you should not let that freedom cause you to write messy code. As an example, let's look at test grades again. We have already covered how to do averaging and number-to-letter conversion using a Select Case statement. However, suppose someone enters an invalid numeric grade in one of the text boxes, such as 789 or 1234? Validating the data entered in the text boxes will require adding some If statements before you attempt to average the grades. If any of the grades entered is not a valid numeric grade, you should not average them but instead report this to the user.

Suppose we have a function called ValidGrade() that returns True or False depending on whether the text box value is a number between 0 100. Each programmer may come up a different way of arranging the input validation code. For example, you could type the If statements in a nested manner as follows:

 If ValidGrade(txtTest1.Text) Then        If ValidGrade(txtTest2.Text) Then              If ValidGrade(txtTest3.Text) Then                    ' INSERT LISTING 7.1 HERE              End If        End If  Else        MessageBox.Show ("Invalid grade entered!")  End If 

You also can use an And operator and put all the grade validation calls in the same If condition:

 If ValidGrade(txtTest1.Text) And ValidGrade(txtTest2.Text)     And ValidGrade(txtTest3.Text) Then        ' INSERT LISTING 7.1 HERE  Else        MessageBox.Show("Invalid grade entered!")  End if 

Finally, you could just do all the input validation up front and exit the subroutine early if there are any problems:

 If Not ValidGrade(txtTest1.Text) Then        MessageBox("Invalid grade entered!")        Exit sub  End if  If Not ValidGrade(txtTest2.Text) Then        MessageBox("Invalid grade entered!")        Exit sub  End if  If Not ValidGrade(txtTest3.Text) Then        MessageBox("Invalid grade entered!")        Exit sub  End if  ' INSERT LISTING 7.1 HERE 

As you can see, there are at least three different ways of validating the text box values by different arrangement of the If statements. Let's look at the structure of each in more detail:

  • Nested If statements are probably the most correct in an academic sense, because there is essentially only one path through the function. One If statement is executed, which happens to contain another If, and so on. The drawback is that if you have a lot of nested Ifs and many lines of code, it may be difficult to keep all the nested conditions straight when reading the code.

  • Boolean operators such as And allow you to include multiple conditions on the same line. This method is about as logical as the nested If, but may be less cluttered and easier to read on the screen. However, when using And you need to be aware of short-circuiting, as described earlier, to avoid unnecessary execution of the ValidGrade function.

  • Exiting the function early is an entirely different approach to solving the problem. I have seen VB programmers use this method because it separates the input validation and task at hand entirely. However, when showing another developer this code, he said "that type of coding would get you fired where I used to work." I asked why and his answer was that having multiple exit points in a function makes the logic too hard to follow. So, to keep the code truly logical, you should move the validation code to a separate function:

 If CheckTextBoxes() Then        ' INSERT LISTING 7.1 HERE  Else        MessageBox.Show("Invalid grade entered!")  End If 

As you can see, even programming style can influence the flow of your program code. As you gain more experience programming, you will be able to write code that is logically easier to follow. Another good way to learn more about these types of issues is to study computer science in school. One result of a good computer science education is that you will learn about good programming practices you can use with any computer language.


    Team-Fly    
    Top
     



    Special Edition Using Visual Basic. NET
    Special Edition Using Visual Basic.NET
    ISBN: 078972572X
    EAN: 2147483647
    Year: 2001
    Pages: 198

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net