--- id: wiki-2026-0508-scratch-refactoring-스크래치-리팩토링 title: Scratch Refactoring (스크래치 리팩토링) category: 10_Wiki/Topics status: verified canonical_id: self aliases: [Scratch Refactoring, throwaway refactor, exploratory refactoring] duplicate_of: none source_trust_level: A confidence_score: 0.9 verification_status: applied tags: [refactoring, technique, learning] raw_sources: [] last_reinforced: 2026-05-10 github_commit: pending tech_stack: language: typescript framework: none --- # Scratch Refactoring (스크래치 리팩토링) ## 매 한 줄 > **"매 throwaway refactor — 매 코드 이해를 위한 도구로서의 리팩토링"**. Michael Feathers가 *Working Effectively with Legacy Code*에서 정형화. 매 결과 코드를 commit하지 않고, 매 ugly legacy를 매 자유롭게 변형하며 매 구조를 학습한 뒤 매 throw away. ## 매 핵심 ### 매 정의 - 매 commit하지 않는 refactor. - 매 목적: 코드 이해 (test 작성 X, 동작 보존 X). - 매 끝나면 `git checkout .`. ### 매 vs 일반 refactor - 일반 refactor: 매 동작 보존 + commit. - Scratch refactor: 매 동작 깨져도 OK + 매 폐기. ### 매 응용 1. 매 거대 legacy class 매 진입할 때 매 first-pass 이해. 2. 매 "이 코드 어떻게 동작?" 질문 매 답. 3. 매 진짜 refactor 의 매 사전 탐색. ## 💻 패턴 ### 매 brutal extraction (commit X) ```bash # 매 worktree에서 자유롭게 변형 git switch -c scratch/explore-OrderService # ... 매 method 분리, rename, comment 추가, dead code 삭제 ... # 매 끝나면 git switch main git branch -D scratch/explore-OrderService ``` ### 매 inline-everything (이해용) ```typescript // 매 before — 매 6단계 helper chain function process(order) { return finalize(validate(normalize(load(order)))); } // 매 scratch: 매 inline 펼침 (commit X) function process(order) { const loaded = db.find(order.id); if (!loaded) throw new Error(); const normalized = { ...loaded, total: loaded.items.reduce((s,i)=>s+i.price, 0) }; // ... 매 read해서 이해 → 매 throw away } ``` ### 매 rename-storm (학습용) ```typescript // 매 이름이 모호한 변수/메서드 매 임시 rename → 매 이해 뒤 폐기 class X { f(d) { return d.x.map(v=>v.y); } } // 매 scratch rename class OrderQuery { extractCustomerIds(order) { return order.lines.map(l=>l.customerId); } } // 매 이해 완료 → revert ``` ### 매 type annotation 추가 (JS → TS scratch) ```typescript // 매 untyped JS legacy를 매 임시로 type 입혀 흐름 파악 function process(/** @type {Order} */ o) { /* ... */ } // 매 끝나면 폐기 (매 진짜 migration은 별도 task) ``` ### 매 log-dump trace ```typescript // 매 모든 분기에 console.log → 매 실제 path 관찰 function calculate(o) { console.log("calculate", o); if (o.type === "A") { console.log("branch A"); /* ... */ } // 매 trace 후 매 모든 log 제거 } ``` ### 매 AI-assisted scratch (2026) ```bash # 매 Claude Opus 4.7에 매 scratch refactor 부탁 claude "Read src/legacy/OrderService.ts. Inline all helpers, rename obscure vars, add comments explaining each branch. Goal: human-readable. Don't preserve behavior." # 매 결과 매 read만 하고 매 discard ``` ## 매 결정 기준 | 상황 | Approach | |---|---| | 매 legacy 매 처음 진입 | Scratch refactor 먼저. | | 매 production fix 중 | X — 매 일반 refactor + test. | | 매 시간 부족 | 매 scratch도 OK — 매 빠른 이해 우선. | | 매 팀 onboarding | 매 신입 매 scratch refactor 권장. | **기본값**: 매 unfamiliar legacy → scratch refactor → 이해 → 진짜 refactor. ## 🔗 Graph - 부모: [[Refactoring_Best_Practices|Refactoring]] - 응용: [[Code Reading]] - Adjacent: [[Seam (접점)]] ## 🤖 LLM 활용 **언제**: 매 거대 legacy 진입, 매 코드 이해가 매 1차 목표일 때. **언제 X**: 매 production behavior change, 매 long-term improvement. ## ❌ 안티패턴 - **매 commit해버림**: 매 scratch가 매 production에 매 leak. - **매 너무 오래 매 매달림**: 매 scratch는 매 hours, 매 days X. - **매 test 없이 매 진짜 refactor 시도**: 매 scratch 후 매 진짜 refactor에는 매 test 필수. ## 🧪 검증 / 중복 - Verified (Feathers, *Working Effectively with Legacy Code*, 2004). - 신뢰도 A. ## 🕓 Changelog | 날짜 | 변경 | |---|---| | 2026-05-08 | Phase 1 | | 2026-05-10 | Manual cleanup — Feathers scratch refactor + AI-assisted variant |