complexity
Find focused refactor hotspots.
What this skill does
# Complexity Skill **YOU MUST EXECUTE THIS WORKFLOW. Do not just describe it.** Analyze code complexity to identify refactoring targets. ## Execution Steps Given `/complexity [path]`: ### Step 1: Determine Target **If path provided:** Use it directly. **If no path:** Use current directory or recent changes: ```bash git diff --name-only HEAD~5 2>/dev/null | grep -E '\.(py|go)$' | head -10 ``` ### Step 2: Detect Language ```bash # Check for Python files ls *.py **/*.py 2>/dev/null | head -1 && echo "Python detected" # Check for Go files ls *.go **/*.go 2>/dev/null | head -1 && echo "Go detected" ``` ### Step 3: Run Complexity Analysis **For Python (using radon):** ```bash # Check if radon is installed which radon || pip install radon # Run cyclomatic complexity radon cc <path> -a -s # Run maintainability index radon mi <path> -s ``` **For Go (using gocyclo):** ```bash # Check if gocyclo is installed which gocyclo || go install github.com/fzipp/gocyclo/cmd/gocyclo@latest # Run complexity analysis gocyclo -over 10 <path> ``` ### Step 4: Interpret Results **Cyclomatic Complexity Grades:** | Grade | CC Score | Meaning | |-------|----------|---------| | A | 1-5 | Low risk, simple | | B | 6-10 | Moderate, manageable | | C | 11-20 | High risk, complex | | D | 21-30 | Very high risk | | F | 31+ | Untestable, refactor now | ### Step 5: Identify Refactor Targets List functions/methods that need attention: - CC > 10: Should refactor - CC > 20: Must refactor - CC > 30: Critical, immediate action ### Step 6: Write Complexity Report **Write to:** `.agents/complexity/YYYY-MM-DD-<target>.md` ```markdown # Complexity Report: <Target> **Date:** YYYY-MM-DD **Language:** <Python/Go> **Files Analyzed:** <count> ## Summary - Average CC: <score> - Highest CC: <score> in <function> - Functions over threshold: <count> ## Refactor Targets ### Critical (CC > 20) | Function | File | CC | Recommendation | |----------|------|-----|----------------| | <name> | <file:line> | <score> | <how to simplify> | ### High (CC 11-20) | Function | File | CC | Recommendation | |----------|------|-----|----------------| | <name> | <file:line> | <score> | <how to simplify> | ## Refactoring Recommendations 1. **<Function>**: <specific suggestion> - Extract: <what to extract> - Simplify: <how to simplify> ## Next Steps - [ ] Address critical complexity first - [ ] Create issues for high complexity - [ ] Consider refactoring sprint ``` ### Step 7: Report to User Tell the user: 1. Overall complexity summary 2. Number of functions over threshold 3. Top 3 refactoring targets 4. Location of full report 5. Run `/refactor <function>` to address critical complexity targets ## Key Rules - **Use the right tool** - radon for Python, gocyclo for Go - **Focus on high CC** - prioritize 10+ - **Provide specific fixes** - not just "refactor this" - **Write the report** - always produce artifact ## Quick Reference **Simplifying High Complexity:** - Extract helper functions - Replace conditionals with polymorphism - Use early returns - Break up long functions - Simplify nested loops ## Examples ### Analyzing Python Project **User says:** `/complexity src/` **What happens:** 1. Agent detects Python files in `src/` directory 2. Agent checks for radon installation, installs if missing 3. Agent runs `radon cc src/ -a -s` for cyclomatic complexity 4. Agent runs `radon mi src/ -s` for maintainability index 5. Agent identifies 3 functions with CC > 20, 7 functions with CC 11-20 6. Agent writes detailed report to `.agents/complexity/2026-02-13-src.md` 7. Agent recommends extracting nested conditionals in `process_request()` function **Result:** Complexity report identifies `process_request()` (CC: 28) as critical refactor target with specific extraction recommendations. ### Finding Refactor Targets in Go Module **User says:** `/complexity` **What happens:** 1. Agent checks recent changes with `git diff --name-only HEAD~5` 2. Agent detects Go files, verifies gocyclo installation 3. Agent runs `gocyclo -over 10 ./...` on project 4. Agent finds `HandleWebhook()` function with complexity 34 5. Agent writes report with recommendation to extract validation logic 6. Agent reports top 3 targets: HandleWebhook (34), ProcessBatch (22), ValidateInput (15) **Result:** Critical function identified for immediate refactoring with actionable extraction plan. ## See Also - [refactor](../refactor/SKILL.md) — Safe, verified refactoring for complexity targets ## Troubleshooting | Problem | Cause | Solution | |---------|-------|----------| | Tool not installed (radon/gocyclo) | Missing dependency | Agent auto-installs: `pip install radon` for Python or `go install github.com/fzipp/gocyclo/cmd/gocyclo@latest` for Go. Verify install path in $PATH. | | No complexity issues found | Threshold too high or genuinely simple code | Lower threshold: try `gocyclo -over 5` or check if path includes actual implementation files vs tests. | | Report shows functions without recommendations | Generic analysis without codebase context | Read the high-CC functions to understand structure, then provide specific refactoring suggestions based on actual code patterns. | | Mixed language project | Multiple languages in target path | Run analysis separately per language: `/complexity src/python/` then `/complexity src/go/`, combine reports manually. | ## Reference Documents - [references/complexity.feature](references/complexity.feature) — Executable spec: rank functions by cyclomatic complexity, scope to recent changes by default, focused hotspot list (soc-qk4b)
Related in Code Review
gstack
IncludedFast headless browser for QA testing and site dogfooding. Navigate pages, interact with elements, verify state, diff before/after, take annotated screenshots, test responsive layouts, forms, uploads, dialogs, and capture bug evidence. Use when asked to open or test a site, verify a deployment, dogfood a user flow, or file a bug with screenshots. (gstack)
startup-due-diligence
IncludedLegal due diligence review for seed-stage and Series A startups (US, Delaware C-Corp focus). Supports both investor and founder perspectives. Capabilities include: (1) Interactive document review and issue spotting; (2) Document request list generation; (3) Cap table and SAFE/convertible note analysis; (4) Red flag identification with severity ratings; (5) Diligence report generation. TRIGGERS: due diligence, DD, startup investment, cap table review, Series A, seed round, investor diligence, legal review startup, SAFE analysis, convertible note, 409A, founder vesting.
interview-master
IncludedThis skill should be used when the user asks to "generate interview questions", "prepare for interview", "optimize resume", "conduct mock interview", "analyze git commits for resume", "generate resume from code", "review my resume", or mentions interview preparation, career assistance, or extracting project experience from git history. Provides comprehensive interview and career development guidance for both job seekers and interviewers.
fix-issue
IncludedFixes GitHub issues using parallel analysis agents for root cause investigation, code exploration, and regression detection. Reads issue context from gh CLI, searches codebase and memory for related patterns, generates a fix with tests, and links the resolution back to the issue via PR. Includes prevention analysis to avoid recurrence. Use when debugging errors, resolving regressions, fixing bugs, or triaging issues.
sf-apex
IncludedGenerates and reviews Salesforce Apex code with 150-point scoring. TRIGGER when: user writes, reviews, or fixes Apex classes, triggers, test classes, batch/queueable/schedulable jobs, or touches .cls/.trigger files. DO NOT TRIGGER when: LWC JavaScript (use sf-lwc), Flow XML (use sf-flow), SOQL-only queries (use sf-soql), or non-Salesforce code.
swift-development
IncludedComprehensive Swift development for building, testing, and deploying iOS/macOS applications. Use when Claude needs to: (1) Build Swift packages or Xcode projects from command line, (2) Run tests with XCTest or Swift Testing framework, (3) Manage iOS simulators with simctl, (4) Handle code signing, provisioning profiles, and app distribution, (5) Format or lint Swift code with SwiftFormat/SwiftLint, (6) Work with Swift Package Manager (SPM), (7) Implement Swift 6 concurrency patterns (async/await, actors, Sendable), (8) Create SwiftUI views with MVVM architecture, (9) Set up Core Data or SwiftData persistence, or any other Swift/iOS/macOS development tasks.