with statement should not be used

  • BAD_WITH_STATEMENT
  • Code Quality
  • Low
  • No tags

This rule applies when with statement is used.

with statement is not recommended by the following problems:

  1. with statement makes it hard to know that the variable is defined in which the specified object scope or parent scope chain.
  2. Since with statement forces the specified object scope to be searched first, accessing scope of parent object can be slower.

Note: In strict mode, using with statement throws a SyntaxError.

Noncompliant Code Example

View with compliant examples side by side
function foo(v, obj) {
    with (obj) { // BAD_WITH_STATEMENT alarm
        v = "ambiguous"; // It is hard to know whether 'v' is a property of 'obj' or the first argument of function 'foo'.
        return v;
    }
}

Compliant Code Example

View with noncompliant examples side by side
function foo(v, obj) {
    obj.v = "not ambiguous";
    v = "not ambiguous";
    return v;
}

Version

This rule was introduced in DeepScan 1.0.0-alpha.

Was this documentation helpful?