Using White Space

Microsoft® Windows® 2000 Scripting Guide

White space, achieved with blank lines, blank spaces, and character indentation, provides visual cues that help delineate and identify program flow and program sections. Because white space does not affect performance, use it whenever and wherever it is required. For instance, consider the following script, with a file size of 75 bytes.

For i = 1 to 95
   intTotal = intTotal + i
Next
Wscript.Echo intTotal

As an experiment, insert 10,000 blank lines before and after each of the lines in the script. This increases the file size to 98 KB, most of it white space. When you run the revised version of this script, there is no appreciable performance difference between the 98-KB version and the 75-byte version. Because performance is not affected, you can insert as many blank lines or blank spaces as needed to improve readability. For example, you might want to use blank spaces:

  • Before and after operators.

  • In parameter lists.

  • To indent control structures, functions, and procedures.

Using Blank Spaces Before and After Operators

A blank space before and after each operator makes your code more readable. In most scripting languages, scripts run regardless of whether or not you put spaces before or after operators; however, the additional blank spaces generally make the script easier to read. For example, in the following line of VBScript code, notice how the k in the variable intCheck tends to run together with the >= sign and the <> sign. In addition, it is hard to separate the >= sign from the - sign that precedes the 3.

If intCheck>=-3 and intCheck<>7 then

Adding spaces before and after each operator makes the code easier to read and understand:

If intCheck >= -3 and intCheck <> 7 then

To further enhance readability, you might also enclose the If Then criteria in parentheses. In addition to enhancing readability, parentheses help enforce the order of precedence when using a conditional statement; the parentheses ensure that these statements are properly evaluated before the script takes any further action. Although parentheses are not required, they also make it easier to see the conditions being evaluated:

If (intCheck >= -3) and (intCheck <> 7) then

Using Blank Spaces in Parameter Lists

System administration scripts, including many WMI scripts that either create new items or reconfigure existing items, require you to supply parameters in a specific order. In many cases, this poses no problem. For example, in this hypothetical script, it is clear that the first parameter is "Large", the second parameter "Heavy", and the third parameter "Red":

objThing.Change("Large","Heavy","Red")

But what if you want to change only the third parameter? In a situation such as that, you typically leave the first two parameters blank. When you do that, you must include two commas, making "Red" the third parameter:

objThing.Change(,,"Red")

When constructing parameter lists such as this, it is a good idea to put blank spaces before or after commas. In the following script, it is difficult to tell how many commas precede the value True. This is important in this example because each comma represents a placeholder for a service parameter, and True must be the sixth item in the list.

Set objServiceList = GetObject("winmgmts:").InstancesOf("Win32_Service")
For Each strService in objServiceList
    Return = strService.Change(,,,,,True,,,,,,)
Next

The commas are easier to count when separated by blank spaces, as shown in the following script snippet:

Set objServiceList = GetObject("winmgmts:").InstancesOf("Win32_Service")
For Each strService in objServiceList
    Return = strService.Change( , , , , , True , , , , , , )
Next

Indenting Control Structures, Functions, and Procedures

Control structures are coding conventions that determine the flow of a script. For example, If Then is a control structure because the flow of the script hinges on this structure: If the condition is True, the script branches in one direction; if it is False, the script branches in another direction.

Indentation is commonly used with control structure statements such as If Then and For Next. For example, the syntax shown in the following snippet is valid, but it is difficult to understand what the script is doing without reading the code several times and mentally tracing the program flow.

For Each strEvent in objInstalledLogFiles
If (strEvent.EventCode >= "529") and (strEvent.EventCode <= "539") Then
If (strEvent.EventCode <> "538") Then
intEventTotal = intEventTotal +1
End If
End If
Next

Compare that with the revised code shown in the following script snippet. This is the same code, but each new control structure statement is indented four spaces.

For Each Event in InstalledLogFiles
    If (strEvent.EventCode >= "529") and (strEvent.EventCode <= "539") Then
        If (strEvent.EventCode <> "538") Then
            intEventTotal = intEventTotal +1
        End If
    End If
Next

The indentation makes it easier to understand when each block of code runs. In the preceding example, you can see that the first If Then section runs only for events with an EventCode of 529 through 539. The nested If Then statement, which adds up the total number of events, runs only if the EventCode does not equal 538. Thus, the script tallies all the events with the EventCode 529, 530, 531, 532, 533, 534, 535, 536, 537, or 539. An event with the EventCode 538 is not included in the tally.

It is also recommended that you use indentation in all major sections in your scripts, including functions and procedures. As a rule of thumb, use four spaces for each indent; in some cases, identifying passages indented less than four spaces is difficult, while indenting more than four spaces usually creates more empty space than needed. For example, indent a function as follows:

Function ConvertToMiles(ByVal sngKilometers)
    ConvertToMiles = sngKilometers * 2.2
End Function

When formatting control structure code, be sure that you align all the control structure statements (such as Do and Loop). In the following example, the If, ElseIf, Else, and End If statements are all aligned. Alignment clearly indicates that three possible values for the variable strState are being evaluated.

If (strState = "WA") Then
    intStateIDNumber = 1
ElseIf (strState = "OR") Then
    intStateIDNumber = 2
Else
    intStateIDNumber = 3
End If

In addition, use spaces rather than tabs when indenting statements. Some text editors use different tab formats than others; as a result, your carefully aligned code might look quite different when viewed by someone using a different editor.