Coding Conventions

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.

Principles

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.

Some Specifics

Follow these guidelines when you write code:

  • Define all variables explicitly. Checking the Require Variable Declaration option under Tools/Options/Editor enforces this. Define variable-length arrays explicitly. (Visual Basic will not enforce this.)

  • Always specify the upper and lower bounds in an array definition.

  • Define constants, and use them in place of numbers in the code.

  • Always use the ByVal and ByRef keywords in parameter lists. Use ByVal where you have a choice.

  • Always use the Private keyword on functions and subroutines that are not exported from a module.

  • Define the types of all variables and function and subroutine parameters explicitly. For example, use Dim nBufSize As Integer instead of Dim nBufSize, and use Function nGetChars(nBufSize As Integer) As Integer instead of Function nGetChars(nBufSize).

  • Do not use implicit type suffixes (such as ! and &) in variable definitions.

  • In For loops, use Next <Index> instead of just Next.

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.

Miscellaneous Conventions

This section describes some other conventions worth noting.

Dimensioning variables

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.

Defining arrays

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.

Using control arrays

Control arrays must be used whenever possible and whenever appropriate in Visual Basic. These make smaller executables, which therefore lead to better performance.

Using operators

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.

Comparing strings

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.

Using parentheses

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.

Initializing variables

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 

Calling subroutines

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.

Setting control properties at design time vs. run time

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

Using For...Next statements

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.

Using the Me statement

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.

Optional parameters

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.



Ltd Mandelbrot Set International Advanced Microsoft Visual Basics 6. 0
Advanced Microsoft Visual Basic (Mps)
ISBN: 1572318937
EAN: 2147483647
Year: 1997
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net