Developer

Code Complexity Estimator

Calculate cyclomatic and cognitive complexity, and interpret the numbers without misusing them.

Last reviewed by the Radiatus Cloud team

Need this built for your product?

We design, build & host secure software & APIs.

Talk to an engineer

Cyclomatic complexity counts paths

Developed by Thomas McCabe in 1976, it counts linearly independent paths through a function. In practice it is one plus the number of decision points: each if, each loop, each case, each catch, and each logical operator that short-circuits. A function with no branches scores 1. The number is also the minimum count of test cases needed for full branch coverage, which is its most concrete practical use.

What the ranges mean

1 to 10 is simple and straightforward to test. 11 to 20 is moderate and warrants a look. 21 to 50 is complex, hard to test thoroughly and a frequent source of defects. Above 50 is generally considered untestable. These bands are conventions rather than laws, and the underlying claim — that defect density rises with complexity — holds in aggregate but not for every individual function.

Cognitive complexity measures something different

Cyclomatic complexity treats a flat switch with twenty cases the same as four levels of nested conditionals, though one is trivial to read and the other is not. Cognitive complexity, introduced by SonarSource, penalises nesting and rewards structures humans follow easily. It correlates better with how hard code is to understand, which is usually the actual question. Where the two disagree, cognitive complexity is generally the more useful signal.

The metric is easy to game

Extracting half a long function into a private helper halves the reported complexity while changing nothing about the logic. If a threshold is enforced as a build gate, this is what teams do, and the codebase acquires helpers that exist purely to satisfy a linter. Complexity is a prompt to look, not a target to hit.

Where high complexity is legitimate

Parsers, state machines, validators and protocol implementations have genuinely many branches because the domain does. Splitting a coherent state machine into fragments to satisfy a threshold makes it harder to follow, not easier. The question is always whether the complexity is inherent in the problem or accidental in the solution.

Use it to prioritise, not to judge

The productive use is intersecting complexity with change frequency. A complex function nobody has touched in three years is stable and can be left alone. A complex function modified fifteen times this quarter is where defects will appear and where refactoring pays. Complexity alone ranks nothing useful; complexity crossed with churn ranks the actual risk.

Related tools

  • JSON Formatter — Format and beautify JSON in your browser. Pinpoints syntax errors by line and column, flags unsafe integers, and never uploads your data to a server.
  • JSON Validator — Validate JSON syntax with precise line and column errors, and check documents against a JSON Schema. Runs locally in your browser, nothing uploaded.
  • Regex Tester — Test regular expressions against sample text with live match highlighting, capture groups and flag control. Runs entirely in your browser.
  • HTML Minifier — Minify HTML by removing comments and redundant whitespace, without breaking inline elements or pre blocks. Runs entirely in your browser.

Frequently Asked Questions

What is cyclomatic complexity?

A count of linearly independent paths through a function: one plus the number of decision points such as if statements, loops, cases and short-circuiting operators. It is also the minimum test count for full branch coverage.

What is a good complexity score?

1 to 10 is simple, 11 to 20 moderate, 21 to 50 complex and hard to test, above 50 generally untestable. These are conventions, and the link to defects holds in aggregate rather than per function.

How does cognitive complexity differ?

It penalises nesting and rewards structures humans follow easily, so a flat twenty-case switch scores low while four levels of nested conditionals score high. It tracks readability better.

Can the metric be gamed?

Easily. Extracting half a function into a helper halves the score without changing the logic, which is exactly what happens when a threshold becomes a build gate.

How should I actually use complexity metrics?

Intersect complexity with change frequency. A complex function untouched for years is stable; a complex function changed fifteen times this quarter is where defects appear and refactoring pays.

Privacy & Security

Analysis done locally.

Data: None
Client-side-Side
Active
v1.0

About This Tool

This tool runs entirely in your browser. No data is sent to any server, ensuring complete privacy. Simply use the interface above to get started — no registration or login required.

Disclaimer: This tool is provided "as is" without warranty of any kind. Results are for educational and utility purposes.