Cognitive complexity is a software metric that expresses how difficult a piece of code is for a person to read and understand. It was proposed in 2016 by SonarSource, the maker of the static analysis tool SonarQube. The score increases for each structure that breaks the linear flow of code, such as conditionals, loops, and exception handling, with additional increments for deeper nesting.
Whereas the long-established cyclomatic complexity counts the number of execution paths, cognitive complexity is designed to reflect the difficulty a human experiences in understanding the code. It is typically measured per function and used to identify refactoring candidates or to set code quality thresholds.