PRG 140 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 any of these guidelines. Use the textbook as an example to properly documented code but if there is a difference between these standards and any of the textbook's examples, follow 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, Type/End Type, etc. structures for readability.

  3. Make sure that you name each form and provide a Caption to each form.

  4. 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 controls the same access key.

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

  6. Line up inline comments when they appear to the right of executable code.

  7. If a procedure contains several major steps or functional parts, there should be comments (called signpost comments) separating the function into each major part.

  8. Variable declarations and descriptive inline comments must be one per line and aligned consistently where possible.

  9. Statements which are continued on a second line must be indented significantly (more than the normal indentation). You may have to use the line continuation operator ([space]_ ). Make sure that long statements do not wrap around to the next line when printed.

  10. Add blank lines above and below structures such as loops and multiple line if statements.

  11. You must use the standard Visual Basic prefixes when naming objects:

Object Class Prefix Example
Form frm frmUserEntry
Command button cmd cmdExit
Text box txt txtDescription
Label lbl lblSum
Option button opt optSingle
Check box chk chkSummary
Frame fra fraChoices
Horizontal scroll bar hsb hsbSpeed
 
Object Class Prefix Example
Vertical scroll bar vsb vsbHeight
Image img imgDesign
Picture box pic picFace
Combo box cbo cboList
List box lst lstEntries
Common Dialog dlg dlgCommon
Menu mnu mnuFile
Shape shp shpRectangle
Common Dialog dlg dlgCommon

Documentation

  1. Include the following information at the very top of the general declarations section of every form in a project and the standard code module. This is called a header.

                                                                          example

    Programmer Name
    Course
    Project Name
    Date
    Program Description

    ' John Doe
    ' PRG 140
    ' Assignment1JD
    ' 9/27/01
    ' Purpose - This program will serve as a checking account
    '         calculator, allowing the user to deposit
    '         and withdraw funds.


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

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

  4. Variable names (and other identifiers) must be self-documenting.   That is, variable names should be whole words or phrases that make refer to the purpose they serve within the program or function. Follow the VB conventions with regard to prefixes for variables based on data type and scope. Follow the InterCap method and other naming conventions specified by our textbook for naming variables, constants, objects, and procedures. Use all uppercase letters in the names of constants, except for the prefix. For example, msngTAX_RATE would be an appropriate name of a module-level constant.

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

  6. Every procedure and function should have a comment below the first line of the procedure that describes the purpose of the procedure. Place a blank line below this comment.

  7. Place parenthesis 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.Caption = "The sum is greater than 100."
    End If

  8. 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.Caption = "The sum is greater than 100."

Turning-In Programming Assignments

  1. Delete empty procedures.

  2. Order the code on top of the form image for each form. Staple the pages together unless you use a three-ring folder or a "plastic sleeve" folder.

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

  1. Local variable declaration (Dim) statements should always appear at the top of a procedure. Module-level and global variables must appear in the general declarations section of a form or module. Declare all variables with the most appropriate data types and level of scope.

  2. Always use the Option Explicit statement in the general declarations section of every form and module. Blank lines should be placed around this statement and it should be placed after the header but before any module-level declarations.

  3. Use perfect spelling and grammar throughout your code, especially where it is viewed by a user.

  4. Name general function procedures with prefixes that indicate the return type of the function.