refactoring-patternslisted
Install: claude install-skill akaszubski/autonomous-dev
# Refactoring Patterns
Safe techniques for restructuring code without changing its behavior. Every refactoring follows: test green -> refactor -> test green.
## Golden Rule
**Never refactor and change behavior in the same commit.** Refactoring = same behavior, different structure. Feature work = different behavior. Mixing them makes bugs untraceable.
## Pre-Refactoring Checklist
Before any refactoring:
- [ ] Tests exist and pass for the code being refactored
- [ ] You can describe what the code does WITHOUT reading it line by line
- [ ] The refactoring has a clear motivation (not "it could be cleaner")
- [ ] The scope is bounded — you know exactly which files/functions change
## Core Refactorings
### 1. Extract Function
**When**: A code block does one identifiable thing, or you need to add a comment explaining what a block does.
```python
# BEFORE
def process_order(order):
# Validate order
if not order.items:
raise ValueError("Empty order")
if order.total < 0:
raise ValueError("Negative total")
if not order.customer_id:
raise ValueError("Missing customer")
# Apply discounts
total = order.total
if order.is_member:
total *= 0.9
if len(order.items) > 10:
total *= 0.95
return total
# AFTER
def process_order(order):
validate_order(order)
return apply_discounts(order)
def validate_order(order):
if not order.items:
raise ValueError("Empty order")
if order.total < 0: