VBScript tutorial Lesson 10 VBScript coding Convention

Source: Internet
Author: User

What is encoding conventions?

Coding conventions are some suggestions to help you write code using Microsoft Visual Basic Scripting Edition. Encoding conventions include the following:

· Naming conventions for objects, variables, and processes
· Annotations
· Guide to text format and indentation

The main reason for using consistent encoding conventions is to standardize the structure and encoding style of the Script or Script set so that the code is easy to read and understand. A good coding convention can make the source code clear, easy to read, accurate, more intuitive and consistent with other language conventions.

Constant naming convention

User-Defined constants cannot be created in earlier versions of VBScript. To use constants, constants are implemented in the form of variables, and all letters are capitalized to distinguish them from other variables. Multiple words in a constant name are separated by underscores. For example:

USER_LIST_MAX
NEW_LINE

This method of identifying constants is still feasible, but you can choose other schemes to use the Const statement to create true constants. This
Conventions use case-insensitive format and use "con" as the prefix of the constant name. For example:

ConYourOwnConstant

Variable naming conventions

For easier reading and consistency, use the following variable naming conventions in 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

Variable Scope

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

Visibility of scope declaration Variables
Process-level events, functions, or sub-processes are visible During Variable declaration.
The HEAD part of the Script-level HTML page, which is visible outside of any process in all processes of the Script

Variable scope prefix

As the length of Script code increases, it is necessary to quickly differentiate the scope of variables. You can add a single-character prefix before the type prefix to achieve this without causing the variable name to be too long.

Example of scope prefix
Process-level dblVelocity
Script-level s sblnCalcInProgress

Descriptive variable name and process name

The subject of the variable name or process name should be in case-insensitive format, and its purpose should be described as full as possible. In addition, the process name should start with a verb, such as InitNameArray or CloseDialog.

For frequently used or long names, we recommend that you use standard abbreviations to keep the names within the appropriate length. Generally, variable names with more than 32 characters become hard to read. Make sure that the abbreviations are consistent throughout the Script. For example, switching Cnt and Count randomly in a Script or Script set can cause confusion.

Object naming conventions

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

Example of object type prefix
3D panel pnl pnlGroup
Animation button 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
Lin linVertical
List box lst lstPolicyCodes
SpnPages
Text Box txt lastname
Vertical scroll bar vsb vsbRate
Slider sld sldScale

Code comments

The start part of all processes should have a brief comment describing its functions. These annotations do not describe the details (how to implement the function), because the details are sometimes changed frequently. In this way, unnecessary annotation maintenance work and incorrect annotations can be avoided. Details are described by the Code and necessary internal comments.

When the purpose of the parameters passed to the process is not obvious, or the process has requirements on the parameter value range, it should be described. If the process changes the return values of functions and variables (especially through parameter reference), the return values should also be described in the beginning of the process.

The comments at the beginning of the process should contain the following section titles. For related examples, see the "format code" section.

Section title comment
The function of the target process (not the method for implementing the function ).
Assume that its status affects the list of external variables, controls, or other elements of the process.
List of effects that an effect process has on each external variable, control, or other element.
Enter an explanation of the parameters that are not obvious for each purpose. Each parameter must have a separate line with its internal comments.
Returns an explanation of the returned value.

Remember the following:

· Each important variable declaration should have an internal comment describing the purpose of the variable.
· Variables, controls, and processes should be clearly named, and internal comments should be needed only to describe complicated details.
· An overview of the Script should be included at the beginning of the Script to list objects, processes, calculation rules, dialog boxes, and other system sub-objects. Sometimes a piece of false code describing the algorithm is very useful.

Format code

Screen space should be kept as much as possible, but the logic structure and nesting should still be reflected in code format. The following are some tips:

· Four spaces should be indented for standard nested blocks.
· The process overview comment should be indented with one space.
· The top-level statement after the comment should be indented with four spaces, and each layer of nested blocks should be indented with four spaces. For example:

'*************************************** ******************
'Purpose: return the location where the specified user first appears in the UserList array.
'Input: strUserList (): list of users to be searched.
'Strtargetuser: the user name to be searched.
'Return: The index of the first appearance of strTargetUser in the strUserList array.
'If the target user is not found,-1 is returned.
'*************************************** ******************

Function intFindUser (strUserList (), strTargetUser)
Dim I 'cycle counter.
Dim blnFound 'indicates the target.
IntFindUser =-1
I = 0' initialize the cyclic counter.
Do While I <= Ubound (strUserList) and Not blnFound
If strUserList (I) = strTargetUser Then
BlnFound = true' flag is set to True.
IntFindUser = I 'returned value is set as a cyclic counter.
End If
I = I + 1 'cycle 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.