Code Complexity Calculator
How complex is your code and does it need refactoring?
Find out if your code is maintainable or needs refactoring. Enter lines of code, number of conditional statements (if/else), and loops — see cyclomatic complexity score and an estimated maintainability rating. Assumes standard procedural or object-oriented code structure.
Send feedback
💡 Share your idea or report a problem
✓ Thanks! We'll take a look.
Put this calculator on your site
Free embed code. Works anywhere. No signup.
Learn more
How It Works
The formula, explained simply
Code complexity grows faster than most developers expect. Ten if statements in a row create up to 2^10 = 1,024 possible paths, yet their complexity is only 11: McCabe counts independent (basis) paths, and 11 test cases can cover those. Testing every full path is what grows exponentially.
This calculator measures cyclomatic complexity using the McCabe method: start with 1, then add 1 for every conditional statement (if, else if, switch case) and every loop (for, while, do-while). The result tells you how many independent paths exist through your code. McCabe proposed 10 as an upper limit for a module, and NIST's Structured Testing guide (SP 500-235) recommends the same.
The maintainability index combines complexity with code volume and operator usage to predict long-term maintenance difficulty. The score runs from 0 to 100: Visual Studio rates 20 and above as good, 10 to 19 as moderate, and below 10 as hard to maintain. This calculator assumes standard procedural or object-oriented code structure without heavy use of functional programming patterns.
When To Use This
Right tool, right situation
Measure complexity during code reviews, especially for functions longer than 50 lines or containing more than 3 levels of nesting. Set up automated complexity monitoring in your build pipeline to catch complexity growth before it impacts maintainability. NIST recommends a limit of 10 and notes that limits up to 15 have been used successfully.
Calculate complexity when deciding whether to refactor existing code or add new features to complex functions. If a function already has high complexity, adding new conditionals will push it further into the danger zone. Sometimes it's faster to refactor first rather than work around existing complexity.
Use complexity measurements when estimating testing effort and planning code reviews. Functions with complexity over 10 require significantly more test cases and review time than simpler functions. Factor this into sprint planning and quality assurance resource allocation.
Common Mistakes
Why results sometimes look wrong
The biggest mistake is ignoring complexity until code reviews become painful. Complexity accumulates gradually — each new feature adds a few more conditionals, and suddenly a clean 50-line function becomes a 300-line monster with complexity over 20. Teams that measure complexity early catch this drift before it becomes expensive to fix.
Another common error is focusing only on line count instead of decision complexity. A 200-line function with mostly sequential logic (complexity 3) is often more maintainable than a 50-line function packed with nested conditionals (complexity 15). Lines of code matter for readability, but complexity sets how many paths you have to test.
Developers also mistake cyclomatic complexity for overall code quality. Low complexity doesn't automatically mean good code — it just means fewer decision points. A function can have low complexity but poor naming, unclear purpose, or tight coupling to other modules. Use complexity as one quality metric alongside others like cohesion, coupling, and test coverage.
The Math
Worked examples and deeper derivation
Cyclomatic complexity follows the formula: M = E - N + 2P, where E is the number of edges and N the number of nodes in the control-flow graph, and P is the number of connected components (usually 1). The simplified version used here is M = 1 + number of decision points, which counts each if, else if, case, for, while, and do-while as one decision point.
The maintainability index uses the formula: MI = 171 - 5.2 × ln(Halstead Volume) - 0.23 × Cyclomatic Complexity - 16.2 × ln(Lines of Code). The raw result tops out at 171, so the calculator rescales it to 0-100 the way Visual Studio does: MI × 100 / 171, and never below 0. Halstead Volume is program length times log2 of vocabulary, counted from operators and operands. This calculator does not count those, so it uses lines × log2(calls + conditionals + loops, at least 2) in their place, which makes the maintainability figure a rough estimate. Higher volume indicates more complex logic that requires greater cognitive load to comprehend.
For example, a 100-line function with 8 conditionals and 3 loops has complexity M = 1 + 8 + 3 = 12. If it also makes 4 function calls, the stand-in Halstead Volume would be 100 × log₂(4 + 8 + 3) = 100 × log₂(15) ≈ 391. This produces a raw index of about 63, which is about 37 on the 0-100 scale: inside the good band, though a complexity of 12 still argues for splitting the function.
Expert Unlock
The thing most explanations skip
The McCabe complexity formula treats all decision points equally, but not all conditionals create equal maintenance burden. Guard clauses and early returns often reduce cognitive load while leaving the McCabe score unchanged. Modern static analysis tools like SonarQube use modified complexity metrics that weight different types of branching differently — nested conditions score higher than sequential guard clauses.
What cyclomatic complexity score means my code needs refactoring?
Need something this doesn't cover?
Suggest a tool — we'll build it →