VBSCRIPT CODING CONVENTIONS

What Are Coding Conventions?


Coding conventions are suggestions that help you write code using Microsoft Visual Basic Scripting Edition.

Coding conventions can include the following:
  •     Naming conventions for objects, variables, and procedures
  •     Commenting conventions
  •     Text formatting and indenting guidelines

The main reason for using a consistent set of coding conventions is to standardize the structure and coding style of a script .

Using good coding conventions results in precise, readable, and unambiguous source code that is consistent with other language conventions and as intuitive as possible.

Constant Naming Conventions


 Constants were implemented as variables and distinguished from other variables using all uppercase characters.

Multiple words were separated using the underscore (_) character.

For example:

 USER_LIST_MAX

 NEW_LINE

By using an alternative naming scheme, we can create true constants using the Const statement. This convention uses a mixed-case format in which constant names have a "con" prefix.

For example: conYourOwnConstant

Variable Naming Conventions:


For purposes of readability and consistency,  we can use the following prefixes with descriptive names for variables in our VBScript code.

 SUBTYPE       PREFIX      EXAMPLE
Boolean           bln             blnFound
Byte                 byt             bytRasterData
Date (Time)    dtm             dtmStart
Double            dbl              dblTolerance
Error              err               errOrderNum
Integer            int               intQuantity
Long               lng               lngDistance
Object             obj               objCurrent
Single             sng               sngAverage
String             str                strFirstName

Object Naming Conventions

Object type                    Prefix                       Example

Check box                      chk                          chkReadOnly

Combo box, 

drop-down list box         cbo                    cboEnglish

Command button      cmd                cmdExit

Common dialog         dlg                 dlgFileOpen

Frame                       fra                 fraLanguage

Horizontal scroll bar hsb                hsbVolume

Image                       img                imgIcon

Label                        lbl                  lblHelpMessage

Line                         lin                   linVertical

List Box                    lst                   lstPolicyCodes

Text box                   txt                   txtLastName

Vertical scroll bar     vsb                  vsbRate

Slider                       sld                   sldScale

Code Commenting Conventions

All procedures should begin with a brief comment describing what they do. This description should not describe the implementation details.

Arguments passed to a procedure should be described when their purpose is not obvious and when the procedure expects the arguments to be in a specific range.

Return values for functions and variables that are changed by a procedure, especially through reference arguments, should also be described at the beginning of each procedure.

Formatting Code:

  • Screen space should be conserved as much as possible, while still allowing code formatting to reflect logic structure and nesting.

    •     Standard nested blocks should be indented four spaces.

    •     The overview comments of a procedure should be indented one space.
    •     The highest level statements that follow the overview comments should be indented four spaces, with each nested block indented an additional four spaces.
  • Every important variable declaration should include an inline comment describing the use of the variable being declared.

    •     Variables, controls, and procedures should be named clearly enough that inline comments are only needed for complex implementation details.
    •     At the beginning of  script,  should include an overview that describes the script, enumerating objects, procedures, algorithms, dialog boxes, and other system dependencies. Sometimes a piece of pseudocode describing the algorithm can be helpful.

No comments:

Post a Comment