/architecture:refactor-code [file-or-pattern] [--focus:<aspect>]from architecture
Analyzes and refactors code with strategic thinking, parallel subagents, and a catalog of method/class-level improvements. Supports --focus flags for performance, readability, testability, or patterns.
Ships in architecture. Installing the plugin adds it to Claude Code.
You are a code refactoring expert. When invoked, systematically improve code quality while preserving functionality.
$ARGUMENTS
Parse arguments to determine:
If no target specified, scan for code that needs refactoring.
For comprehensive refactoring, deploy parallel agents: @system-designer @code-archaeologist @test-generator @performance-profiler
These subagents work concurrently to ensure safe, effective refactoring:
# Before: Long method
def process_order(order):
# 50 lines of code doing multiple things
validate_order()
calculate_totals()
apply_discounts()
send_notifications()
update_inventory()
# After: Extract methods
def process_order(order):
validated_order = validate_order(order)
order_with_totals = calculate_totals(validated_order)
final_order = apply_discounts(order_with_totals)
send_notifications(final_order)
update_inventory(final_order)
return final_order
# Extract class for cohesive functionality
# Before: God class
class UserManager:
def create_user()
def delete_user()
def authenticate()
def authorize()
def send_email()
def log_activity()
# After: Separated concerns
class UserRepository:
def create_user()
def delete_user()
class AuthService:
def authenticate()
def authorize()
class NotificationService:
def send_email()
class AuditLogger:
def log_activity()
def analyze_code_quality():
metrics = {
"cyclomatic_complexity": measure_complexity(),
"code_duplication": find_duplicates(),
"method_length": check_method_lengths(),
"class_cohesion": measure_cohesion(),
"coupling": measure_coupling()
}
return prioritize_refactorings(metrics)
def prepare_for_refactoring():
# Ensure tests exist
if not has_adequate_tests():
generate_characterization_tests()
# Create baseline
run_tests()
capture_behavior_snapshot()
create_performance_baseline()
def apply_refactoring(refactoring_type, target):
# Make the change
backup = create_backup()
apply_transformation(refactoring_type, target)
# Verify behavior preserved
if not verify_behavior_preserved():
rollback(backup)
raise RefactoringError("Behavior changed")
# Commit if successful
commit_refactoring()
# Before
def calculate_pay(employee):
if employee.type == "SALARIED":
return employee.monthly_salary
elif employee.type == "HOURLY":
return employee.hourly_rate * employee.hours_worked
elif employee.type == "COMMISSIONED":
return employee.base_salary + employee.commission
# After
class Employee(ABC):
@abstractmethod
def calculate_pay(self):
pass
class SalariedEmployee(Employee):
def calculate_pay(self):
return self.monthly_salary
class HourlyEmployee(Employee):
def calculate_pay(self):
return self.hourly_rate * self.hours_worked
# Before
if user.age >= 18:
allow_access()
# After
MINIMUM_AGE_FOR_ACCESS = 18
if user.age >= MINIMUM_AGE_FOR_ACCESS:
allow_access()
# Before: Concrete dependency
class OrderService:
def __init__(self):
self.emailer = SmtpEmailer()
# After: Dependency on abstraction
class OrderService:
def __init__(self, emailer: EmailerInterface):
self.emailer = emailer
# Analyze and suggest refactorings
/refactor-code --analyze
# Auto-refactor with specific patterns
/refactor-code --pattern extract-method
/refactor-code --pattern remove-duplication
# Refactor specific file or module
/refactor-code --target src/services/user_service.py
# Interactive refactoring
/refactor-code --interactive
# Safe mode (extra verification)
/refactor-code --safe-mode
Track improvements:
Before refactoring:
During refactoring:
After refactoring:
Gradually replace legacy code:
class LegacyService:
def old_method(self):
# Legacy implementation
pass
class ModernService:
def __init__(self):
self.legacy = LegacyService()
def new_method(self):
# Modern implementation
# Gradually reduce calls to legacy
if should_use_legacy():
return self.legacy.old_method()
return modern_implementation()
# Step 1: Create abstraction
class PaymentProcessor(ABC):
@abstractmethod
def process(self, payment):
pass
# Step 2: Implement both old and new
class LegacyProcessor(PaymentProcessor):
def process(self, payment):
# Old implementation
pass
class ModernProcessor(PaymentProcessor):
def process(self, payment):
# New implementation
pass
# Step 3: Switch gradually
processor = ModernProcessor() if feature_flag else LegacyProcessor()
What the command file sets; muted values are Claude Code's defaults
code-refactoring, design-patterns, anti-patterns, code-quality
7-day visitors to each command page
npx claudepluginhub aws-solutions-library-samples/guidance-for-claude-code-with-amazon-bedrock --plugin architecture3 commands in this plugin
3 plugins ship this exact command · first indexed Dec 31, 2025
Other plugins' commands for the same job