If/Else With Identical Branches
ID |
javascript.if_identical_branches |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
JavaScript |
Tags |
copy-paste, dead-code, reliability |
Description
Reports an if/else where both branches have identical bodies:
if (cond) {
doStuff();
} else {
doStuff();
}
Rationale
A conditional that runs the same code on both sides almost always means one of two things, and both are bugs:
-
the
elsebranch was copy-pasted from theifbranch and the developer forgot to change it -
the condition was meant to control a different statement and now does nothing
Either way the conditional is misleading. Readers expect that if/else means "two different things happen" — when both sides are identical, every reader has to take an extra moment to verify that observation.
Remediation
Drop the if/else if the condition has no side effects:
doStuff();
If the two branches were supposed to differ, fix whichever one is wrong:
if (cond) {
doStuff();
} else {
doOtherStuff();
}
Notes
The rule compares the textual content of the two branches after stripping the surrounding braces. It does not fire on else if chains — those are covered by javascript.duplicate_else_if_conditions. Empty branches are also not reported, since if (cond) {} may be a deliberate placeholder.