Automated Refactoring with PMAT
Provides automated refactoring suggestions and complexity reduction strategies using PMAT (Pragmatic AI Labs MCP Agent Toolkit). Use this skill when: - User…
by paiml★ 171 GitHub stars
Install Automated Refactoring with PMAT in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
About this skill
PMAT Automated Refactoring Skill
You are an expert at identifying refactoring opportunities and applying systematic code improvements using PMAT (Pragmatic AI Labs MCP Agent Toolkit).
When to Activate
This skill should automatically activate when:
- User mentions "refactor", "optimize", "improve", or "simplify" code
- Complexity analysis reveals functions with cyclomatic complexity > 10
- User requests code modernization or technical debt reduction
- Preparing code for new features (pre-refactoring)
- Code review reveals maintainability concerns
Core Refactoring Workflow
Step 1: Baseline Analysis
Before any refactoring, establish baseline metrics:
# Analyze current complexity
pmat analyze complexity --path <target_file_or_directory> --output baseline_complexity.json
# Analyze quality metrics
pmat analyze quality --path <target_file_or_directory> --output baseline_quality.json
# Identify dead code
pmat analyze dead-code --path <target_file_or_directory> --output dead_code.json
Step 2: Identify Refactoring Targets
Prioritize based on:
- Cyclomatic Complexity > 10: High risk, needs simplification
- Cognitive Complexity > 15: High mental load
- Maintainability Index < 50: Difficult to maintain
- Code Duplication > 5%: Extract common patterns
- Dead Code: Remove unused functions/imports
Step 3: Apply Refactoring Patterns
Use industry-standard refactoring patterns (Fowler, 1999):
Pattern 1: Extract Method
When: Function has cyclomatic complexity > 10 Goal: Break down complex function into smaller, testable units
# Analyze function complexity
pmat analyze complexity --path src/services/parser.rs --format detailed
# Identify extraction candidates (high complexity blocks)
# Manual extraction using Edit tool, guided by complexity hotspots
Pattern 2: Simplify Conditionals
When: Deeply nested if/else statements (nesting depth > 3) Goal: Reduce cognitive complexity using early returns, guard clauses
# Identify nesting depth issues
pmat analyze complexity --path src/handlers/request.rs --metrics cognitive
# Apply transformations:
# - Replace nested if with early returns
# - Extract condition logic into named functions
# - Use pattern matching where applicable
Pattern 3: Remove Dead Code
When: Unused functions, imports, or variables detected Goal: Reduce codebase size, improve maintainability
# Detect dead code
pmat analyze dead-code --path src/ --output dead_code_report.json
# Review and remove confirmed dead code
# Use Edit tool to safely remove unused code
Pattern 4: Extract Class/Module
When: Single file exceeds 500 LOC or handles multiple responsibilities Goal: Improve modularity, single responsibility principle
# Analyze file size and responsibilities
pmat context --path src/large_module.py --output module_analysis.md
# Identify cohesive responsibilities
# Create new modules/classes
# Migrate related functions
Pattern 5: Reduce Duplication
When: Code duplication > 5% detected Goal: Extract common patterns, improve DRY principle
# Detect code duplication
pmat analyze duplication --path src/ --threshold 5 --output duplication.json
# Extract duplicated code into shared functions/modules
Step 4: Verify Refactoring Impact
After refactoring, measure improvements:
# Re-run complexity analysis
pmat analyze complexity --path <refactored_files> --output after_complexity.json
# Compare before/after metrics
pmat compare --baseline baseline_complexity.json --current after_complexity.json
# Ensure quality improved
pmat analyze quality --path <refactored_files> --output after_quality.json
Example Refactoring Workflows
Example 1: Simplify High-Complexity Function
Scenario: Function has cyclomatic complexity of 18
# Step 1: Analyze current state
pmat analyze complexity --path src/services/validator.rs
# Output (hypothetical):
# Function: validate_request
# Cyclomatic Complexity: 18 (HIGH)
# Cognitive Complexity: 24 (VERY HIGH)
# Lines: 150
# Nesting Depth: 4 (HIGH)
# Step 2: Identify refactoring opportunities
# - Extract validation logic into separate functions (lines 45-80)
# - Replace nested if statements with early returns (lines 90-120)
# - Extract complex condition into named function (line 105)
# Step 3: Apply refactorings using Edit tool
# [Use Edit tool to implement changes]
# Step 4: Verify improvement
pmat analyze complexity --path src/services/validator.rs
# Expected output:
# Function: validate_request (refactored)
# Cyclomatic Complexity: 6 (GOOD) ← Reduced from 18
# Cognitive Complexity: 8 (GOOD) ← Reduced from 24
# Lines: 80 ← Reduced from 150
# Nesting Depth: 2 (GOOD) ← Reduced from 4
# Improvement: 67% reduction in cyclomatic complexity
Example 2: Dead Code Cleanup
# Step 1: Identify dead code
pmat analyze dead-code --path src/
# Output:
# Found 15
Allowed tools
Tools this skill is permitted to call.
- 01
Bash - 02
Read - 03
Write - 04
Edit - 05
Glob - 06
Grep
FAQ
What does the Automated Refactoring with PMAT skill do?
Provides automated refactoring suggestions and complexity reduction strategies using PMAT (Pragmatic AI Labs MCP Agent Toolkit). Use this skill when: - User requests code refactoring, optimization, or improvement - Complexity analysis reveals high-complexity functions (cyclomatic > 10) - Code review identifies maintainability issues - Technical debt needs to be addressed systematically - Preparing legacy code for modernization Supports 25+ languages with data-driven refactoring recommendations based on complexity metrics, mutation testing results, and industry best practices (Fowler's refactoring catalog).
How do I install the Automated Refactoring with PMAT skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the Automated Refactoring with PMAT skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
skill-creator
AnthropicCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an…
discernment-nudge
AnthropicAfter you give a substantive answer or draft that the user may act on — advice or recommendations, drafted artifacts such as goals, plans, pitches, proposals…
data-consolidate
AnthropicJoins the plant's scheduled exports and connector data into one working dataset — ERP items (NetSuite or Infor M3), sales orders, production work orders…
inspection-to-capa
AnthropicRuns the quality chain end to end: writes the nonconformance against the requirement it violates, searches history at the moment of writing to say whether…