Refactor with tests first, one change at a time, never mix refactoring with bug fixes or new features
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "Refactoring Safely" 技能: 1. 下载 https://raw.githubusercontent.com/obra/clank/main/skills/coding/refactoring-safely/SKILL.md 2. 保存为 ~/.claude/skills/refactoring-safely/SKILL.md 3. 装好后重载技能,告诉我可以用了
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:
…
帮助你撰写不过时的代码注释,聚焦做什么与为什么而非时序背景。
帮助开发者按业务领域为代码命名,提升可读性与协作一致性。
系统性沿调用链逆向排查缺陷,快速定位问题的最初触发点
帮助开发者为代码补充设计意图注释,聚焦原因与关键取舍而非表面功能。
并行分派多个智能体,同时调查并修复彼此独立的问题
帮助开发者规范完成开发分支收尾,支持合并、提 PR 或清理流程。