Cognitive complexity
Cognitive complexity is the mental effort required to understand code, especially as its control flow and reasoning paths become more complex.
The Art of Code
Cognitive complexity is a code-complexity metric that estimates the mental effort required to understand control flow, especially when flow-breaking structures and nesting are present.
- How it is used
- Uses cognitive complexity to identify control flow that impedes readability and guide simplification.
- How deep it goes
- Explains per-method scoring and demonstrates reductions through extraction and declarative iteration.
- Examples
- Refactors a Java email-reminder method and assesses a Kotlin promotional-message if/else chain.
- What you can do after
- Evaluate code for flow-breaking structures and apply refactorings that reduce nesting and clarify intent.
Troubleshooting Java, Second Edition
The mental difficulty of understanding code investigation as the number of nested reasoning paths and open investigative plans increases.
- How it is used
- Guides code investigation when visible logic is insufficient to explain runtime behavior.
- How deep it goes
- Explains nested reasoning plans and investigation trade-offs, but stops short of formal measurement or debugging metrics.
- Examples
- Uses a Java Decoder method, StringDigitExtractor calls, and questions about returned digits and changed state.
- What you can do after
- Diagnose/debug with it by controlling investigation depth and tracing hidden behavior behind method calls.
Read how Troubleshooting Java, Second Edition explains cognitive complexity →
Related concepts
- expressive powercontrasts with · Expressive power is the capacity of a model, language, or system to represent or convey a range and complexity of functions, patterns, distinctions, or ideas.
- functional programmingaddressed by · Functional programming is a programming paradigm that expresses computation through the evaluation and composition of functions, typically favoring immutable data and minimizing mutable state and side effects.
- complexity metrickind of · A complexity metric is a quantitative measure of the structural intricacy or difficulty of understanding, modifying, or executing software code.
- cyclomatic complexityalternative to · Cyclomatic complexity is a software metric that measures the number of linearly independent execution paths through a program's control-flow graph.
- flow-breaking structuremeasures · A flow-breaking structure is a control-flow construct that interrupts straightforward sequential execution and requires reasoning about alternate execution paths.
Questions about cognitive complexity
What is cognitive complexity?
Cognitive complexity is the mental effort required to understand code, especially as its control flow and reasoning paths become more complex.
Which Manning books explain cognitive complexity?
The Art of Code and Troubleshooting Java, Second Edition.
What is the difference between cognitive complexity and expressive power?
Cognitive complexity is the mental effort required to understand code, especially as its control flow and reasoning paths become more complex. Expressive power is the capacity of a model, language, or system to represent or convey a range and complexity of functions, patterns, distinctions, or ideas.
What is cognitive complexity a kind of?
Cognitive complexity is a kind of complexity metric. A complexity metric is a quantitative measure of the structural intricacy or difficulty of understanding, modifying, or executing software code.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

