A common code smell that can hinder the clarity of source code is making assignments within sub-expressions. This practice involves assigning a value to a variable inside a larger expression, such as within a loop or a conditional statement.
This practice essentially gives a side-effect to a larger expression, thus making it less readable. This often leads to confusion and potential errors.
Moreover, using chained assignments in declarations is also dangerous because one may accidentally create global variables. Consider the following
code snippet: let x = y = 1;. If y is not declared, it will be hoisted as global.
The rule does not raise issues for the following patterns:
a = b = c = 0; (a = 0) != b a = 0, b = 1, c = 2 () => a = 0 a || (a = 0) while (a = 0); Making assignments within sub-expressions can hinder the clarity of source code.
This practice essentially gives a side-effect to a larger expression, thus making it less readable. This often leads to confusion and potential errors.
Extracting assignments into separate statements is encouraged to keep the code clear and straightforward.
if (val = value() && check()) { // Noncompliant
// ...
}
val = value();
if (val && check()) {
// ...
}