Microsoft Office 2000/Visual Basic Programmer's Guide |
Scripting languages are "loosely typed" and as a consequence, 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 that you expressly declare variables before you use them.
Given these unique characteristics, does it still make sense to talk about a naming convention in the context of writing script? Absolutely!
The naming conventions and other coding guidelines discussed in this chapter apply just as well to script in an HTML page as they do to VBA code in an 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 probably more 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.
Even though 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 so 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 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 had an HTML page that contained multiple frames designated by a <FRAMESET> tag pair, a variable or constant declared with the Public keyword would 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.
Neither VBScript nor VBA requires you to declare variables before using them. The default behavior in both languages is to allow you to create variables by simply using a variable name in an assignment statement.
However, every experienced developer knows that the failure to use the Option Explicit statement to force explicit variable declaration is a coding blunder of the highest order. Using undeclared variables can introduce subtle, hard-to-find bugs into your code that are easily avoided by using this one, 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.