This section outlines a number of conventions we have found useful when writing code. It isn't comprehensive—our in-house manual, VB Vogue, contains much more detail than we have space for here. We suggest you use this section as a basis for designing your own coding standards.
Always code for clarity, not efficiency. Choose variable and function names carefully. Be verbose rather than terse, and parenthesize expressions to make them clearer. Write your code for the reader.
Limit the scope of constants, variables, functions, and subroutines as much as possible. If you have the choice, always use a local object in preference to a global one.
Use explicit type conversions (CInt, CStr, and so on) when assigning between different variable types.
Follow these guidelines when you write code:
Follow the general principle of restricting the scope of variables and functions to the barest minimum possible. However, be sure to use common sense. For example, a procedure that logically serves only one form should be "contained" within that form. Likewise, a procedure that could logically serve many forms should be placed in a module, even though it might currently be called only from one form.
This section describes some other conventions worth noting.
Variables should each be dimensioned individually, meaning there should be only one Dim, Public, Private, or Static per variable. Multiple dimensions on a line can lead to ambiguity about the scope and type of a variable.
Both the upper and lower bounds in array definitions must be specified. Do not rely on the Option Base setting (Option Base should not be used).
Public anDigits(10) As Integer
This dimensions 11 entries in the array if the Option Base is equal to 0, which is its default.
Public anDigits(1 to 10) As Integer
This dimensions 10 entries in the array, regardless of the Option Base setting, and is the way to dimension arrays.
Control arrays must be used whenever possible and whenever appropriate in Visual Basic. These make smaller executables, which therefore lead to better performance.
Use only the addition operator (+) in numerical expressions. Do not use it to facilitate some string concatenation. This is especially true if using variables of type Variant.
StrComp is by far the fastest way to compare strings. Use it instead of a language operator (such as =) whenever possible. Binary Comparison is faster than Text Comparison (especially if you're using the Like operator), and should be used whenever it is appropriate. Generally, using fixed strings is slower, with the exception of passing strings to API or DLL procedures.
While a parenthesized expression might be slower to evaluate than a non-parenthesized expression, parentheses should be applied where appropriate to ensure the readability of code.
Initialize variables through assignment. Initialization or pre-assignment (in the case of Visual Basic) of variables is a good programming habit to get into.
Initialize variables as close as possible to where they are used. This avoids having to hunt for the initialization:
Dim nCount As Integer : nCount = 1 ' for counting iterations
or
Dim nCount As Integer ' for counting iterations nCount = 1 ' initialized
The Call keyword should prefix any call to a subroutine, since it makes explicit what is happening in the code. Using Call also avoids the problem of an apparent parameter being merely evaluated.
As a general rule, it is a good idea to do as much in the code as possible. For example, the Mask property of a masked-edit control is best set "in code" at run time rather than at design time, because setting it in code generally makes clearer what is going on. This method also makes code more portable (because operational logic is not tied to any form property, such as in the previous example, the code is more portable and generic).
In For loops, use Next <counter> instead of just Next. In For Each loops (VBA), use Next <element> instead of just Next. These recommendations are faster.
For...Next loops are more than twice as fast as any equivalent Do...Loop and While....Wend loops. Not surprisingly, Integer counters are the fastest of all the variable types to use as a counter.
The Me statement should be used whenever possible in the code. Where code in an object module (class or form) refers to objects in the same module, use Me rather than the object's name to make the reference implicit, and be assured the code will work with multiple instances of the object.
NOTE
It is not possible to use Me when setting an instance of a class or form object to Nothing.
Visual Basic's typed optional parameters are faster than untyped (Variant) optional parameters. However, note that IsMissing does not work with typed optional parameters, only with optional Variant parameters.