Setting Statement Breaks

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Text editors allow you to create lines of seemingly infinite length (for example, you can type 1,024 characters on a single line in Notepad); in theory, you could type an entire script and a reasonably lengthy one at that on a single line in Notepad. There might be some advantage to creating long lines of code; nevertheless, it is strongly recommended that you limit the length of any statement in a script to a maximum of 80 characters. This is suggested for at least two reasons.

  • 80 characters is the default setting for the command shell. Unless you change this setting, a statement longer than 80 characters forces you to scroll horizontally as well as vertically to view all parts of the statement. Without scrolling horizontally, the following statement is cut off:
    Set colServiceList = GetObject("winmgmts:").ExecQuery("Select * from 32_Servi 

    Statements that require a user to scroll back and forth are more difficult to read and modify.

    In addition, many system administrators use programs such as Edit.exe for editing scripts. With many of these editors, lines are always wrapped at the 80th character, regardless of the screen resolution.

  • Notepad is often used for printing scripts. Notepad is not designed to work with lines with more than 80 characters. In fact, lines that exceed 80 characters are printed in the following fashion:
    Set colServiceList = GetObject("winmgmts:").ExecQuery("Select * from Win32_Servi ce Where State = 'Stopped' And StartMode = 'Auto' ") 

If a statement has more than 80 characters, pick a logical spot to split the line, insert a statement break, and then continue the statement on the next line:

Set colServiceList = GetObject("winmgmts:").ExecQuery("Select * from " _     & "Win32_Service Where State = 'Stopped' And StartMode = 'Auto' ") 

Limiting Each Line to a Single Statement

Although many scripting languages allow you to place multiple statements on a single line, this can be confusing to anyone reading your script and should be avoided. For example, using VBScript you can place multiple statements on a single line by separating each statement with a colon. The following code sample shows four statements included on a single line of code:

For i = 1 to 100 : intTotal = intTotal + i : Next : Wscript.Echo intTotal 

This code is valid, but the purpose of the script is not readily apparent. In fact, although it appears far more complicated, this script merely adds the integers from 1 to 100 and then echoes the result. The purpose is more apparent when each statement is placed on a separate line and formatted according to previous recommendations:

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

Avoiding Single-Line If Then Statements

VBScript allows you to place If Then statements on a single line, a shortcut method that can save a few keystrokes when writing a script. However, the time saved writing the script could result in additional time required for someone editing your script, because single-line If Then statements are often hard to read. For example, determining the purpose and possible variable values is difficult in this line of code:

If (intDept = 1) Then strDepartment = "Accounting" Else strDepartment = "Other" 

By comparison, the multiline If Then statement in the following script snippet clearly shows the purpose of the script: check the value of the variable intDept, and use that result to set the value of the variable strDepartment.

If (intDept = 1) Then     strDepartment = "Accounting" Else     strDepartment = "Other" End If 

By avoiding single-line If Then statements, you also facilitate using statement blocks such as If End If and Do Loop. One common scripting mistake is to start a statement block but then fail to end it, such as using an opening statement like For i = 1 to 100 without using a closing statement such as Next.

Note

  • Preparing for Visual Basic .NET. Single-line If-Then statements are not allowed in Visual Basic .NET. In Visual Basic .NET, you must use multiple-line If-Then statements.

Limiting Your Scripts to a Single Language

To help other administrators who maintain or modify your scripts, limit each individual script to a single scripting language. Windows Script Files provide a way to use multiple scripting languages within a single script; for example, you can write a script that carries out one function in VBScript, a second function in JScript®, and a third in any other registered ActiveX® scripting language. Although this provides considerable flexibility, anyone who maintains your script must be proficient in each of these languages. Use additional languages only if you cannot perform those operations any other way.

Note

  • Preparing for Visual Basic .NET. Windows Script Files can be used with Visual Basic .NET, but only if all the jobs specified in the file use the same scripting language. Windows Script Files using multiple languages are not allowed.

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net