Choosing a Script Construction
Microsoft® Windows® 2000 Scripting Guide
Although scripts often include an initialization section, a main body, and a set of functions and procedures, you can arrange those elements in different ways. As you might expect, some of these arrangements are better than others, depending on the length and complexity of the script. For example, scripts that span hundreds of lines and need to use the same code over and over benefit from a construction in which repeated code is placed in individual functions. By contrast, functions and procedures can needlessly complicate shorter scripts that perform only a single task. For example, placing the following code in a function does nothing to help the performance of the script and only makes a simple script much more complex:
Wscript.Echo AddTwoNumbers
Function AddTwoNumbers
AddTwoNumbers=2+2
End Function
This same script can be written as follows:
Wscript.Echo 2+2
Table 18.4 lists advantages and disadvantages of several different scripting formats.
Table 18.4 Advantages and Disadvantages of Various Scripting Formats
Script Construction |
Advantages |
Disadvantages |
---|---|---|
Scripts that do not call any functions or procedures |
|
|
Scripts that scatter functions and procedures throughout the script |
|
|
Scripts constructed as follows:
|
|
|
Scripts constructed as follows:
|
|
|
Scripts constructed as follows:
|
|
|