indent-complexity
Language-agnostic code complexity analyzer that works on any language by measuring indentation depth
A language-agnostic code complexity analyzer. Works on any language by measuring indentation depth.
Usage
npm install indent-complexity
import { analyzeComplexity, analyzeDiffComplexity } from 'indent-complexity';
// Pass any code snippet, function or complete file to get complexity assessment:
const codeComplexity = analyzeComplexity(codeSnippet);
console.log(codeComplexity.score)
// Also supports `git diff` output to evaluate complexity for added lines:
const diffComplexity = analyzeDiffComplexity(gitDiff);
console.log(diffComplexity.score)
The Score
A single-metric approach inspired by Cyclomatic and Cognitive complexity:
Σ(depth²) / lineCount
Deeper nesting contributes exponentially more to the score. Default thresholds:
| Level | Score |
|---|---|
low | < 4 |
medium | 4 - 10 |
high | ≥ 10 |
Depth squared weighting mirrors cognitive complexity principles — nested code is harder to understand. Normalized by line count for cross-file comparison. Shallow-but-wide code scores low; deep code scores high.
Verbose Mode
verbose: true returns all research-backed metrics for experimentation:
| Metric | Description |
|---|---|
variance | Correlates with McCabe cyclomatic complexity |
max | Deepest nesting level |
mean | Average depth per line |
lineCount | Lines analyzed (excluding comments/blanks) |
depthHistogram | Distribution of depths |