VBScript Basic tutorial Ten VBScript coding conventions

Source: Internet
Author: User
Tags format comments constant html page naming convention variables variable variable scope
vbscript| Coding | Basic Tutorials 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 guide

The main reason for using a consistent coding convention is to standardize the structure and coding style of script or script sets 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 one
The Convention uses a mixed-case format and is prefixed with "con" as a constant name. For example:

Conyourownconstant

Variable Naming conventions

For readability and consistency purposes, use the following variable naming conventions in VBScript code:

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


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 event, function, or child procedure Visible in the process of declaring a variable
Script level The head part of an HTML page, beyond 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 Example
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 keep consistent throughout the Script. For example, switching Cnt and Count randomly in a 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 Example
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.
• An overview of the script should be included at the beginning of the script, enumerating objects, procedures, algorithms, dialogs, and other system dependencies. Sometimes it is useful to have a dummy code that describes the algorithms.

Formatting code

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

• Standard nested blocks should be indented 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 is indented 4 spaces. For example:

'*********************************************************
' Purpose: Returns the position that the specified user first appears in the UserList array.
' Input: Struserlist (): The list of users you are looking for.
' Strtargetuser: The name of the user to find.
' Returns: The index of the first occurrence of the strtargetuser in the struserlist array.
' If the target user is not found, return-1.
'*********************************************************

Function Intfinduser (Struserlist (), Strtargetuser)
Dim i ' Loop counter.
Dim BlnFound ' discovers the target's tag.
Intfinduser =-1
i = 0 ' Initialize the loop counter.
Do-I <= Ubound (struserlist) and not BlnFound
If struserlist (i) = Strtargetuser Then
BlnFound = True ' flag set to True.
The Intfinduser = I ' return value is set to the loop counter.
End If
i = i + 1 ' Loop counter plus 1.
Loop
End 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.