Code Complexity Scanner
Overview
This skill analyzes source code to measure cyclomatic complexity, cognitive complexity, and function length. It identifies the most complex functions and files in a codebase, helping teams focus refactoring efforts on the code that's hardest to maintain and most likely to harbor bugs.
Instructions
Step 1: Identify Target Language and Files
Detect the primary language from file extensions and package files. Filter to source code only (exclude node_modules, vendor, dist, build, pycache, .git).
Step 2: Measure Cyclomatic Complexity
For each function/method, count:
if, elif/else if → +1 each
for, while, do-while → +1 each
case in switch (each case) → +1 each
catch → +1
&&, || in conditions → +1 each
- Ternary
?: → +1
- Base complexity starts at 1
Step 3: Measure Cognitive Complexity
More nuanced than cyclomatic — penalizes nesting:
- Each control flow break: +1
- Each nesting level: +1 additional per level
- Recursion: +1
- Boolean operator sequences that switch: +1
Step 4: Identify Hotspot Files
Rank files by:
- Maximum function complexity in the file
- Average complexity across all functions
- Number of functions above threshold (default: 15)
- Total lines of code
Step 5: Output Report
Present findings in three tiers:
- 🔴 Critical (complexity > 25): Must refactor — these functions are untestable
- 🟡 Warning (complexity 15-25): Should refactor when next modifying
- 🟢 OK (complexity < 15): Acceptable maintainability
Examples
Example 1: TypeScript Project Scan
User prompt:
Scan src/ for the most complex functions
Agent output:
Complexity Report — src/ (89 files scanned)
🔴 CRITICAL (complexity > 25)
src/billing/calculator.ts
calculateInvoiceTotal() Cyclomatic: 34 Cognitive: 48 Lines: 180
applyDiscountRules() Cyclomatic: 26 Cognitive: 31 Lines: 95
src/api/middleware/auth.ts
validateAndRefreshToken() Cyclomatic: 28 Cognitive: 35 Lines: 120
🟡 WARNING (complexity 15-25)
src/workers/sync.ts
reconcileRecords() Cyclomatic: 19 Cognitive: 24 Lines: 88
src/utils/validators.ts
validateAddress() Cyclomatic: 16 Cognitive: 18 Lines: 65
Summary:
Files scanned: 89 | Functions analyzed: 412
Average complexity: 6.8 | Median: 4
Functions above threshold: 14 (3.4%)
Recommended target: reduce critical functions below 15
Example 2: Python Project with Trend
User prompt:
What are the complexity hotspots in our Django app?
Agent output:
Complexity Hotspots — myapp/ (67 files)
Top 10 by Cyclomatic Complexity:
Rank Function CC Lines File
1 process_order() 38 340 views/checkout.py
2 generate_report() 27 210 reports/builder.py
3 sync_inventory() 24 180 tasks/inventory.py
4 parse_import_file() 22 150 importers/csv.py
5 calculate_shipping() 19 95 shipping/rates.py
Nesting depth violations (>4 levels):
views/checkout.py:142 — 6 levels deep in process_order()
reports/builder.py:89 — 5 levels deep in generate_report()
Recommended refactoring order: process_order() first (highest complexity,
most changed file per git history, 340 lines is 3x recommended max).
Guidelines
- Thresholds are configurable — default 15 works for most teams, but ask the user if they have a team standard
- Cognitive > cyclomatic for human readability — cognitive complexity better captures how hard code is to understand
- Context matters — a parser with complexity 20 might be acceptable; a controller with complexity 20 needs splitting
- Combine with change frequency — complex code that never changes is less urgent than complex code edited weekly
- Don't count generated code — exclude auto-generated files, migrations, and schema definitions
- Suggest specific refactorings — "Extract method" for long functions, "Replace conditional with polymorphism" for deep switch statements, "Introduce parameter object" for functions with 5+ parameters
1---2name: code-complexity-scanner3description: >-4 Measures cyclomatic complexity, cognitive complexity, and function length5 across codebases to identify maintenance hotspots. Use when someone asks6 about code complexity, function length analysis, maintainability metrics,7 or needs to find the most complex parts of their codebase. Trigger words:8 complexity, cyclomatic, cognitive complexity, long functions, hotspots,9 maintainability index, code metrics.10license: Apache-2.011compatibility: "Works with any language; best results with JS/TS, Python, Go, Java"12metadata:13 source: https://skillmd.com/skills/eliferjunior/code-complexity-scanner14 author: terminal-skills15 version: "1.0.0"16 category: development17 tags: ["code-quality", "complexity", "metrics", "static-analysis"]18---1920# Code Complexity Scanner2122## Overview2324This skill analyzes source code to measure cyclomatic complexity, cognitive complexity, and function length. It identifies the most complex functions and files in a codebase, helping teams focus refactoring efforts on the code that's hardest to maintain and most likely to harbor bugs.2526## Instructions2728### Step 1: Identify Target Language and Files2930Detect the primary language from file extensions and package files. Filter to source code only (exclude node_modules, vendor, dist, build, __pycache__, .git).3132### Step 2: Measure Cyclomatic Complexity3334For each function/method, count:35- `if`, `elif`/`else if` → +1 each36- `for`, `while`, `do-while` → +1 each37- `case` in switch (each case) → +1 each38- `catch` → +139- `&&`, `||` in conditions → +1 each40- Ternary `?:` → +141- Base complexity starts at 14243### Step 3: Measure Cognitive Complexity4445More nuanced than cyclomatic — penalizes nesting:46- Each control flow break: +147- Each nesting level: +1 additional per level48- Recursion: +149- Boolean operator sequences that switch: +15051### Step 4: Identify Hotspot Files5253Rank files by:541. Maximum function complexity in the file552. Average complexity across all functions563. Number of functions above threshold (default: 15)574. Total lines of code5859### Step 5: Output Report6061Present findings in three tiers:62- 🔴 **Critical** (complexity > 25): Must refactor — these functions are untestable63- 🟡 **Warning** (complexity 15-25): Should refactor when next modifying64- 🟢 **OK** (complexity < 15): Acceptable maintainability6566## Examples6768### Example 1: TypeScript Project Scan6970**User prompt:**71```72Scan src/ for the most complex functions73```7475**Agent output:**76```77Complexity Report — src/ (89 files scanned)7879🔴 CRITICAL (complexity > 25)80 src/billing/calculator.ts81 calculateInvoiceTotal() Cyclomatic: 34 Cognitive: 48 Lines: 18082 applyDiscountRules() Cyclomatic: 26 Cognitive: 31 Lines: 958384 src/api/middleware/auth.ts85 validateAndRefreshToken() Cyclomatic: 28 Cognitive: 35 Lines: 1208687🟡 WARNING (complexity 15-25)88 src/workers/sync.ts89 reconcileRecords() Cyclomatic: 19 Cognitive: 24 Lines: 8890 src/utils/validators.ts91 validateAddress() Cyclomatic: 16 Cognitive: 18 Lines: 659293Summary:94 Files scanned: 89 | Functions analyzed: 41295 Average complexity: 6.8 | Median: 496 Functions above threshold: 14 (3.4%)97 Recommended target: reduce critical functions below 1598```99100### Example 2: Python Project with Trend101102**User prompt:**103```104What are the complexity hotspots in our Django app?105```106107**Agent output:**108```109Complexity Hotspots — myapp/ (67 files)110111Top 10 by Cyclomatic Complexity:112 Rank Function CC Lines File113 1 process_order() 38 340 views/checkout.py114 2 generate_report() 27 210 reports/builder.py115 3 sync_inventory() 24 180 tasks/inventory.py116 4 parse_import_file() 22 150 importers/csv.py117 5 calculate_shipping() 19 95 shipping/rates.py118119Nesting depth violations (>4 levels):120 views/checkout.py:142 — 6 levels deep in process_order()121 reports/builder.py:89 — 5 levels deep in generate_report()122123Recommended refactoring order: process_order() first (highest complexity,124most changed file per git history, 340 lines is 3x recommended max).125```126127## Guidelines128129- **Thresholds are configurable** — default 15 works for most teams, but ask the user if they have a team standard130- **Cognitive > cyclomatic for human readability** — cognitive complexity better captures how hard code is to understand131- **Context matters** — a parser with complexity 20 might be acceptable; a controller with complexity 20 needs splitting132- **Combine with change frequency** — complex code that never changes is less urgent than complex code edited weekly133- **Don't count generated code** — exclude auto-generated files, migrations, and schema definitions134- **Suggest specific refactorings** — "Extract method" for long functions, "Replace conditional with polymorphism" for deep switch statements, "Introduce parameter object" for functions with 5+ parameters