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 declaration variables at the point of visibility
Procedure-level events, functions, or subroutines are visible in the process of declaring a variable
The head portion of the script-level HTML page, which is visible in all procedures of the script outside of any process
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 without dblvelocity
Script class 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):
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
Frame fra Fralanguage
Horizontal scroll bar HSB Hsbvolume
Image img Imgicon
Label LBL Lblhelpmessage
Straight Lin Linvertical
list box LST Lstpolicycodes
Knob SPN Spnpages
text Box txt Txtlastname
Vertical scroll bar VSB vsbrate
Slide 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 header annotation Content
The 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.
• 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 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:
• 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
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.