Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 3 - Write Readable Code and Comments for Windo...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Write Readable Code and Comments for Windows Forms Applications
By: Murach Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-07-09

    Table of Contents:
  • Write Readable Code and Comments for Windows Forms Applications
  • The event handlers for the Invoice Total form
  • How to code with a readable style
  • How to code comments

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

    1. Use indentation and extra spaces to align statements and clauses within statements so they reflect the structure of the program.
    2. Use spaces to separate the words, operators, and values in each statement. 
       
    3. Use blank lines before and after groups of related statements. 
       
    4. Use line-continuation characters to shorten long lines of code so they’re easier to read in the Code Editor window.

    Notes

    1. 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.
    2. 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 

    More Visual Basic.NET Articles
    More By Murach Publishing


     

    Buy this book now. 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.

    VISUAL BASIC.NET ARTICLES

    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek