📄 vbs3.htm
字号:
<HTML>
<HEAD>
<TITLE>Coding Conventions</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso8859-1">
<META NAME="MS.LOCALE" CONTENT="EN-US">
<META NAME="PRODUCT" CONTENT="Visual Basic Scripting Edition">
<META NAME="TECHNOLOGY" CONTENT="SCRIPTING">
<META NAME="CATEGORY" CONTENT="Tutorial">
<META NAME="Description" CONTENT="VBScript Coding Conventions">
</HEAD>
<BODY BGCOLOR=FFFFFF LINK=#0033CC>
<!--TOOLBAR_START-->
<!--TOOLBAR_EXEMPT-->
<!--TOOLBAR_END-->
<FONT FACE="Verdana, Arial, Helvetica" SIZE=2>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%><TR VALIGN=TOP><TD WIDTH=360>
<FONT SIZE=1 COLOR=#660033>Microsoft® Visual Basic® Scripting Edition</FONT><BR>
<FONT SIZE=5 COLOR=#660033><B>VBScript Coding Conventions</B></FONT>
</TD>
<TD ALIGN=RIGHT>
<FONT SIZE=2> <A HREF="vbstutor.htm">VBScript Tutorial</A> <BR> <A HREF="vbs14.htm">Previous</A> <P></FONT>
</TD></TR>
</TABLE>
<BR>
<HR NOSHADE SIZE=1>
<H5>What Are Coding Conventions?</H5>
<BLOCKQUOTE>
Coding conventions are suggestions that may help you write code using Microsoft Visual Basic Scripting Edition. Coding
conventions can include the following:
<UL>
<LI>Naming conventions for objects, variables, and procedures
<LI>Commenting conventions
<LI>Text formatting and indenting guidelines
</UL>
The main reason for using a consistent set of coding conventions is to standardize the structure and coding style of a script or set of scripts
so that you and others can easily read and understand the code. Using good coding conventions results in precise, readable, and unambiguous source code that is consistent with other language conventions and as intuitive as possible.
</BLOCKQUOTE>
<H5>Constant Naming Conventions</H5>
<BLOCKQUOTE>
Earlier versions of VBScript had no mechanism for creating user-defined constants. Constants, if used, were implemented as variables and distinguished from other variables using all uppercase characters. Multiple words were separated using the underscore (_) character. For example:
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> USER_LIST_MAX
NEW_LINE
</FONT></PRE></BLOCKQUOTE>
While this is still an acceptable way to indentify your constants, you may want to use an alternative naming scheme, now that you can create true constants using the <a href="vbs569.htm"><b>Const</b></a> statement. This convention uses a mixed-case format in which constant names have a "con" prefix. For example:
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> conYourOwnConstant
</FONT></PRE></BLOCKQUOTE>
</BLOCKQUOTE>
<H5>Variable Naming Conventions</H5>
<BLOCKQUOTE>
For purposes of readability and consistency, use the following prefixes with descriptive names for variables in your VBScript code.<P>
<TABLE WIDTH=87% BORDER=1 CELLPADDING=5 CELLSPACING=0>
<TR BGCOLOR=#DDDDDD VALIGN=TOP>
<TD><FONT SIZE=2><B>Subtype</B></FONT></TD>
<TD><FONT SIZE=2><B>Prefix</B></FONT></TD>
<TD><FONT SIZE=2><B>Example</B></FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Boolean</FONT></TD>
<TD><FONT SIZE=2>bln</FONT></TD>
<TD><FONT SIZE=2>blnFound</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Byte</FONT></TD>
<TD><FONT SIZE=2>byt</FONT></TD>
<TD><FONT SIZE=2>bytRasterData</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Date (Time)</FONT></TD>
<TD><FONT SIZE=2>dtm</FONT></TD>
<TD><FONT SIZE=2>dtmStart</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Double</FONT></TD>
<TD><FONT SIZE=2>dbl</FONT></TD>
<TD><FONT SIZE=2>dblTolerance</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Error</FONT></TD>
<TD><FONT SIZE=2>err</FONT></TD>
<TD><FONT SIZE=2>errOrderNum</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Integer</FONT></TD>
<TD><FONT SIZE=2>int</FONT></TD>
<TD><FONT SIZE=2>intQuantity</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Long</FONT></TD>
<TD><FONT SIZE=2>lng</FONT></TD>
<TD><FONT SIZE=2>lngDistance</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Object</FONT></TD>
<TD><FONT SIZE=2>obj</FONT></TD>
<TD><FONT SIZE=2>objCurrent</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Single</FONT></TD>
<TD><FONT SIZE=2>sng</FONT></TD>
<TD><FONT SIZE=2>sngAverage</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>String</FONT></TD>
<TD><FONT SIZE=2>str</FONT></TD>
<TD><FONT SIZE=2>strFirstName</FONT></TD></TR>
</TABLE></BLOCKQUOTE>
<H5>Variable Scope</H5>
<BLOCKQUOTE>
Variables should always be defined with the smallest scope possible. VBScript <A HREF="vbs15.htm">variables</A> can have the following scope.<P>
<TABLE WIDTH=87% BORDER=1 CELLPADDING=5 CELLSPACING=0>
<TR BGCOLOR=#DDDDDD VALIGN=TOP>
<TD><FONT SIZE=2><B>Scope</B></FONT></TD>
<TD><FONT SIZE=2><B>Where Variable Is Declared</B></FONT></TD>
<TD><FONT SIZE=2><B>Visibility</B></FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Procedure-level</FONT></TD>
<TD><FONT SIZE=2>Event, Function, or Sub procedure</FONT></TD>
<TD><FONT SIZE=2>Visible in the procedure in which it is declared</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Script-level</FONT></TD>
<TD><FONT SIZE=2>HEAD section of an HTML page, outside any procedure</FONT></TD>
<TD><FONT SIZE=2>Visible in every procedure in the script</FONT></TD></TR>
</TABLE>
<H5>Variable Scope Prefixes</H5>
As script size grows, so does the value of being able to quickly differentiate the scope of variables. A one-letter scope prefix preceding the
type prefix provides this, without unduly increasing the size of variable names.<P>
<TABLE WIDTH=87% BORDER=1 CELLPADDING=5 CELLSPACING=0>
<TR BGCOLOR=#DDDDDD VALIGN=TOP>
<TD><FONT SIZE=2><B>Scope</B></FONT></TD>
<TD><FONT SIZE=2><B>Prefix</B></FONT></TD>
<TD><FONT SIZE=2><B>Example</B></FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Procedure-level</FONT></TD>
<TD><FONT SIZE=2>None</FONT></TD>
<TD><FONT SIZE=2>dblVelocity</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Script-level</FONT></TD>
<TD><FONT SIZE=2>s</FONT></TD>
<TD><FONT SIZE=2>sblnCalcInProgress</FONT></TD></TR>
</TABLE>
</BLOCKQUOTE>
<H5>Descriptive Variable and Procedure Names</H5>
<BLOCKQUOTE>
The body of a variable or procedure name should use mixed case and should be as complete as necessary to describe its purpose.
In addition, procedure names should begin with a verb, such as InitNameArray or CloseDialog.<P>
For frequently used or long terms, standard abbreviations are recommended to help keep name length reasonable.
In general, variable names greater than 32 characters can be difficult to read. When using abbreviations, make sure they are consistent throughout the entire script. For example, randomly switching between Cnt and Count within a script or set of scripts may lead to confusion.
</BLOCKQUOTE>
<H5>Object Naming Conventions </H5>
<BLOCKQUOTE>
The following table lists recommended conventions for objects you may encounter while programming VBScript.<P>
<TABLE WIDTH=87% BORDER=1 CELLPADDING=5 CELLSPACING=0>
<TR BGCOLOR=#DDDDDD VALIGN=TOP>
<TD><FONT SIZE=2><B>Object type</B></FONT></TD>
<TD><FONT SIZE=2><B>Prefix</B></FONT></TD>
<TD><FONT SIZE=2><B>Example</B></FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>3D Panel</FONT></TD>
<TD><FONT SIZE=2>pnl</FONT></TD>
<TD><FONT SIZE=2>pnlGroup</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Animated button</FONT></TD>
<TD><FONT SIZE=2>ani</FONT></TD>
<TD><FONT SIZE=2>aniMailBox</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Check box</FONT></TD>
<TD><FONT SIZE=2>chk</FONT></TD>
<TD><FONT SIZE=2>chkReadOnly</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Combo box, drop-down list box</FONT></TD>
<TD><FONT SIZE=2>cbo</FONT></TD>
<TD><FONT SIZE=2>cboEnglish</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Command button</FONT></TD>
<TD><FONT SIZE=2>cmd</FONT></TD>
<TD><FONT SIZE=2>cmdExit</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Common dialog</FONT></TD>
<TD><FONT SIZE=2>dlg</FONT></TD>
<TD><FONT SIZE=2>dlgFileOpen</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Frame</FONT></TD>
<TD><FONT SIZE=2>fra</FONT></TD>
<TD><FONT SIZE=2>fraLanguage</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Horizontal scroll bar</FONT></TD>
<TD><FONT SIZE=2>hsb</FONT></TD>
<TD><FONT SIZE=2>hsbVolume</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Image</FONT></TD>
<TD><FONT SIZE=2>img</FONT></TD>
<TD><FONT SIZE=2>imgIcon</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Label</FONT></TD>
<TD><FONT SIZE=2>lbl</FONT></TD>
<TD><FONT SIZE=2>lblHelpMessage</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Line</FONT></TD>
<TD><FONT SIZE=2>lin</FONT></TD>
<TD><FONT SIZE=2>linVertical</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>List Box</FONT></TD>
<TD><FONT SIZE=2>lst</FONT></TD>
<TD><FONT SIZE=2>lstPolicyCodes</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Spin</FONT></TD>
<TD><FONT SIZE=2>spn</FONT></TD>
<TD><FONT SIZE=2>spnPages</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Text box</FONT></TD>
<TD><FONT SIZE=2>txt</FONT></TD>
<TD><FONT SIZE=2>txtLastName</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Vertical scroll bar</FONT></TD>
<TD><FONT SIZE=2>vsb</FONT></TD>
<TD><FONT SIZE=2>vsbRate</FONT></TD></TR>
<TR VALIGN=TOP>
<TD><FONT SIZE=2>Slider</FONT></TD>
<TD><FONT SIZE=2>sld</FONT></TD>
<TD><FONT SIZE=2>sldScale</FONT></TD></TR>
</TABLE>
</BLOCKQUOTE>
<H5>Code Commenting Conventions</H5>
<BLOCKQUOTE>
All procedures should begin with a brief comment describing what they do. This description should not describe the implementation details (how it does it) because these often change over time, resulting in unnecessary comment maintenance work, or worse, erroneous comments. The code itself and any necessary inline comments describe the implementation. <P>
Arguments passed to a procedure should be described when their purpose is not obvious and when the procedure expects the arguments
to be in a specific range. Return values for functions and variables that are changed by a procedure, especially through reference
arguments, should also be described at the beginning of each procedure.<P>
Procedure header comments should include the following section headings. For examples, see the "Formatting Your Code" section that follows.<P>
<TABLE WIDTH=87% BORDER=1 CELLPADDING=5 CELLSPACING=0>
<TR BGCOLOR=#DDDDDD VALIGN=TOP>
<TD WIDTH=15%><FONT SIZE=2><B>Section Heading</B></FONT></TD>
<TD><FONT SIZE=2><B>Comment Contents</B></FONT></TD>
<TR VALIGN=TOP>
<TD WIDTH=15%><FONT SIZE=2>Purpose</FONT></TD>
<TD><FONT SIZE=2>What the procedure does (not how).</FONT></TD>
<TR VALIGN=TOP>
<TD WIDTH=15%><FONT SIZE=2>Assumptions</FONT></TD>
<TD><FONT SIZE=2>List of any external variable, control, or other element whose state affects this procedure.</FONT></TD>
<TR VALIGN=TOP>
<TD WIDTH=15%><FONT SIZE=2>Effects</FONT></TD>
<TD><FONT SIZE=2>List of the procedure's effect on each external variable, control, or other element.</FONT></TD>
<TR VALIGN=TOP>
<TD WIDTH=15%><FONT SIZE=2>Inputs</FONT></TD>
<TD><FONT SIZE=2>Explanation of each argument that isn't obvious. Each argument should be on a separate line with inline comments.</FONT></TD>
<TR VALIGN=TOP>
<TD WIDTH=15%><FONT SIZE=2>Return Values</FONT></TD>
<TD><FONT SIZE=2>Explanation of the value returned.</FONT></TD>
</TABLE>
Remember the following points:
<UL>
<LI>Every important variable declaration should include an inline comment describing the use of the variable being declared.
<LI>Variables, controls, and procedures should be named clearly enough that inline comments are only needed for complex implementation details.
<LI>At the beginning of your script, you should include an overview that describes the script, enumerating objects, procedures, algorithms, dialog boxes, and other system dependencies. Sometimes a piece of pseudocode describing the algorithm can be helpful.
</UL>
</BLOCKQUOTE>
<H5>Formatting Your Code</H5>
<BLOCKQUOTE>
Screen space should be conserved as much as possible, while still allowing
code formatting to reflect logic structure and nesting. Here are a few pointers:
<UL>
<LI>Standard nested blocks should be indented four spaces.
<LI>The overview comments of a procedure should be indented one space.
<LI> The highest level statements that follow the overview comments should be indented four spaces, with each nested block indented an additional four spaces. For example:
</UL>
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE =3>
'*********************************************************
' Purpose: Locates the first occurrence of a specified user
' 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 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 ' Target found flag
intFindUser = -1
i = 0 ' Initialize loop counter
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
Loop
End Function
</FONT></PRE></BLOCKQUOTE></BLOCKQUOTE>
<hr noshade size=1>
<p align=center><em><a href="../../common/colegal.htm">© 1997 by Microsoft Corporation. All rights reserved.</a></em></p>
</FONT>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -