Operands always having the same value should be avoided
- SAME_OPERAND_VALUE
- Code Quality
- Medium
- No tags
This rule applies when the operation's result is fixed because operands have the same value always.
For example, the result of x - x
is 0 for all values of x
except NaN
.
It is recommended to use the constant value because it is often indistinguishable from mistake.
The following operators are considered: -
, /
, %
, ^
, <
, >
, =
, >=
, ==
, !=
, ===
, !==
Noncompliant Code Example
View with compliant examples side by side// Example 1
if (x) {
diff = x - y;
} else {
x = y;
diff = x - y; // SAME_OPERAND_VALUE alarm. The result is always 0.
}
// Example 2
if (value > maxValue) {
value = maxValue;
diff = value - maxValue; // SAME_OPERAND_VALUE alarm. The result is always 0.
}
Compliant Code Example
View with noncompliant examples side by side// Example 1
if (x) {
diff = x - y;
} else {
x = y;
diff = 0;
}
// Example 2
if (value > maxValue) {
diff = value - maxValue;
value = maxValue;
}
Version
This rule was introduced in DeepScan 1.0.0-alpha.