Refactor with tests first, one change at a time, never mix refactoring with bug fixes or new features
Copy the install command and let the AI configure it · recommended for beginners
Please install the "Refactoring Safely" skill from askskill: 1. Download https://raw.githubusercontent.com/obra/clank/main/skills/coding/refactoring-safely/SKILL.md 2. Save it as ~/.claude/skills/refactoring-safely/SKILL.md 3. Reload skills and tell me it's ready
Refactoring means changing code structure without changing behavior. Safe refactoring requires tests first, one change at a time, and NEVER mixing refactoring with bug fixes or features.
Core principle: Refactoring changes HOW code works internally. Bug fixes change WHAT code does. Never mix HOW and WHAT changes.
Violating the letter of this rule is violating the spirit of safe refactoring.
Use before any refactoring:
Critical moments:
LAW 1: TESTS BEFORE REFACTORING (NO EXCEPTIONS)
LAW 2: ONE CHANGE AT A TIME
LAW 3: NEVER MIX REFACTORING WITH BUG FIXES OR FEATURES
Violating any law makes refactoring unsafe.
Before refactoring ANY code, you MUST have passing tests.
No exceptions:
Process:
❌ Without tests (from baseline):
# Refactor immediately
def calc(x, y, z): # Bad names
...
# Refactor to:
def calculate_capped_product(first, second, multiplier):
...
# Hope it still works!
✅ With tests first:
# Step 1: Add tests
def test_calc():
assert calc(2, 3, 4) == 20
assert calc(10, 20, 5) == 100 # Capped
# Run - ALL PASS
# Step 2: Refactor
def calculate_capped_product(first, second, multiplier):
sum_of_addends = first + second
uncapped = sum_of_addends * multiplier
return min(uncapped, 100)
# Step 3: Run tests again - VERIFY STILL PASS
Tests prove refactoring didn't break behavior.
Make ONE structural change. Test. Commit. Repeat.
Don't:
Do:
❌ Multiple changes (risky):
# Change 1: Extract validation
# Change 2: Extract calculation
# Change 3: Extract persistence
# Change 4: Rename variables
# Change 5: Reorder statements
# Change 6: Add error handling
# If tests fail: which change broke it?
✅ Incremental (safe):
# Change 1: Extract validation
def validate_order(data):
...
# Run tests → PASS → Commit
# Change 2: Extract calculation
def calculate_total(items):
...
# Run tests → PASS → Commit
# If any test fails: know exactly which change broke it
From baseline: Agent chose incremental approach correctly. Skill reinforces this.
This is the critical discipline most developers violate.
Refactoring = change structure, preserve behavior Bug fix = change behavior to correct it
Never do both in the same commit.
You're refactoring and discover a bug:
# Extracting validation from larger function
def validate_order_items(order_data):
if not order_data.get('items'):
return None # ← BUG: should raise error
Tempting reasoning:
Why this is WRONG:
…
Dispatch multiple agents in parallel to investigate and fix independent issues.
Helps developers finish a branch with merge, PR, or cleanup workflows.
Execute implementation plans with task-specific subagents and code review checkpoints.
Create isolated Git worktrees safely with smart directory selection.
Debug issues with a four-phase method that finds root causes before fixes.
Helps developers write code comments explaining intent, rationale, and key tradeoffs.