Duplicate if Condition

ID

javascript.duplicate_if_condition

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

JavaScript

Tags

control-flow, copy-paste, reliability

Description

Reports two consecutive if statements in the same block whose conditions are textually identical:

if (x > 0) doStuff();
if (x > 0) doOther();    // FLAW — same condition as the previous if

Rationale

Two consecutive if statements with the same condition almost always mean either:

  • the developer copy-pasted the first if and forgot to change the condition on the duplicate, or

  • the two branches should have been merged into a single if with two statements.

In both cases, evaluating the condition twice is wasted work, and the duplication confuses readers — they spend time looking for the difference that isn’t there.

Remediation

Merge the two branches into one:

if (x > 0) {
  doStuff();
  doOther();
}

If the second condition was meant to be different, fix it:

if (x > 0) doStuff();
if (x < 0) doOther();

Notes

The rule looks at top-level statements within the same block. Unrelated if`s separated by other statements or at different nesting levels are not flagged. `else if chains are handled separately by javascript.duplicate_else_if_conditions.

Comparison is by the trimmed source of the condition.