![]() |
VBScript Coding Conventions![]() ![]() ![]() ![]() ![]()
What Are Coding Conventions?
Coding conventions are suggestions that may help you write code using Microsoft Visual Basic Scripting Edition. Coding conventions can include the following: Naming conventions for objects, variables, and procedures Commenting conventions Text formatting and indenting guidelines 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. Constant Naming Conventions 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: USER_LIST_ MAX NEW_LINE
Descriptive Variable and Procedure Names 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. 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. Object Naming Conventions The following table lists recommended conventions for objects you may encounter while programming VBScript.
Code, Commenting Conventions 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. 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. Procedure header comments should include the following section headings. For examples, see the 'Formatting Your Code" section that follows. Section Heading !Comment Content& Purpose What the procedure does (not how). Assumptions List of any external variable, control, or other element whose state affects this procedure. Effects List of the procedure's effect on each external variable, control, or other element. Inputs Explanation of each argument that Isn't obvious. Each argument should be on a separate line with inline comments. Return Values Explanation of the value returned. Remember the following points: Every important variable declaration should include an inline comment describing the use of the variable being declared. Variables, controls, and procedures should be named clearly enough that inline comments are only needed for complex implementation details. 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.
A Simple VBScript Page
MsgBox "Mirabile visu." End Sub The Sub procedure in the <SCRIPT> tags is an event procedure. There are two parts to the procedure name: the name of the button, Button1 (from the NAME attribute in the <INPUT> tag), and an event name, OnClick. The two names are joined by an underscore(_). Any time the button is clicked, Internet Explorer looks for and runs the corresponding event' procedure, Botton1 OnClick. Pages can use combinations of controls and procedures, too. VBScript and Forms shows some simple interactions. between controls. Other Ways to Attach Code to Events Although the preceding way is probably the simplest and most general, you can attach VBScript code to events in two other ways. Internet Explorer allows you to add short sections of inline code in the tag defining the control. For example, the following <INPUT> tag performs the same action as the previous code example when you click the butt on: <INPUT NAME= "Button1 " TYPE="BUTTON" VALUE= "Click Here" Notice that the function call itself is enclosed in single quotation marks, and the string for the MsgBox function is enclosed in double quotation marks. You can use multiple statements as long as you separate the statements with colons (:). You can also write a <SCRIPT> tag so that it applies only to a particular event for a specific control: <SCRIPT LANGUAGE="VBScript" Because the <SCRIPT> tag already specifies the event and the control, you don't use Sub and End Sub statements.
|
Domain NamesVBSCRIPT VBScript Code VBScript Data Types VBScript Variables constants VBScript Operators Using Conditional Statements Looping Statements in VBScript VBScript Procedures VBScript Coding Conventions VBScript and Forms
Using VBScript with ObjectsWeb DesignWeb HostingE Commerce
|