VBScript Coding Conventions

Domain Hosting image
Web Hosting
Dedicated server
ssl certificate
Web Design image
Email
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

Subtype Prefix Example
BooleanBinbinFound
Byte Byt bytRasterData
Date(Time) Dtm DtmStart
Double Dbl DblTolerance
Error Err ErrOrderNum
Integer Int IntQuantity
Long Lng LngDistnace
Object Obj ObjCurrent
Single Sng SngAverage
String Str StrFirstName

Variable Scope Prefixes

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.

Scope Prefix Example
Procedure level NoneDblVelocity
Script level S SbInCalclnProgress

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.

Object typePrefix Example
3D Panel pnI pnIGroup
Animated button ani aniMailBox
Check box chk chkReadOnly
Command button cmd cmdExit
Common dialog dlg dlgFileOpen
Frame fra fralanguage
Horizontal scroll bar hsb hsbVolume
Image img imgIcon
Label lbl lblHeIpMessage
Line lin linVertical
List Box lst lstPolicyCodes
Spin spn spnPages
Text box txt txtLastName
Slider sld sldScale

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

A Simple Page With Microsoft Internet Explorer, you can view the page produced by the following HTML code. If you click the button on the page, you see VBScript in action. <HTML>
<HEAD>
<TITLE>A Simple First Page</TITLE>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub Button1_OnClick

MsgBox "Mirabile visu."

End Sub
-->
</SCRIPT>
</HEAD>
<BODY>
<H3>A Simple First Page</H3><HR>
<FORM>
<INPUT NAME="Button1"
TYPE="BUTTON" VALUE="Click Here">
</FORM>
</BODY> </HTML>

The result is a little underwhelming: a dialog box displays a Latin phrase ("Wonderful to behold"). However, there's quite a bit going on. When Internet Explorer reads the page, it finds the <SCRIPT> tags, recognizes there is a piece of VBScript code, and saves the code. When you click the button, Internet Explorer makes the connection between the button and the code, and runs the procedure.
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"
OnClick='MsgBox "Mirabile visu."'>

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"
EVENT="OnClick" FOR="Button1">
<!-
MsgBox 'Mirabile visu."
->
</SCRIPT>

Because the <SCRIPT> tag already specifies the event and the control, you don't use Sub and End Sub statements.



Domain Name Search

www.


Copyright (C) 2007. Web Domain design hosting. All rights reserved.