Writing Solid Script

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Scripting languages are "loosely typed" and, therefore, all variables used in script have a Variant data type. In addition, script is written directly into the HTML code behind a Web page, and there are no modules used to contain code as there are in VBA and other "strongly typed" languages. Finally, scripting languages do not require you to expressly declare variables before you use them.

Given these unique characteristics, it still makes sense to talk about a naming convention in the context of writing script. The naming conventions and other coding guidelines apply to script in an HTML page as they do to Microsoft® Visual Basic® for Applications (VBA) code in a Microsoft® Office application. The benefits associated with writing reusable, understandable, and maintainable code can be realized whether you are writing script or VBA code. In fact, there is a great deal of work to be done persuading script developers to pay attention to issues of code reuse and maintainability. There are just as many benefits to writing solid script as there are to writing solid code.

Although script is written directly into the HTML code of a Web page, questions of visibility and lifetime are still important. Variables and constants declared within a procedure are local to that procedure and have a lifetime that lasts only as long as the script within the procedure is executing.

Variables and constants declared in script outside a procedure are visible to any script contained in the current HTML page. These variables have the equivalent of the module-level scope described earlier. Variables and constants declared in Visual Basic Scripting Edition (VBScript) by using the Public keyword are visible to all script in the current HTML page and to all script in all other currently loaded pages. For example, if you have an HTML page that contains multiple frames designated by a <FRAMESET> tag pair, a variable or constant declared with the Public keyword will be visible to all pages loaded within all the frames specified by the <FRAMESET> tag.

In addition, although all script variables have a Variant data type, it is important to remember that the Variant data type encompasses many different data types and can coerce a variable to the most appropriate data type in a particular context. Although you cannot declare a variable as a specific data type, you should name your variables as if you could. Naming script variables as if they were strongly typed will not prevent you from assigning an integer value to the strCompanyName variable, but it will force you to think about how the variable is used and the data subtype it will contain. You declare VBScript variables by using the Dim statement and Microsoft® JScript® variables by using the var statement.

Note   Unlike VBScript, JScript is a case-sensitive language; if you name a variable

strCompanyName

but refer to it as

STRCompanyName

, you will encounter errors.

Using the Option Explicit Statement

Neither VBScript nor VBA requires you to declare variables before using them. The default behavior in both languages makes it possible for you to create variables by simply using a variable name in an assignment statement. However, the failure to use the Option Explicit statement to force explicit variable declaration can be a serious mistake. Using undeclared variables can introduce subtle, hard-to-find bugs into your code that are easily avoided by using this simple technique.

To force VBA to insert the Option Explicit statement in every module you create, open the Visual Basic Editor, click Options on the Tools menu, and then click Require Variable Declaration on the Editor tab.

To force variables to be declared in VBScript, type Option Explicit immediately after the first <SCRIPT> tag in your HTML document. For example, <SCRIPT> Option Explicit </SCRIPT>.

See Also

Writing Solid Code | Using a Naming Convention | Structuring and Formatting Your Code | Commenting Code | Designing Code to Be Used Again