Local variable not previously declared

objectscriptQuality release 
1.0.3
Id 
OS0064
Rule type 
Code Smell
Severity 

Minor

Minor
SQALE characteristic 
  • Maintainability
    • Readability
Tags 
clarity, coding-guidelines
Remediation function 
Constant/issue
Remediation cost 
2min

In these code samples:

    do ..someMethod(.x)
    // or
    do ##class(some.Class).someMethod(.x)

x is automatically created and initialized.

However, this can be confusing since no prior declaration of x exists.

For clarity reasons, it is recommended that a prior declaration exists. The preferred way here is to use #dim which, even though it will do nothing to create the variable itself, has the advantage that you can declare the type, if any. Therefore, the code above would become:

    #dim x as sometype

    do ..someMethod(.x)
    // or
    do ##class(some.Class).someMethod(.x)

Other possibility

Another possibility is to use set instead:

    set x = something

    do ..someMethod(.x)
    // or
    do ##class(some.Class).someMethod(.x)

However, this works quite differently from #dim:

  • set will actually create the variable;
  • a set declaration doesn't allow to specify the type, unlike #dim.

This check will also look for a prior set declaration and will not trigger in this case.