VBS TUTORIAL: VBScript base-vbscript Coding Conventions _vbs

Source: Internet
Author: User
Tags comments constant mixed naming convention variable scope

VBScript Coding Conventions

Coding conventions are some of the recommendations that help you write code using Microsoft Visual Basic scripting Edition. The encoding convention contains the following:

    • Naming conventions for objects, variables, and procedures
    • Annotation conventions
    • Text formatting and indenting guides

The main reason for using a consistent coding convention is to standardize the structure and coding style of a script or script set so that the code is easy to read and understand. Using good coding conventions makes the source code clear, readable, accurate, intuitive, and consistent with other language conventions.

Constant naming convention

Earlier versions of VBScript did not allow the creation of user custom constants. If you want to use constants, constants are implemented as variables, and all letters are capitalized to distinguish them from other variables. Multiple words in a constant name are separated by an underscore (_). For example:

 USER_LIST_MAX NEW_LINE

This method of identifying constants is still possible, but you can also choose a different scenario and create a true constant with the Const statement. This convention uses a mixed-case format and is prefixed with "con" as a constant name. For example:

 conYourOwnConstant

Variable Naming conventions

To improve readability and consistency, use the following variable naming conventions in VBScript code:

Sub Type prefix Sample
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

Variable scope

Variables should be defined in as small a scope as possible. The scope of the VBScript variable is as follows:

Scope declaring variables at Visibility of
Process level An event, function, or child procedure. Visible in the process of declaring a variable.
Script level The head part of the HTML page, outside of any process. Visible in all procedures of the script.

Variable scope prefix

As the length of the script code increases, it is necessary to quickly differentiate the scope of the variable. Adding a single character prefix before the type prefix can accomplish this without causing the variable name to be too long.

Scope prefix Sample
Process level No Dblvelocity
Script level S Sblncalcinprogress

Descriptive variable name and procedure name

The body of the variable or procedure name should be mixed with the case, and describe its purpose as fully as possible. In addition, the procedure name should begin with a verb, such as initnamearray or Closedialog.

For frequently used or longer names, it is recommended that the standard abbreviation be used to keep the name within the appropriate length. Variable names that are usually more than 32 characters become difficult to read. When using abbreviations, be sure to be consistent throughout the script. For example, switching Cnt and Count at random in one script or script set creates confusion.

Object Naming conventions

The following table lists the object naming conventions that may be used in VBScript (recommended):

Object Type prefix Sample
3D Panel PnL Pnlgroup
Animation buttons Ani Animailbox
check box Chk Chkreadonly
combo box, drop-down list box Cbo Cboenglish
command button Cmd Cmdexit
Public dialog box Dlg Dlgfileopen
Framework Fra Fralanguage
Horizontal scroll bar Hsb Hsbvolume
Image Img Imgicon
Label LbL Lblhelpmessage
Linear Lin Linvertical
list box Lst Lstpolicycodes
Knob Spn Spnpages
text box Txt Txtlastname
Vertical scroll bar Vsb Vsbrate
Sliding block Sld Sldscale

Code Comment Conventions

The beginning of all procedures should have a brief comment describing their functionality. These comments do not describe details (how to implement functionality), because the details are sometimes changed frequently. This avoids unnecessary annotation maintenance work and incorrect annotations. Detail information is described by the code itself and the necessary internal annotations.

This should be explained when the parameters passed to the process are not intended to be used, or if the procedure is required for the range of parameters. If a procedure changes the return value of a function and a variable (especially by a parameter reference), the return value should also be described at the beginning of the procedure.

The comments in the beginning of the procedure should contain the following section headings. See the "Formatting Code" section later in this example.

section title Comment Content
Objective The function of the procedure (not the method of implementing the function).
Assume A list of external variables, controls, or other elements whose state affects this procedure.
Effect A list of effects that a procedure has on each external variable, control, or other element.
Input An explanation of the parameters that are not apparent for each purpose. Each parameter should occupy a single row and have its internal annotations.
Return The explanation of the return value.

Please keep the following points in mind:

    • Each important variable declaration should have an internal annotation that describes the purpose of the variable.
    • variables, controls, and procedures should be clearly named, and internal annotations are required only when describing complex details.
    • You should include an overview of the script at the beginning of the script, enumerating objects, procedures, algorithms, dialog boxes, and other system dependencies. Sometimes it is useful to have a dummy code that describes the algorithms.

Formatting code

You should retain as much screen space as possible, but still allow the logical structure and nesting to be reflected in code format. Here are some tips:

    • A standard nested block should indent 4 spaces.
    • An overview comment for a procedure should indent 1 spaces.
    • The top-level statement after an overview annotation should indent 4 spaces, and each layer of nested blocks indents 4 spaces. For example:
' ********************************************************* ' purpose:locates the occurrence a specified user ' & nbsp;         in the userlist array. ' Inputs:struserlist (): The list of users to be searched.          strtargetuser:the name of the user to search for. ' Returns:the Index of the the the occurrence of the Strtargetuser '           I n the struserlist array. '           If The target user is not found, return-1. ' Function Intfinduser (Struserlist (), Strtargetuser)    dim i    ' Loop counter.   dim blnfound    ' Target Found Flag   intfinduser = -1   i = 0    ' Initialize loop counter & Nbsp; do while I <= Ubound (struserlist) and not Blnfound      if struserlist (i) = Strtargetuser Then           blnfound = true    ' Set flag to true          intfinduser = i    ' Set return value to loop count       end if      i = i + 1    ' Increment loop counter    LoopEnd Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.