Constructing Scripts

Microsoft® Windows® 2000 Scripting Guide

One of the strengths of the telephone directory is that no user manual is required to read and use it; this is because the telephone directory is laid out in a consistent and logical manner. In most U.S. communities, for example, telephone directories have a set of blue pages that contain information about schools and government agencies. These directories always feature a set of white pages that contain residential phone numbers. Phone numbers are sorted by the name of the resident, and are presented in alphabetical order, from A to Z. Telephone directories also contain a set of yellow pages that include business listings, this time ordered by the business category (Hairdressers, Sporting Goods, and so forth). No matter how big the directory, you can find phone numbers with relative ease, because you know exactly where to look for that information. Unlike, say, mystery novels, telephone directories are specifically designed to keep the suspense to a minimum.

To make your scripts easy to read and maintain, consider using a similarly consistent and logical structure. Although scripts vary depending on their nature and complexity, longer scripts might include the following sections:

  • Initialization section. Used for declaring variables and defining constants, the initialization section should always come first in a script.

    This section might also be needed to parse command-line arguments and to ensure that the script is running under the proper script host. If necessary, these actions should always occur before the script does anything else. If your script requires three command-line arguments and cannot complete successfully without all three, the script should verify the existence of those arguments before doing anything else. Never carry out half the functions in a script before verifying whether the script can complete successfully.

  • Main body. To perform the primary actions of the script, some scripts include lines of code in the main body. Other scripts might use the main body as a starting point to call procedures and functions that perform the primary actions of the script. This approach is typically used for long scripts that carry out many activities.

  • Procedures and functions. It is useful to place blocks of code used repeatedly to perform specific actions (for example, creating a user account or converting Fahrenheit temperatures to Celsius) in a function or procedure. That way, the script writer does not have to write the same code multiple times in a script.

    It is also a good idea to list procedures and functions in the same order in which they are called. This is not required; placing a procedure in one location rather than another does not affect performance. In fact, before a script is run, the script host checks the file and identifies the location of all functions and procedures. As a result, the script host can find each function and procedure regardless of its location in the script, and regardless of the order in which the functions and procedures are called. However, placing procedures strategically makes it much easier for others to read and maintain your script.