Visual Basic Coding Standards
Visual Basic Coding Standards
Version 0.1
(Draft)
Revision History
Date Issue Description Author
11/03/99 0.1 Initial draft Steve Schmitt
Table of Contents
1 OBJECTIVES.....................................................................................................................................................5
3 DEFINITIONS....................................................................................................................................................5
4 USAGE GUIDELINES.......................................................................................................................................6
4.1 OPTION STATEMENTS..........................................................................................................................................6
4.2 DECLARATIONS..................................................................................................................................................6
4.3 ERROR HANDLING..............................................................................................................................................7
4.4 TESTING...........................................................................................................................................................8
6 COMMENTS.....................................................................................................................................................11
6.1 COMMENT FORMATTING....................................................................................................................................11
6.2 MODULE COMMENTS........................................................................................................................................11
6.3 PROCEDURE COMMENTS....................................................................................................................................11
6.4 DECLARATION COMMENTS.................................................................................................................................12
6.5 GENERAL COMMENTS.......................................................................................................................................12
7 NAMING CONVENTIONS.............................................................................................................................13
7.1 MODULE NAMES.............................................................................................................................................13
7.2 PROCEDURE NAMES.........................................................................................................................................13
7.2.1 General Procedure Names...................................................................................................................13
7.2.2 Boolean Function Names....................................................................................................................13
7.2.3 Conversion Method Names..................................................................................................................14
7.2.4 State Modifying Method Names...........................................................................................................14
7.3 VARIABLE AND PARAMETER NAMES...................................................................................................................14
7.3.1 Atomic Types........................................................................................................................................14
7.3.2 Object and Complex Types..................................................................................................................14
7.3.3 Constant Names...................................................................................................................................15
7.4 ARRAY NAMES................................................................................................................................................16
7.5 GLOBAL VARIABLE NAMES...............................................................................................................................16
7.6 MISCELLANEOUS..............................................................................................................................................16
1 Objectives
The objectives of this standard are:
• To document the Visual Basic coding conventions.
• To improve the quality of the deliverable software and its associated documentation.
• To improve communication within the development and maintenance teams.
• To decrease the costs associated with the software and its associated documentation.
3 Definitions
A convention is one of the following:
• A standard (S) is a mandatory constraint on the content or format of something (e.g., code,
documentation). You must follow standards unless you obtain a waiver from the Process Team
and you document the deviation from the standard.1
• A guideline (G) is an optional constraint on the content or format of something (e.g., code,
documentation). You should follow guidelines in most cases. You must document any deviation
from the guideline.
• A recommendation (R) offers advice. It is less constraining than either a standard or guideline.
You may follow or ignore recommendations as appropriate. You need not document any
deviations from recommendations.
Visual Basic (VB for short) refers to any programming environment, which uses a Visual Basic-like
language. The standards in this document apply to these other environments, as well as Visual Basic
itself:
• Visual Basic for Applications (VBA), used in Microsoft Office products
• Visual Basic Script (VBScript), used in Active Server Pages and in MS Outlook
1
You do not need to document or obtain a waiver to use an exception that is included in this
document.
Public Domain 1999 by Donald Firesmith Page 5
Donald Firesmith Document ID: VBCS Version: 0.1
4 Usage Guidelines
The following sections provide standards and guidelines for the general usage of Visual Basic. These
standards ensure consistent usage of various Visual Basic features.
4.2 Declarations
The following conventions help you build good abstractions that maximize cohesion:
S) Use narrow scoping. Use private variables and constants whenever feasible. Local (procedure-
level) variables are preferred, followed by module-level, with globals as a last resort.
Rationale: Increases cohesion.
S) Declare module level variables in this order:
1. Public API Declarations
2. Public Constants
3. Public Types
4. Public Variables
R) Include error handling in every procedure where errors are likely to occur. This includes any
procedure that performs I/O, database functions, operating system calls, COM operations, or
invokes methods on other objects. Errors should be handled in the same procedure where they
occur.
Rationale: Localized error handling increases cohesion, and allows you to provide error handling
specific to the types of errors that are likely to occur. The farther that a project-specific error
bubbles up, the less likely that it will make sense to the procedure that must handle it.
R) Use common error trapping label names. Use the same label name (for example, “ErrTrap”) in
all procedures
Rationale: This improves the consistency and readability of the code.
Example:
Sub cmdOK_Click
On Error Goto ErrTrap
Exit Sub
ErrTrap:
MsgBox “Error “ & Err.Number & “: “ & Err.Description & “
in “ & Err.Source, vbExclamation, App.Title
End Sub
4.4 Testing
R) Use Debug.Print Statements. Print the value of variables to help confirm intermediate results
and confirm that code is executing as expected. Debug.Print statements are ignored when the
code is compiled
Rationale: Makes code verification and debugging easier.
Rationale: Small, cohesive modules are more manageable and easier to understand. They also
reduce sharing conflicts in version control tools.
R) Avoid putting general code in Form modules. Form modules (and other modules with a visual
interface) should contain only the event procedures for the form. Procedures called by the event
procedures should be placed in other modules unless they deal directly with many of the controls
on the form itself.
Rationale: Form modules tend to grow large through their event procedures and visual
interface definitions. Moving other code into other modules will help to keep Form modules
small and focused on their main purpose, which is interacting with the user.
Because Forms consist of both binary and textual components, they are not handled as well
by version control tools. Keeping code in other modules minimizes conflicts.
R) Keep procedures to a manageable size. Each procedure should be concise enough that its entire
purpose can be easily expressed and understood. One rule of thumb is that the well-commented
procedure should fit on a printed page. A more relevant rule when editing the code is that it fit on
one screen in the development environment.
Rationale: Small procedures are easier to understand and verify.
Exception: A single control structure (for example, a Select structure) may be unavoidably longer
than the recommended procedure length. In this case, the control structure should be placed in its
own procedure, separate from other code, so as not to obscure the function of the code around it.
5.2 Indentation
G) Use the standard indentation provided by Visual Basic tabs. The VB environment provides tools
that make it easy to indent blocks of code by tab stops.
Rationale: Indentation improves readability. Using the standard tab-stop indentation improves
maintainability, allowing the indent level of blocks of code to be easily changed.
Exception: LotusScript indents automatically, and does not support tabs.
G) Use four spaces for each indent level. The default tab spacing of in VB is four spaces, and this
should be maintained.
Rationale: Four spaces is enough to make the indentation level obvious, without taking up too
much horizontal space.
Exception: LotusScript indents automatically
5.3 Spaces
R) Apply spaces liberally.
Rationale: Spaces break up the code, making it easier to read.
Example 1:
CheckPrecondition
DoFoo
DoFam
CheckPostcondition
End Sub
Comment: If the method is long or violates other standards, it should be factored based on the
logical sections described above.
6 Comments
The following Visual Basic comment standards provide a consistent approach to documenting code
that will also improve the quality of the associated javadoc documentation.
7 Naming Conventions
Naming conventions are partly based on those found in Bruce McKinney’s Hardcore Visual Basic.
s String sFirstName
b Boolean bQuiet
c Currency cCurrentBal
d Date dStartTime
Rationale: Consistency with standard Visual Basic and Windows API naming conventions.
Allows easy identification of the type and role of a variable, without adding a lot of naming
overhead for frequently used types.
Many other types are possible which are not on this list, including user-defined types and objects.
For these types, and appropriate three- or four-letter prefix convention should be established and
documented for the project.
(Note that the frm prefix should be used only for a variable in the code which holds a form
reference, and not the VB class and file which defines the form. Form modules should be named
as per the section “Module Names”, above.)
Rationale: Consistency with standard Visual Basic and Windows API naming conventions.
Allows easy identification of the type and role of a variable.
7.6 Miscellaneous
S) Do not use abbreviations or acronyms unless they are extremely popular abbreviations in the
project. The prefix standards described above or established by the project are examples of
popular abbreviations, which are exempted.
Rationale: Abbreviations and acronyms make the code harder to understand.
Example:
sOutstandingBalance instead of sCurOutBal
S) Document cases where the client ignores the return value.
Rationale: This is typically an error; if not, make the intent clear.
S) Use plural for collections and singular for individual objects.
Rationale: This makes the names easier to understand.
Examples: