Microsoft® Windows® 2000 Scripting Guide

In the script in Listing 2.3, the amount of free megabytes is calculated by taking the value of the FreeSpace property and dividing it by the hard-coded value 1048576. (Hard-coded values such as this are often referred to as literals because they do not stand for something else but literally represent the value.)

In a small script such as this (particularly a small script written for your own use), hard-coding literal values usually does not pose much of a problem. However, in a larger script, particularly one used in an enterprise setting, literals can lead to at least two problems.

For one thing, in a small script it might be obvious that 1048576 is the value required to convert bytes (the value returned from the FreeSpace property) to megabytes. In a larger script, however, one that includes a number of mathematical equations, this might be less obvious. This is especially true in an enterprise setting, in which multiple administrators might use and modify the same script. You might know what the 1048576 represents, but another administrator charged with modifying the script might not.

The fact that scripts often need to be modified raises a second issue. Literal values not only can be confusing but can also require extra work for anyone modifying the script. Suppose this same procedure, converting kilobytes to megabytes, is used five or six times throughout a script. If you later decide to convert the value to gigabytes rather than megabytes, you will have to correctly modify each line of code where the conversion takes place or your script will no longer provide accurate results.

One way to work around the problems that can arise from the use of literals is to use constants instead. Constants are similar to variables in that they are places to store data. Unlike variables, however, after a constant has been defined (that is, after it has been assigned a value), it cannot be changed while the script is running. By assigning important items, such as the value required to convert bytes to megabytes, to a constant, you ensure that the value cannot be changed, inadvertently or otherwise.

In the script in Listing 2.5, a constant named CONVERSION_FACTOR is defined in line 1 and is assigned the value 1048576. Later in the script (line 4), the number of bytes of free disk space is converted to the number of megabytes of free disk space. Instead of the literal value 1048576, the constant CONVERSION_FACTOR is used. Both equations return the same result; however, the equation in Listing 2.5 is easier to read and understand.

Listing 2.5 Using Constants


Set objWMIService = GetObject("winmgmts:")
Set objLogicalDisk = objWMIService.Get("Win32_LogicalDisk.DeviceID='c:'")
FreeMegaBytes = objLogicalDisk.FreeSpace / CONVERSION_FACTOR
Wscript.Echo Int(FreeMegaBytes)

Another benefit to using constants is that they can be defined once and then used multiple times throughout the same script. For example, an expanded version of the script in Listing 2.5 might require you to convert bytes to megabytes several times during the running of the script. Rather than using the literal value in each equation, use the constant instead. If you later decide to convert bytes to gigabytes, you will have to change only the value of the constant; you will not have to change the value used in each equation.