Boolean expression with five or more operands

ID

c.maintainability.expression_too_complex

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Complexity

Language

C / C++

Description

Reports a boolean expression chaining five or more operands with the same logical operator (&& or ||).

Rationale

A reader has to hold every operand and the overall truth table in mind at once to understand when a long boolean chain evaluates true or false, and to know which operand a bug fix or a new requirement should be added to. The more operands, the more combinations a test suite needs to cover to exercise the condition’s logic, and the more likely it is that the chain is really testing several independent things at once.

The following code illustrates the pattern detected by this rule:

void long_and_chain()
{
    // FLAGGED: Boolean expression with five or more operands
    if (is_admin && is_active && is_verified && has_quota && is_enabled) {
        do_something();
    }
}

Remediation

Extract the condition (or a coherent subset of its operands) into a well-named helper function or a local boolean variable that states the intent, e.g. bool canProceed = a && b && c; followed by if (canProceed && d && e).

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved