Visual Basic Coding Standards

Use the following styles and standards in your Visual Basic programs. You may lose points on any assignment if you fail to adhere to these guidelines.

When a programmer works for a company that produces software or studies computer science in college, he or she must work closely with others. Often, programmers work on team assignments. Therefore, the code that one programmer writes must be consistent with the code written by others around him or her including bosses, fellow employees, fellow students, and teachers. Following the institution's coding standards is essential. At Wyomissing, students must apply these principles to every program that is submitted for a grade. Even though a program may function properly, the programmer is responsible for documenting the program.

It is also essential for all programmers to use good internal documentation (i.e. comments) so other programmers can understand your code. It is also important to use good external documentation (i.e. user manuals, Help menus, etc.) so your program is user-friendly and doesn't confuse users.

Coding

  1. Use perfect spelling and grammar throughout your code, especially when it can be viewed by a user. If necessary, use a dictionary or copy and paste your code into Microsoft Word or Google to do a spell-check.

  2. Include the following information in the general declarations section of every form in a project and the standard code module:

    Programmer Name
    Project Name
    Class Period
    ' John Doe
    ' Chaos
    ' Period 1

  3. Make sure that you provide a meaningful Text property to each form. Do not keep Form1 as the Text property for your form.

  4. You must use the standard Visual Basic prefixes when naming objects (except for labels that never change throughout a program). Here are the proper prefixes for some useful objects:

    Object Prefix Example
    Button btn btnExit
    Text box txt txtDescription
    Label lbl lblTitle
    Radio button rdb rdbSingle
    Check box chk chkSummary
    Horizontal scroll bar hsb hsbSpeed
     
    Object Prefix Example
    Vertical scroll bar vsb vsbHeight
    Picture box pic picFace
    Combo box cbo cboList
    List box lst lstEntries
    Menu mnu mnuFile

  5. Each method should have a comment above it that describes the purpose of that method. For example

    ' ends program
    Private Sub btnExit_Click( )
        Application.Exit()
    End Sub

  6. Add a blank line between methods. For example,

       ' clears labels
       Private Sub btnClear_Click( )
          lblStep1.Text = ""
          lblStep2.Text = ""
       End Sub

       ' ends program
       Private Sub btnExit_Click( )
          Application.Exit()
       End Sub

    End Class


  7. Make sure that the focus is initially placed on the correct object when a form displays. The focus should move around the form in a logical manner when the user presses the Tab key.

  8. Use underlined access keys on buttons and in menu commands that are considered standard in Windows software (e.g. x for Exit and s for Save). Do not give two objects the same underlined access key.

  9. Variable and constant declaration statements (i.e. Dim and Const statements) should appear at the top of a method. Declaration statements for module variables & constants should be placed at the top of the form. Do not use global variables.

  10. There must be a comment to the right of each variable declaration (Dim statement) and constant declaration (Const statement) that explains the purpose of the variable or constant in the method or program. You can use phrases or symbols instead of complete sentences. Line up these comments vertically if possible. For example,

    Dim numApples As Integer = 0          ' # of apples purchased by customer
    Const APPLE_PRICE As Double = 0.65    ' price per apple


  11. Even though VB usually does this automatically, make sure that there are blank spaces around operators such as +, *, and = to make the code easier to read.

    Example: Instead of
            sum=num1+num2
                     use                     sum = num1 + num2

          
  12. Use consistent indentation. Indent all statements inside the body of methods, If statements, and loops.

  13. Add blank lines above and below  loops and If statements. Also, add a blank line below a section of code that contains variable declarations (i.e. Dim statements) and below the comments at the top of each method.

  14. Place parentheses around control expressions in all If statements and loops. Even though this is not required in Visual Basic, it makes your code easier to read and it gives you good habits for other computer languages.

    Example:

    If (num > 100) Then
        lblOutput.Text = "The number is greater than 100."
    End If


  15. Do not use single-line If statements in the form,

    If (num > 100) Then lblOutput.Text = "The number is greater than 100."

    Instead, you should use multiline If statements to make your code more readable.

Documentation

  1. Use a consistent format with regard to internal documentation. If you use complete sentences in inline comments, then do so throughout your program's code, otherwise use explanatory, understandable phrases. Remember comments are the main form of internal documentation. They should be explanatory but not too verbose (wordy). They should help explain the purpose of the program or method in which they are used. They should also explain the purpose of variables within specific algorithms. Always use correct spelling, punctuation, and grammar within comments.

  2. A program should be self-documenting which means that its variable names are descriptive and not single letters like x and y. That is, variable names should be whole words or phrases that make refer to the purpose they serve within the program. Begin the names of variables with lowercase letters as in num instead of Num. However, if the variable name consists of two or more words then every word except for the first one should be capitalized as in numApples or numApplesPurchased. Constants must be named with all-uppercase letters and underscores to separate consecutive words (e.g. TAX_RATE).

  3. Annotate medium to complex algorithms and assignment statements with inline comments. Do not necessarily assume that fellow programmers (or instructors) will understand your logic. However, do not document the obvious.

Turning-In Programming Assignments

  1. Delete unused methods (i.e. methods that have no statements in their body.) Highlight the code in the code window and click the Delete key. The code should not appear on the final code printout. If it does, try it again.

  2. Never fix code or add statements with a pen or pencil. You MUST make any necessary corrections on the computer and reprint the program.


Other Links of Interest

Other Stylesheets & Standards from Corporations and Universities