Unsafe namespace change

objectscriptQuality release 
1.0.0
Id 
OS0061
Rule type 
Code Smell
Severity 

Critical

Critical
SQALE characteristic 
  • Reliability
    • Instruction
Tags 
probable-bug
Remediation function 
Constant/issue
Remediation cost 
5min

In some situations, an application may want to switch namespaces temporarily using, for instance:

    Method m()
    {
        set oldNs = $namespace
        set $namespace = newNs // or: znspace newNs
        do ..something()
        set $namespace = oldNs // or: znspace oldNs
    }

But if the code between namespace changes fails for any reason, the namespace will not be switched back. The solution is to use new $namespace instead:

    Method m()
    {
        // Make any changes to $namespace private to this method
        new $namespace
        set $namespace = newNs // or: znspace newNs
        do ..something()
        // Namespace is automatically restored here
    }