SC22 Function cognitive complexity exceeds threshold
R2026bThe function cognitive complexity of a function is greater than the defined threshold
Since R2026b
Description
Function cognitive complexity (FCC) estimates how difficult a function is to read and reason about. Polyspace® scans the code inside a function body and looks for control flow interruptions and nested structures.
void func() { // Only statements inside this function contribute to the FCC for func }
Linear score — A count of control flow interruptions in the function. Polyspace increments the linear score by 1 for each of these control flow interruptions:
elsecontinuebreak— Only when not used as acasestatement terminatorgotostatementgotolabel (target)Direct recursive calls
First logical operator in a boolean expression (
&&or||)Each change of logical operator type in a boolean expression (for example, switching from
&&to||)
else ifis treated as a single construct, not as nestedelsecontainingif, to avoid double counting. For example, in this code, each control flow interruption increments the linear score by 1:void g(bool a, bool b, bool c) { if (a && b || c) {} // linear score imrement: +1 for first operator (&&), +1 for operator change (||) else if (b) {} // linear score imrement: +1 for else else { // linear score imrement: +1 for else if (c) goto end; // linear score imrement: +1 for goto } end: return; // linear score imrement: +1 for goto label } // Linear score: 6Nesting score — A measure of control flow structure nesting depth. Polyspace increments the nesting score for each nested construct by 1 plus its current nesting depth. The following constructs contribute to nesting:
ifstatements (excludingelse if)Ternary operator
?:switchstatementsforloopswhileloopsdoloopscatchclauses
For example, in this code, the nesting score increment increases with deeper nesting levels:
Polyspace considers lambdas to increase the nesting depth of child elements by one.void h(bool a, int n) { if (a) { // nesting score increment: (0+1) = 1 for (int i=0;i<n;++i) { // nesting score increment: (1+1) = 2 int x = a ? 1 : 2; // nesting score increment: (2+1) = 3 } } }
The total cognitive complexity of a function is the sum of these two scores:
FCC = Linear Score + Nesting Score
Polyspace reports a violation when the total cognitive complexity exceeds the
specified threshold. Polyspace uses a default threshold of 15 unless you specify a different threshold.
To specify an activation XML file where you can set the threshold, use the option
Checkers
activation file (-checkers-activation-file).
Risk
Exceeding the function cognitive complexity threshold indicates that the function contains many divergent control flow paths and a high degree of nesting, making the function difficult to read and understand. These kinds of functions are difficult to maintain and test because of their complex structure. Functions with high cognitive complexity are also more difficult to parse using AI-based tools, which can obstruct workflows that rely on automated code analysis or generation.
Fix
To fix this violation:
Reduce nesting by using early returns or guard clauses.
Extract deeply nested logic into separate helper functions.
Simplify boolean expressions by breaking them into named variables.
Replace complex conditional chains with simpler control flow structures.
Examples
Check Information
| Group: Software Complexity |
| Language: C | C++ |
Acronym:
SC22
|
| Default Threshold: 15 |
Version History
Introduced in R2026b