The Code Review Journalreview desk · diff view

Review bench › Code Smells

Are Code Smells the Wrong Metric? A Case for Cognitive Complexity

Checklist entry · recorded 2026-10-22

1 Overview

Code review has long relied on code smells as a shorthand for maintainability problems. Long methods, deep nesting, duplicated blocks, and large classes are easy to detect and easy to discuss. They give reviewers a vocabulary for saying something feels wrong without having to prove it. But a smell is a surface pattern, not a measurement of how hard the code is to understand. Two methods can have the same line count and nesting depth while one reads clearly and the other forces a reviewer to reconstruct state across several branches. When teams treat smells as the metric, they optimize for the absence of patterns rather than the presence of clarity.

Cognitive complexity offers a different lens. It attempts to estimate how much mental effort a reader spends following control flow, nesting, and interruptions. The goal is not to replace judgment with a number. The goal is to give reviewers a more honest signal about where comprehension breaks down. A high cognitive complexity score often points to code that will be expensive to change, test, and debug, even if it contains no classic smell. A low score can still hide poor naming or misleading abstractions, so the metric is a starting point for discussion, not a verdict.

This article argues that code smells are useful as hints but weak as a primary metric. Cognitive complexity, used carefully, aligns review attention with the actual cost of understanding code. The sections that follow explain why the usual advice misses the point, how to apply the core idea, a repeatable review process, common mistakes, tooling limits, and practical answers to frequent questions.

2 Why the usual advice misses the point

The usual advice says to keep functions short, avoid deep nesting, and eliminate duplication. These are reasonable defaults, but they describe shapes rather than experiences. A short function with a misleading name and hidden side effects can be harder to review than a longer function with a clear linear flow. A deeply nested block may be unavoidable in a parser or state machine, while a flat block may hide a tangle of boolean conditions. When reviewers treat smell checklists as the goal, they produce comments that are easy to write but not always tied to risk. The author fixes the shape, the smell disappears, and the comprehension problem remains.

Another problem is that smells are context-blind. The same pattern can be harmless in a small, stable module and dangerous in a frequently changed core path. Reviewers who lack that context may flag the pattern everywhere, creating noise and eroding trust. Over time, authors learn to write code that satisfies the linter rather than code that communicates intent. The review becomes a compliance exercise instead of a design conversation. Metrics like cognitive complexity are also context-blind, but they at least attempt to model the reader's effort rather than the code's silhouette.

The deeper issue is that smells are often used as a proxy for future maintenance cost. That proxy is weak because maintenance cost depends on how often the code changes, how many people need to understand it, and how easily tests can pin down behavior. A smell in a stable, well-tested module may never cause pain. A cognitively dense function in a hot path can cause pain every sprint. The usual advice misses this distinction because it focuses on the code as an object rather than the team's experience of changing it.

3 The core idea in practice

Cognitive complexity tries to measure how much a reader must hold in working memory to follow a piece of logic. It increases with nesting, with breaks in linear flow such as early returns and jumps, and with boolean conditions that require tracking multiple states. It does not simply count lines or branches. The practical implication is that a reviewer can ask a sharper question: where does my understanding slow down? If the answer is a specific nested block or a chain of conditions, that is a better review target than a generic smell.

In practice, you can use cognitive complexity in two ways. First, as a triage signal before reading: a high score tells you to slow down and read carefully, not to reject the change. Second, as a discussion aid during review: when you feel confused, you can point to the structure that caused the confusion and suggest a refactor that reduces the reader's burden. For example, replacing a deeply nested conditional with guard clauses often lowers cognitive complexity and makes the happy path obvious. Extracting a well-named helper can do the same, provided the helper has a clear contract.

The key is to treat the metric as a model, not a truth. It cannot see naming, domain meaning, or test quality. A function with low cognitive complexity can still be wrong or misleading. A function with high cognitive complexity can be appropriate if it is a well-tested, rarely changed state machine. The value comes from using the metric to direct attention and then applying human judgment. That combination is more useful than either smells alone or scores alone.

4 A repeatable review process

Start with a quick scan of the change to understand intent and scope. Before diving into details, note where the author has added or modified control flow. These are the places where cognitive complexity is most likely to rise. If your tooling surfaces a complexity score, use it as a map rather than a gate. Mark the highest-scoring functions and read them first. If no score is available, you can still approximate by looking for nesting, multiple exit points, and compound conditions.

Next, read the high-complexity areas with a specific question: can a new team member follow this without stepping through it line by line? If not, identify the exact structure that causes the difficulty. Is it a nested loop with a conditional inside? A long chain of else-if branches? A boolean expression with several negations? Write a comment that describes the reader's experience, not just the pattern. For example, say that the nested condition makes it hard to see when the function returns early, and suggest a guard clause or an extracted predicate. This keeps the review focused on comprehension rather than style.

Finally, balance the review. Not every high-complexity function needs to be refactored in the same change. Consider the change's risk, the team's familiarity with the area, and the cost of delay. If the code is correct and well-tested, you might accept it with a follow-up task. If it is in a critical path and likely to change soon, ask for a refactor now. The process is repeatable because it always starts with intent, moves to comprehension risk, and ends with a proportionate recommendation. It avoids the trap of treating every smell as a blocker and every score as a verdict.

5 Common mistakes reviewers make

The first common mistake is treating every code smell as a defect that must be fixed immediately. This turns review into a checklist exercise and floods authors with low-value comments. A long method may be perfectly readable if it has a linear structure and clear naming. A duplicated block may be acceptable if the duplication is stable and extracting it would create a worse abstraction. Reviewers who insist on eliminating every smell often miss the more important question: does this change make the code harder to understand or change?

The second mistake is using raw complexity scores as a gate without reading the code. A number cannot tell you whether the complexity is essential or accidental. It cannot tell you whether the function is well-tested or whether the domain requires a complex state machine. If you reject a change solely because a score crosses a threshold, you are substituting a metric for judgment. That erodes trust and encourages authors to game the metric rather than improve the design. Use scores to prioritize reading, not to automate approval.

The third mistake is ignoring reviewer cognitive load and context switching costs. A reviewer who is juggling five pull requests will not read a complex change carefully. A review that arrives without context forces the reviewer to reconstruct intent from the diff. Teams that care about cognitive complexity in code should also care about cognitive complexity in the review process. Keep changes small, provide a clear description, and avoid mixing refactoring with behavior changes. These practices reduce the mental effort required to review and make it more likely that real comprehension risks are caught.

6 Tooling that helps (and where it fails)

Static analysis tools can calculate cognitive complexity and highlight functions that exceed a threshold. They can also flag classic smells, track trends over time, and integrate with review workflows to show scores on a diff. Used well, this tooling reduces the manual effort of finding dense code. It gives reviewers a starting point and helps teams discuss complexity with a shared vocabulary. It can also support dashboards that show whether complexity is concentrated in a few modules or spread across the codebase.

However, tooling fails in predictable ways. It cannot understand domain semantics, so it may flag a complex but necessary algorithm while missing a simple function with a misleading name. It cannot see test coverage or change frequency, so it cannot tell you whether a high score actually matters. It may also produce false precision: a score of 15 versus 12 sounds meaningful, but the difference may be noise. Teams that treat the number as an objective truth often end up optimizing for the tool rather than for readability. The tool becomes a source of noise instead of a guide.

The better approach is to use tooling as a conversation starter. Let it surface candidates, then apply human judgment about risk, clarity, and context. Configure thresholds as warnings rather than hard failures, and review them periodically to avoid alert fatigue. Pair complexity metrics with other signals such as change frequency, defect history, and test coverage, but do not reduce the decision to a single number. The tool's job is to direct attention; the reviewer's job is to decide what the code needs. When that division of labor is clear, tooling helps without dictating.

7 Frequently asked questions

Is cognitive complexity always better than code smells? No. Smells are still useful as quick hints, especially for duplication and naming. Cognitive complexity is better at estimating comprehension effort, but it is blind to semantics and context. The strongest reviews use both: smells to spot common patterns, complexity to prioritize where understanding is likely to break down, and human judgment to decide what matters.

Should we enforce a maximum cognitive complexity score? A hard maximum is rarely wise. A threshold can be useful as a warning that prompts a closer look, but it should not automatically block a change. Some domains require complex logic, and a well-tested, rarely changed function may be acceptable even with a high score. The goal is to make complexity visible and discussable, not to eliminate it everywhere.

How do we introduce this without disrupting the team? Start small. Add complexity reporting to your review tooling as an informational signal, and discuss it in a retrospective. Agree on a few examples of code that was hard to understand and see whether complexity scores would have flagged them. Then adjust your review guidelines to ask for comprehension-focused comments. Avoid mandating refactors based solely on scores. Over time, the team can develop a shared sense of when complexity is worth addressing and when it is acceptable.

What about legacy code with high complexity? Legacy code often has high complexity because it evolved without review. You do not need to refactor it all at once. Use complexity scores to identify the riskiest areas, then improve them when you next touch them for a feature or bug fix. This keeps refactoring tied to real work and avoids large, risky changes that are hard to review. The metric becomes a guide for incremental improvement rather than a source of guilt.