Code Complexity Analyzer
Measure lines of code, cyclomatic complexity, nesting depth, and maintainability
Acerca de esta herramienta
Paste any code snippet and get instant, language-agnostic metrics: total lines, blank lines, comment lines, and code lines; cyclomatic complexity (count of decision points — if, else, for, while, switch, case, &&, ||); function count (function, def, func keywords); maximum nesting depth (by tracking indentation changes); and a maintainability score derived from LOC and complexity. Each metric comes with an interpretation badge (Good / Fair / Poor) so you can prioritise refactoring work. Practical tips for reducing complexity are shown inline whenever a metric exceeds the recommended threshold.
Cómo usar
- 1 Paste your code snippet into the text area.
- 2 Click 'Analyze' — metrics appear instantly below the editor.
- 3 Check the colour-coded badges: green means Good, yellow is Fair, red is Poor.
- 4 Review the inline tips for any metric flagged as Fair or Poor.
- 5 Refactor your code and re-paste to see the updated scores.
What "complexity" actually measures in code
Two programs can do the same job and yet differ wildly in how hard they are to read, test, and change safely. Complexity metrics put a number on that difference. They don't tell you whether code is correct — they estimate how much mental effort and how many test cases a human will need to work with it. This analyzer computes four classic structural metrics directly from the text you paste, using pattern matching rather than a real parser, which is why it stays language-agnostic across C-style languages, Python, Ruby, PHP, Go, and more.
Cyclomatic complexity, and how this tool counts it
Cyclomatic complexity counts the number of independent paths through a piece of code — roughly, the minimum number of test cases needed to exercise every branch at least once. The tool starts at 1 (the single straight-line path) and adds one for each decision point it finds: every if, else if, else, for, while, do, switch, case, catch, ternary ?, and every logical && or ||. Boolean operators count because each one adds a short-circuit branch that can be true or false independently.
| Score | Meaning | Badge |
|---|---|---|
| 1–10 | Simple, easy to test | Good |
| 11–20 | Moderate — watch it | Fair |
| 21+ | Hard to test and reason about | Poor |
A worked example
Suppose you paste this function:
function rate(u){ if(u.age > 18 && u.verified){ return "ok"; } else if(u.flagged || u.banned){ return "no"; } return "review"; }
The analyzer finds one if, one else if, one &&, and one || — four decision points. Added to the base path of 1, the cyclomatic complexity is 5. It also counts the function keyword (function count 1), measures nesting by tracking { and } (max depth here is 2, function body plus the conditional blocks), and classifies each line as blank, comment, or code. With so few code lines and low complexity, the maintainability score stays near the top of the range.
The maintainability score
The maintainability score is a single 0–100 number that summarises the others. It begins at 100 and subtracts three penalties: up to 30 points for the number of code lines, up to 40 for cyclomatic complexity above the base, and up to 20 for nesting depth. So a long, deeply nested file packed with branches loses points on all three fronts. Seventy and above is Good, 40–69 is Fair, below 40 is Poor. Treat it as a relative gauge — useful for comparing two versions of the same file before and after a refactor, not as an absolute grade.
How nesting depth is detected
Nesting depth here is brace depth: the tool walks line by line, adding the count of { and subtracting }, and records the deepest level reached. Deep nesting is one of the strongest predictors of bugs, because every extra level multiplies the conditions a reader must hold in their head at once. A function buried five braces deep forces you to remember five surrounding conditions to understand the innermost line. Because the count is brace-based, indentation-only languages like Python will report a low or zero depth even when the logic is deeply nested — read that metric with the language in mind.
Reading the results and acting on them
- High complexity, low line count. A short function with complexity over 10 is doing too much branching. Split it, or replace an if/else chain with a lookup table or map.
- Deep nesting. Use guard clauses and early returns. Inverting a condition to
returnearly flattens the body and usually drops the depth by a level or two. - Zero functions but many code lines. Top-level script with no structure is hard to reuse and test; group related logic into named functions.
- Lots of comment lines around little code. Sometimes a sign the code needs comments to be understood at all — clearer names can let the code explain itself.
Common mistakes when interpreting the numbers
The biggest error is chasing a low score for its own sake. Some genuinely complex domains — parsers, state machines, financial rules — carry irreducible complexity, and forcing the number down by hiding branches inside helper functions can make the code harder to follow. Another trap: because the analyzer matches keywords rather than parsing, the word if inside a string or comment can inflate the count slightly, and unusual or macro-heavy syntax may be approximate. Use the metrics to find candidates for review, then judge each one with your eyes. Everything is computed in your browser as you type; nothing you paste is sent anywhere.
Preguntas frecuentes
Formatea, valida y minifica JSON — con señalización exacta de errores de sintaxis.
Pruebe expresiones regulares con resaltado de coincidencias en tiempo real, grupos de captura y modo de reemplazo.
Compare dos bloques de texto y resalte adiciones, eliminaciones y cambios — diferencia línea por línea con resaltado a nivel de palabra.