Branches in the conditional statements should not have the same implementation

  • IDENTICAL_BRANCHES
  • Code Quality
  • Medium
  • cwe

This rule applies when branches in the conditional statements or expressions have the same implementation.

This might imply the condition is unnecessary, but at worst this is a programmer's mistake. For example, a programmer copies code from a branch into another and he often forgets to modify it.

Therefore, a programmer needs to check the same code is really needed for both.

Note: Not applied for case clauses in switch statement because it is likely to be a programmer's intent.

Noncompliant Code Example

View with compliant examples side by side
function example1() {
    if (x >= 0) { // IDENTICAL_BRANCHES alarm
        y = x;
    } else {
        y = x;
    }
}

function example2() {
    y = x >= 0 ? x : x; // IDENTICAL_BRANCHES alarm
}

Compliant Code Example

View with noncompliant examples side by side
function example1() {
    // In case when it's a copy-paste mistake
    if (x >= 0) {
        y = x;
    } else {
        // Do another thing
    }
}

function example2() {
    // In case when two branches have actually the same implementation
    y = x;
}

Version

This rule was introduced in DeepScan 1.0.0-alpha.

See

Was this documentation helpful?