Write Readable Code and Comments for Windows Forms Applications - How to code with a readable style
(Page 3 of 4 )
When you build an application, Visual Basic makes sure that your code follows all of its rules. If it doesn’t, Visual Basic reports syntax errors that you have to correct before you can continue.
Besides adhering to the coding rules, though, you should try to write your code so it’s easy to read, debug, and maintain. That’s important for you, but it’s even more important if someone else has to take over the maintenance of your code. You can create more readable code by following the four coding recommendations presented in figure 3-7. These recommendations are illustrated by the event handler in this figure.
The first coding recommendation is to use indentation and extra spaces to align related elements in your code. This is possible because you can use one or more spaces or tabs to separate the elements in a Visual Basic statement. In this example, all of the statements within the event handler are indented. In addition, the statements within each clause of the If statement are indented and aligned so you can easily identify the parts of this statement.
The second recommendation is to separate the words, values, and operators in each statement with spaces. If you don’t, your code will be less readable as illustrated by the second code example in this figure. In this example, each line of code includes at least one operator. Because the operators aren’t separated from the word or value on each side of the operator, though, the code is difficult to read. In contrast, the readable code includes a space on both sides of each operator.
The third recommendation is to use blank lines before and after groups of related statements to set them off from the rest of the code. This too is illustrated by the first procedure in this figure. Here, the code is separated into four groups of statements. In a short procedure like this one, this isn’t too important, but it can make a long procedure much easier to follow.
The fourth recommendation is to use line-continuation characters so long statements are easier to read in the Code Editor window. This also makes these statements easier to read when you print them.
Throughout this chapter and book, you’ll see code that illustrates the use of these recommendations. You will also receive other coding recommendations that will help you write code that is easy to read, debug, and maintain.
By default, the Code Editor automatically formats your code as you enter it. When you press the Enter key at the end of a statement, for example, the Editor will indent the next statement to the same level. In addition, it will capitalize all variable names so they match their declarations, and it will add a space before and after each operator.
A procedure written in a readable style
Private Sub btnCalculate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim discountPercent As Decimal
If txtSubtotal.Text >= 500 Then
discountPercent = 0.2
ElseIf txtSubtotal.Text >= 250 And txtSubtotal.Text < 500 Then
discountPercent = 0.15
ElseIf txtSubtotal.Text >= 100 And txtSubtotal.Text < 250 Then
discountPercent = 0.1
Else
discountPercent = 0
End If
Dim discountAmount As Decimal = txtSubtotal.Text * discountPercent
Dim invoiceTotal As Decimal = txtSubtotal.Text - discountAmount
txtDiscountPercent.Text = FormatPercent(discountPercent, 1)
txtDiscountAmount.Text = FormatCurrency(discountAmount)
txtTotal.Text = FormatCurrency(invoiceTotal)
txtSubtotal.Select()
End Sub
Statements written in a less readable style
dim discountAmount as Decimal=txtsubtotal.Text*discountpercent
dim invoiceTotal as Decimal=txtsubtotal.Text-discountamount
txtdiscountpercent.Text=formatpercent(discountpercent,1)
txtdiscountamount.Text=formatcurrency(discountamount)
txttotal.Text=formatcurrency(invoicetotal)
Coding recommendations
Use indentation and extra spaces to align statements and clauses within statements so they reflect the structure of the program.
Use spaces to separate the words, operators, and values in each statement.
Use blank lines before and after groups of related statements.
Use line-continuation characters to shorten long lines of code so they’re easier to read in the Code Editor window.
Notes
As you enter code in the Code Editor, Visual Studio may adjust the indentation, spacing, and capitalization so it’s easier to read. This has no effect on the operation of the code.
If Visual Basic doesn’t adjust the code, check the Pretty Listing option in the Options dialog box. To find this option, expand the Text Editor group, the Basic group, and the VB Specific group.
Figure 3-7. How to code with a readable style
Next: How to code comments >>
More Visual Basic.NET Articles
More By Murach Publishing
|
This article is excerpted from chapter three of Murach's Visual Basic 2008, written by Anne Boehm (Murach, 2008; ISBN: 1890774456). Check it out today at your favorite bookstore. Buy this book now.
|
|