Avoid ‘if/else if’ & cases statements having the same condition

Why you should care

A chain of if/else if statements is evaluated from top to bottom. At most, only one branch will be executed: the first one with a condition that evaluates to true.

Therefore, duplicating a condition automatically leads to dead code. Usually, this is due to a copy/paste error. At best, it’s simply dead code and at worst, it’s a bug that is likely to induce further bugs as the code is maintained, and obviously it could lead to unexpected behavior.

How we detect

CAST Highlight counts one occurrence each time:

  • PATTERN 1 : a whole condition is the same than a previous one in the sequence,
  • PATTERN 2 : a simple condition is combined with “||” operator in a previous one in the sequence
  • PATTERN 3 : a compound condition is combining with “||” operator a previous condition of the sequence

Bad Code

func example(condition1, condition2 bool) {
if condition1 {
} else if condition1 { // +1 VIOLATION (PATTERN 1)
}

if z || y{
} else if y { // +1 VIOLATION (PATTERN 2)
}


if a {
} else if a || b { // +1 VIOLATION (PATTERN 3)
}

if (a) {
} else if a || b { // +1 VIOLATION (PATTERN 3)
}

if foo && bar {
} else if (bar && foo) { // +1 VIOLATION (PATTERN 1) /!\ difficult to detect because simple conditions are not in the same order
}
}

References

5362

About CAST and Highlight’s Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.