"매 untestable 한 method 의 옆에 매 새 method 의 sprout — 매 새 logic 의 isolation, 매 test 의 가능". Michael Feathers 의 Working Effectively with Legacy Code (2004) 의 핵심 technique. 매 legacy code 의 modification 시 매 직접 수정 X — 매 옆에 새 method 의 grow.
매 핵심
매 motivation
Legacy method: 매 too long, too coupled, 매 test 의 X.
새 feature 의 add 필요: 매 직접 수정 → 매 risk 의 폭발.
Sprout: 매 new logic 의 separate method, 매 test 의 first, 매 caller 의 단순 call.
매 절차
Identify 매 change point.
Write 매 새 method (with tests) — empty 매 body, 매 signature first.
Test-drive 매 새 method 의 구현.
Call 매 새 method 의 from legacy code (one line).
매 legacy code 의 거의 untouched.
매 응용
Bug fix 의 add — 매 새 validation 의 sprout.
Feature flag 의 add — 매 새 conditional 의 sprout.
Logging 의 add — 매 새 logger call 의 sprout.
💻 패턴
Before sprout
publicclassOrderProcessor{publicvoidprocess(Orderorder){// 100+ lines of untested legacy logicrepository.save(order);emailService.send(order.getCustomer(),"thanks");}}
Sprout method 의 적용
publicclassOrderProcessor{publicvoidprocess(Orderorder){// 100+ lines untouchedrepository.save(order);sendOrderConfirmation(order);// sprouted}// New method — fully testedvoidsendOrderConfirmation(Orderorder){vartemplate=order.isPriority()?"priority-thanks":"thanks";emailService.send(order.getCustomer(),template);}}
언제: 매 legacy codebase 의 small additive change, 매 surrounding code 의 untested.
언제 X: 매 greenfield code (그냥 inline), 매 logic 의 deeply intertwined (Wrap 의 사용).
❌ 안티패턴
Sprout 의 then leave: 매 sprouted method 의 grow 의 indefinite — 매 결국 same problem.
No tests for sprout: 매 point 의 lost — 매 sprout 의 핵심 value 의 testability.
Sprout into same class: 매 class 의 already God class → sprout class 의 사용.