Skip to content
Fallow home
All docs pages

Docs home

High cognitive complexity

What a high-cognitive-complexity finding of fallow health means, why it matters and how to fix it.

What it reports

Function has high cognitive complexity.

Health explains how fallow finds it.

Why it matters

SonarSource cognitive complexity exceeds the configured threshold. Unlike cyclomatic complexity, cognitive complexity penalizes nesting depth and non-linear control flow (breaks, continues, early returns). It measures how hard a function is to understand when reading sequentially. fallow also emits this rule on synthetic <template> findings across every supported template dialect (Angular .html templates and inline @Component({ template: ... }) literals, Vue SFC <template> blocks, Svelte markup, Astro markup), where nesting penalties accumulate on stacked control-flow blocks: Angular @if/@for/@switch, Vue v-if/v-for, Svelte {#if}/{#each}/{#await}; on each top-level Svelte {#snippet name(...)} block, emitted as its own <snippet:NAME> unit with nesting rebased to zero so in-file snippet extraction lowers the parent template's score; and on synthetic <component> rollup findings whose cognitive is the worst class method's score plus the template's. Ranking and --targets use the rollup total; JSON exposes the per-half breakdown under component_rollup.

Example

applyDiscount puts an if inside a for inside another if, with a continue and a && chain in the inner block. Each nesting level adds to the score, which reaches 21 while health.maxCognitive is 15.

Fix

For function findings, extract named helpers, split independent branches, flatten guard clauses, and add tests around the behavior before refactoring. For <template> findings, flatten nested control flow and move branch selection out of the markup: Angular @if/@for/@switch/@defer and legacy *ngIf/*ngFor into computed signals on the component class, Vue v-if/v-else-if/v-for into computed properties, Svelte {#if}/{:else if}/{#each}/{#await}/{#key} into derived state, Astro control flow into props resolved in the frontmatter. In Svelte, moving a repeated or deeply nested block into a top-level {#snippet} gives it its own <snippet:NAME> unit with nesting rebased to zero, so the in-file extraction moves the score; splitting a large template into child components lowers the score in every dialect. For <component> rollup findings, attack the larger half first; the per-half breakdown lives in component_rollup. To report these findings without a failed run, set the complexity-cyclomatic or complexity-cognitive rule to warn.

Facts

FactValue
Issue typehigh-cognitive-complexity
Rule id in SARIF outputfallow/high-cognitive-complexity
Commandfallow health
Default severityerror
Config key in rulescomplexity-cognitive
Filter option--complexity
Suppression comment// fallow-ignore-next-line complexity
Fixable with fallow fixNo
Opt-inNo

When to keep the code