* Welcome to my corner of the web! *

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:

LevelScore
low< 4
medium4 - 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:

MetricDescription
varianceCorrelates with McCabe cyclomatic complexity
maxDeepest nesting level
meanAverage depth per line
lineCountLines analyzed (excluding comments/blanks)
depthHistogramDistribution of depths