VBScript Coding Conventions

Source: Internet
Author: User
Tags format array object end naming convention variables variable variable scope
vbscript| Coded |vbscript| encoding

What is a coding convention?

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 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 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 SampleBooleanblnblnfoundbytebytbytrasterdatadate (Time) Dtmdtmstartdoubledbldbltoleranceerrorerrerrordernumintegerintintquantitylonglnglngdistanceobjectobjobjcurrentsinglesngsng Averagestringstrstrfirstname

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 ofA procedure-level event, function, or subroutine can be seen in the head of a script-level HTML page in the process of declaring a variable, visible outside of any procedure in 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 SampleProcess level without dblvelocityscript level ssblncalcinprogress

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 Sample3D Panel pnlpnlgroup animation button anianimailbox check box chkchkreadonly combo box, Drop-down list box cbocboenglish command button cmdcmdexit Common dialog dlgdlgfileopen frame frafralanguage horizontal scroll bar hsbhsbvolume image Imgimgicon label Lbllblhelpmessage line LINL invertical list Box lstlstpolicycodes knob spnspnpages text Box txttxtlastname vertical scroll bar vsbvsbrate slider Sldsldscale

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 ContentThe function of the purpose process (not the method of implementing the function). A list of external variables, controls, or other elements whose state affects this procedure. A list of effects that effect procedures have on each external variable, control, or other element. Enter an explanation of the parameters that are not apparent for each purpose. Each parameter should occupy a single row and have its internal annotations. Returns an 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: 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.