beautypg.com

Script writing guidelines, Coding guidelines, Style suggestions – Visara Master Console Center Scripting Guide User Manual

Page 22: Coding guidelines style suggestions

background image

Chapter 2 Advanced Topics

Scripting Guide

22

Script Writing Guidelines

The following coding guidelines and style suggestions increase code
readability and ease maintenance:

Coding Guidelines

ƒ

End all scripts with RETURN, so they can be easily called from other
scripts.

ƒ

Always check return values.

ƒ

Use SCANP whenever possible, not SCANB.

ƒ

Avoid using GOTOs, except with functions such as SCANP.

ƒ

If a section of code is repeated, make it a subroutine.

ƒ

Initialize variables before use.

ƒ

Plan the script to ensure there are paths for each possible condition,
and avoid “fall-through” situations. For example, if a RETURN is not
specified, control of the script may fall through to the next subroutine
by default, and not be processed correctly.

ƒ

Use Manifest Constants (see page 42), not values. For example,
TRUE|FALSE is preferred to 0|1.

ƒ

SWITCH-ENDSWITCH statements should always have a DEFAULT:
condition to handle unexpected values.

Style Suggestions

ƒ

Keep subroutines as brief and as simple as possible. If a subroutine
exceeds 100 lines, consider breaking it into several subroutines.

ƒ

Precede all scripts or subroutines with a description, including pre-
and post-conditions as well as exceptions and return codes that may
be passed.

ƒ

Use variables in place of literals wherever possible.

ƒ

Comment at each gosub call, stating the reason for each call if it is not
obvious.

ƒ

Include a commented usage statement for each subroutine if it is not
obvious.

ƒ

Do not comment obvious statements, for example, “%Continue
:=FALSE //don’t continue”.

ƒ

Try to use the same case for variable names throughout a script.

ƒ

Indent statements within control loops (IF-ENDIF, WHILE-
ENDWHILE...).

ƒ

Put one space after each open parenthesis, and also after each comma.
This allows use of the Ctrl-Left Arrow and Ctrl-Right Arrow keys to
jump to the next word.