Wyo Visual Basic Coding Standards
Wyo Coding Standards Home Page | VB Home Page

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.

Coding

  1. Type blank spaces around operators of all kinds.

    Example: Instead of
            intSum=intAmount+intSum
                     use                     intSum = intAmount + intSum

                    since it is much easier to read

  2. Use consistent indentation, especially with regard to complex structures like if statements, loops, etc. Do not indent the lines in the general declarations section of a form or code module. Indent all lines inside If/Then/Else, With/End With, For/Next, Do/Loop, Select Case, etc. statements for readability.

  3. Line up inline comments where possible.

  4. 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. Add a blank line between methods.

  5. Make sure that you provide a meaningful Text property to each form.

  6. Follow the Windows standards, especially in relation to color, size, and placement of controls.  Also, use access keys that are considered standard in Windows software (e.g. x for Exit and s for Save). Do not give two objects the same access key.

  7. Make sure that the focus is on the correct object when a form displays. The tab order must proceed correctly when the user presses the Tab key.

  8. Constant & variable declaration statements should always appear at the top of a method. Global variables & constants must be declared in a code module. But you should never use global variables anyway.

  9. 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
    Date
    ' John Doe
    ' Unit1Proj1
    ' Period 1
    ' 9/9/08

  10. Never use goto statements or global variables.

  11. Statements which are continued on a second line must be indented further. You can use the line continuation operator ([space] _ ) to break up lines that would be cut off by the printer. If the VB printer produces a small black continuation arrow in the right margin then the continued line of code may be even with the first line of code.

  12. 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.

    Example:

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


  13. Use multiline If statements rather than single-line If statements to make your code more readable. Do not use If statements in the form,

    If (intSum > 100) Then lblOutput.Text = "The sum is greater than 100."

  14. 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 Class Prefix Example
    Button btn btnExit
    Text box txt txtDescription
    Label lbl lblSum
    Radio button rdb rdbSingle
    Check box chk chkSummary
    Horizontal scroll bar hsb hsbSpeed
     
    Object Class Prefix Example
    Vertical scroll bar vsb vsbHeight
    Picture box pic picFace
    Combo box cbo cboList
    List box lst lstEntries
    Common Dialog dlg dlgCommon
    Menu mnu mnuFile

  15. Use perfect spelling and grammar throughout your code, especially when it can be viewed by a user. If necessary, copy and paste your code into a word processor, like MS Word, and run a spell-check before you turn it in.

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 procedure 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. Always include a variable dictionary to explain the purpose of each variable. That is, there should be an inline comment to the right of each variable declaration (Dim statement) and constant declaration (Const statement) that explains the purpose of the variable or constant. You can use phrases instead of complete sentences. Line up the inline comments vertically as much as possible. Variable declarations must be one per line and aligned consistently.

  3. Follow the InterCap method for naming variables (e.g. intTotalPrice), making sure that the proper prefix (in lowercase letters) is used to identify a variable's data type or a function's return type. Programs must be "self-documenting."   A program is self-documenting if its variable names imply the purposes of the variables. Variable names should be whole words or phrases that make refer to the purpose they serve within the program or function. Constants must be named with all-capital letters and underscores to separate consecutive words except for the data type prefix. For example, a module-level constant that identifies the maximum number might be named mintMAX_NUMBER.

  4. 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.

  5. Every method should have a comment above the first line of the method that describes the purpose of the method.

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.

  3. Name general functions with prefixes that indicate the return type of the function.