-D-

Coding and Naming Conventions
for VBScript Developers

This appendix outlines a variety of recommended coding and variable naming conventions for VBScript developers. Coding and naming conventions are suggestions or recommendations to standardize the way in which developers implement the most basic of functionality in a language or development environment. Naming and coding conventions can cover topics such as these:

You might wonder why you should bother with using conventions. The main reason is that, by following a standardized set of recommendations, you are making your code easier to read, understand, and maintain--for others as well as yourself. Using standard conventions helps ensure that you are speaking the same "dialect" of the VBScript language as most of your fellow Visual Basic developers.


Using Variable Naming Conventions

Although VBScript has just one primary data type (the Variant type), it has several subtypes. As you might expect, using a standard naming convention for each subtype helps you and fellow developers immediately recognize the subtype of a variable simply by reading its name. VBScript variable naming conventions use standard prefixes. Table D.1 lists the recommended prefixes and an example of each.

Table D.1. VBScript Variable Naming Conventions.
Subtype Prefix Example
Boolean bln blnFound
Byte byt bytPixelData
Date (Time) dtm dtmBegin
Double dbl dblDelta
Error err errOrderNum
Integer int intTotal
Long lng lngDist
Object obj objCurrent
Single sng sngAverage
String str strLastName


Microsoft recommends that you use the prefixes listed in this table along with descriptive names when naming variables in your scripts.


Providing Cues about Variable Scope

Complex VBScripts can grow in size rapidly. When this is the case, it is nice to be able to determine something about the scope of variables in use. VBScript variable scopes occur on two levels:

To provide a simple indication of a variable's scope (without increasing its size too much), Microsoft recommends that you add a single-letter prefix to the names of script-level variables. A procedure-level variable of subtype Double, for example, might be named dblAmplitude (note that no prefix is used). A script-level variable, however, would be prefixed with the letter s. A variable of Boolean subtype might be named sblnErrorOccurred. Naming the variable in this manner gives an instant indication to anyone reading the code that this is a Boolean type with script-level scope.


Using Descriptive Variable and Procedure Naming

A few guidelines for naming variables and procedures in your scripts follow:


Using Object Naming Conventions

Using a standard naming convention for various objects referenced in your scripts helps you and fellow developers immediately recognize the object type simply by reading its name. Just as with variable naming, VBScript object naming conventions use standard prefixes. Table D.2 lists the recommended prefixes and an example of each.

Table D.2. VBScript Object Naming Conventions.
Object Type Prefix Example
3D panel pnl pnlGroup
Animated button ani aniUserMailBox
Checkbox chk chkReadWrite
Combo box, cbo cboEnglish
drop-down listbox
Command button cmd cmdExit
Common dialog box dlg dlgFileOpen
Frame fra fraLanguage
Horizontal scrollbar hsb hsbSoundLevel
Image img imgFTPIcon
Label lbl lblWarningMessage
Line lin linHorizontal
Listbox lst lstProductIDs
Slider sld sldPercentage
Spin spn spnPages
Textbox txt txtFirstName
Vertical scrollbar vsb vsbRate


Microsoft recommends that you use the prefixes listed in this table along with descriptive names when naming objects in your scripts.


Using Constant Naming Conventions

To easily differentiate constants from variables in your scripts, Microsoft recommends that you make constant names all uppercase and that you use the underscore (_) character between words if the name consists of more than one word. You might use MAX_LIST_INDEX, for example, to specify a constant that has the value of the highest index in an array used in the script.


Commenting Your Code

Anyone who has had the responsibility of maintaining code developed by someone else will attest to the fact that poorly documented and commented code often just exacerbates an already difficult situation. Instead of focusing on fixing bugs or enhancing functionality, code maintainers often are disproportionately concerned with just figuring out what the existing code is supposed to be doing. Therefore, I highly recommend that you get in the habit of commenting your code consistently and accurately.

A few guidelines for commenting your scripts follow:


Table D.3. Recommended Contents for Script HeaderSections
.
Header Section Description of Contents
Purpose Describes what the procedure does--not a detailed account of how
Assumptions Lists any external variables, controls, or other elements whose state affects this procedure
Effects Describes the effect the procedure has on each external variable, control, or other element
Inputs Explains arguments that are not obvious; each argument should be on a separate line with inline comments
Return Values Explains the value returned


Using VBScript in HTML Documents

Here are a few guidelines that Microsoft recommends for using VBScript in your HTML documents:

Listing D.1 shows a template that demonstrates these guidelines for using VBScript in HTML documents.

Listing D.1. Using VBScript in HTML documents.

<! Template for VBScript enabled pages >
<HTML>
<HEAD>
<TITLE> </TITLE>
<SCRIPT LANGUAGE="VBScript">
<!
        Function WeCanDeliver (Dt)
                WeCanDeliver = (Cdate(Dt) - Now()) > 2
        End Function
>
</SCRIPT>
</HEAD>
<BODY>
<FONT FACE=ARIAL SIZE=2> <! global default >

... your HTML content goes here ...

</FONT>
</BODY>
</HTML>