Same condition in two consecutive branches

ID

apex.maintainability.duplicate_branch_condition

Severity

high

Remediation Complexity

medium

Remediation Risk

medium

Remediation Effort

low

Resource

Control Flow

Language

Apex

Description

Reports an if whose immediately following else if tests the same condition, so the second branch can never run. Only consecutive branches of a chain are reported.

Rationale

The second branch is dead: the chain reached it precisely because the condition was false, and nothing between the two tests can have changed it. Whatever that branch was meant to handle is therefore not handled, and the case it was written for silently takes the first branch’s behaviour instead. This is almost always a copy of the previous branch whose condition was never edited, so the condition that should be there - the next status, the next amount band - is missing from the chain entirely.

The following code illustrates the pattern detected by this rule:

public class OrderRouter {

    public static String queueFor(Order o) {
        // FLAGGED: Same condition in two consecutive branches
        if (o.Status == 'Draft') {
            return 'review';
        } else if (o.Status == 'Draft') {
            return 'fulfilment';
        }
        return 'none';
    }

    public static String tierFor(Decimal amount) {
        // FLAGGED: Same condition in two consecutive branches
        if (amount > 1000) {
            return 'gold';
        } else if (amount > 1000) {
            return 'silver';
        } else {
            return 'bronze';
        }
    }

    public static String tierForFixed(Decimal amount) {
        if (amount > 1000) {
            return 'gold';
        } else if (amount > 500) {
            return 'silver';
        } else {
            return 'bronze';
        }
    }

    public static String queueForFixed(Order o) {
        if (o.Status == 'Draft') {
            return 'review';
        } else if (o.Status == 'Activated') {
            return 'fulfilment';
        }
        return 'none';
    }

    public static String simple(Boolean express) {
        if (express) {
            return 'express';
        } else {
            return 'standard';
        }
    }
}

Remediation

Correct the second condition to the test that was intended, or delete the branch if the first one already covers the case. Where the two branches do the same work, merge them into one condition with ||.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved