Initializing local variables in advance





This article describes the standards that apply to local variable initialization. All of the listed recommendations are mandatory unless noted otherwise.

This recommendation is optional

When a script fragment calculates one or several local variable values, we recommend that you initialize the variables in advance, in order to avoid runtime errors that appear when the variable value is Undefined but the script expects a value of specific type.

Incorrect

If Something Then
    MyVariable = 10;
ElsIf
    // more branches
    …
EndIf;
 
… = MyVariable; // if Something is not TRUE, MyVariable might be Undefined

Correct:

MyVariable = 0; // default value
 
If Something Then
    MyVariable = 10;
ElsIf
    // more branches
    …
EndIf;
 
… = MyVariable;  // the variable value is always a number

This recommendation makes sense for long If / ElsIf / Else blocks where variable initializations inside the blocks might be easily overlooked.


Comments
0
Add comment