This Claude skill provides a comprehensive framework for safe code refactoring and maintainability. It helps developers identify and resolve common 'code smells'—such as bloaters, object-oriented abusers, and change preventers—by applying industry-standard patterns like Extract Method and Polymorphism. By enforcing a test-driven workflow and incremental changes, it ensures that code improvements reduce complexity without introducing regressions, ultimately lowering technical debt and enhancing long-term project velocity.
Use Cases
Decomposing Monolithic Methods: Automatically identifying methods exceeding complexity thresholds and extracting them into smaller, testable functions to improve readability.
Decoupling Tightly Coupled Code: Resolving 'Feature Envy' and 'Shotgun Surgery' smells by moving methods and fields to their appropriate classes, making the system easier to change.
Modernizing Legacy Logic: Replacing brittle switch statements and nested conditionals with clean, polymorphic class structures to enhance extensibility.
Codebase Auditing and Cleanup: Scanning for 'Dispensables' like dead code, duplicate logic, or speculative abstractions to keep the repository lean and focused.
name
refactoring
description
Safe refactoring patterns and code improvement strategies. Auto-triggers when cleaning up code, reducing complexity, or improving maintainability.
Refactoring Skill
Golden Rules
Never refactor without tests - Tests are your safety net
Small steps - One change at a time, test after each
Keep it working - Code should pass tests at every step
Commit often - Easy to revert if something breaks
Code Smells to Address
Bloaters
Smell
Symptom
Refactoring
Long Method
>20 lines
Extract Method
Large Class
>200 lines
Extract Class
Long Parameter List
>3 params
Introduce Parameter Object
Data Clumps
Same fields appear together
Extract Class
Object-Orientation Abusers
Smell
Symptom
Refactoring
Switch Statements
Multiple type checks
Replace with Polymorphism
Parallel Inheritance
Every subclass needs partner
Merge Hierarchies
Refused Bequest
Subclass doesn't use parent
Replace Inheritance with Delegation
Change Preventers
Smell
Symptom
Refactoring
Divergent Change
One class changed for multiple reasons
Extract Class
Shotgun Surgery
One change affects many classes
Move Method/Field
Feature Envy
Method uses other class's data
Move Method
Dispensables
Smell
Symptom
Refactoring
Dead Code
Unused code
Delete
Duplicate Code
Same logic repeated
Extract Method
Speculative Generality
Unused abstraction
Collapse Hierarchy
Comments
Explaining bad code
Refactor until self-explanatory
Common Refactorings
Extract Method
# Before
def process_order(order):
# Validate order
if not order.items:
raise ValueError("Empty order")
if order.total < 0:
raise ValueError("Invalid total")
# ... more validation ...
# Calculate shipping
shipping = 0
if order.total > 100:
shipping = 0
elif order.weight < 1:
shipping = 5
else:
shipping = 10
# ... continue
# After
def process_order(order):
validate_order(order)
shipping = calculate_shipping(order)
# ... continue
def validate_order(order):
if not order.items:
raise ValueError("Empty order")
if order.total < 0:
raise ValueError("Invalid total")
def calculate_shipping(order):
if order.total > 100:
return 0
elif order.weight < 1:
return 5
return 10