DocsConcepts

Cognitive complexity

Cognitive is a number for how hard a function is to follow. This page explains what raises it, what the 0–14, 15–29 and 30+ tiers mean, and where you see it.

What raises the score

Whaatcher measures SonarSource's Cognitive Complexity for each function. Points come from breaks in the flow, not from length, so a long function with no branches scores 0.

  • if, loops, switch, catch and ternaries add 1, plus 1 for each level of nesting.
  • else if and else add 1, with no nesting bonus.
  • A switch counts once, however many cases it has.
  • Logical operators add 1 each time the operator changes: a && b && c adds 1, a && b || c adds 2.
  • Code inside a nested function or closure counts toward the outer function, one level deeper.

The three tiers

  • 0–14 shows in the normal text color.
  • 15–29 shows in amber with ⚠. 15 is SonarSource's default threshold.
  • 30+ shows in red.

The tiers help you scan. They don't judge whether a function needs changing.

Where you see it

  • Files: the COG column of OUTLINE, with the tiers in a legend below. See Files.
  • Overview: Hotspots ranks functions by changes in the last 90 days × current score, and Growing lists those that rose in the last 30 days. See Overview.
  • Impact: the Functions section shows each changed function's Cognitive before → after, in green for a drop and red for a rise to 30 or more. See Impact.
  • Insight: Growing functions lists functions that rose during the period and are now 30 or more. See Insight.

Languages

Whaatcher measures functions in 13 languages: TypeScript, JavaScript, Swift, Python, Go, Kotlin, Java, Rust, C#, C, C++, Ruby and PHP. It reads .h files as C++. Files in other languages have no COG column.

When analysis is off or not ready

The COG column measures the open file directly, so it doesn't wait for analysis. Everything else needs the repository's analysis, which starts when you add the repository, runs in the background and resumes where it stopped.

  • Until the first pass finishes, you see Analysis pending, and Overview shows the progress.
  • When analysis is off, turn it on with Turn On Analysis in Overview or in Settings › Code Analysis.
  • If analysis fails, you see Analysis failed. Whaatcher tries again after a restart or a new commit on the analyzed branch.

Code analysis covers the analyzed branch and these settings.