Files
2nd/10_Wiki/Topics/Architecture/Red-Green Refactoring.md
T
Antigravity Agent f8b21af4be Wiki cleanup: error-doc removal, dedup merge, link normalization
10_Wiki/Topics 대규모 정리:
- 오류 캡처/미완성 stub 문서 227개 제거
- 교차폴더 중복 43클러스터 병합 (63파일 → redirect)
- 링크명 정규화: 깨진 링크 수정·redirect 직결·개념 매핑 ~2,400건
- 카테고리 MOC 6개 신규 생성
- Graph 섹션 미해결 related-keyword 링크 10,058건 제거

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 23:52:15 +09:00

158 lines
5.2 KiB
Markdown

---
id: wiki-2026-0508-red-green-refactoring
title: Red-Green Refactoring
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [Red-Green-Refactor, TDD Cycle, RGR]
duplicate_of: none
source_trust_level: A
confidence_score: 0.92
verification_status: applied
tags: [tdd, refactoring, testing, xp]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
language: TypeScript
framework: Vitest
---
# Red-Green Refactoring
## 매 한 줄
> **"매 Red → Green → Refactor — 매 minute scale 의 design loop"**. Kent Beck 의 *Test-Driven Development: By Example* (2002) 에서 codified 된 micro-cycle. 매 failing test (Red) → minimum code 로 pass (Green) → smell 제거 (Refactor) 의 30초~3분 사이클이 매 production-grade XP/TDD 의 atomic unit 이다.
## 매 핵심
### 매 세 phase
- **Red**: 매 새 behavior 를 실패하는 test 로 명시. Compile error 도 Red 로 인정 (Beck).
- **Green**: 매 *fastest* path 로 pass — fake it, obvious implementation, triangulation 중 택 1. 매 ugly OK.
- **Refactor**: 매 두 hat 의 second hat — duplicate 제거, naming, 추상화. 매 test 는 계속 green.
### 매 cycle 시간
- 매 한 cycle = 30초 ~ 3분 권장. 30분 넘으면 step too big.
- 매 commit 단위 = green 직후 또는 refactor 직후. Red 상태 commit 의 X.
### 매 응용
1. Outside-in (London school): mock 기반 acceptance test → unit drill-down.
2. Inside-out (Detroit/Chicago school): domain core 부터 state-based test.
3. AI pair: 매 Claude Code 가 test 작성, 매 human 이 implementation 확인 — 또는 reverse.
## 💻 패턴
### Vitest 의 매 cycle 예시
```typescript
// === RED ===
import { describe, it, expect } from 'vitest';
import { Money } from './money';
describe('Money', () => {
it('multiplies amount by scalar', () => {
expect(new Money(5, 'USD').times(2)).toEqual(new Money(10, 'USD'));
});
});
// → fails: Money is not defined
```
### Green: 매 fastest path
```typescript
// money.ts — Beck 의 "fake it" 단계
export class Money {
constructor(public amount: number, public currency: string) {}
times(n: number): Money {
return new Money(this.amount * n, this.currency);
}
// equals 는 Vitest 의 toEqual 가 structural 로 처리
}
```
### Refactor: 매 immutable + value semantics
```typescript
export class Money {
private constructor(
public readonly amount: number,
public readonly currency: string,
) {}
static of(amount: number, currency: string): Money {
if (!Number.isFinite(amount)) throw new RangeError('amount NaN');
return new Money(amount, currency);
}
times(n: number): Money {
return Money.of(this.amount * n, this.currency);
}
}
```
### Triangulation pattern
```typescript
// 매 두 번째 test 가 generalization 을 강제
it('multiplies by 3', () => {
expect(Money.of(5, 'USD').times(3)).toEqual(Money.of(15, 'USD'));
});
// → 매 hardcoded `return new Money(10, ...)` 는 더 이상 통하지 않음
```
### Tidy First (Beck 2023)
```typescript
// 매 refactor 와 behavior change 를 별도 commit 으로 분리
// 1. tidy first: rename, extract — pure structural
// 2. behavior: 새 test + impl
// 매 PR review 의 noise 감소
```
### TCR (test && commit || revert) — Beck 2018
```bash
# 매 hardcore variant: green 이면 commit, red 이면 코드 폐기
vitest run && git commit -am wip || git checkout -- .
```
### AI-assisted RGR (Claude Code 2026)
```typescript
// 1. human: write Red test
// 2. claude: minimal Green implementation
// 3. human: review + suggest refactor
// 4. claude: apply Extract Function, etc.
// 매 test 의 contract 는 human 이 own
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| 새 feature, 도메인 명확 | Inside-out RGR |
| 외부 contract 우선 | Outside-in (mock-driven) |
| Legacy, 매 test 없음 | 매 Characterization test 후 RGR |
| Spike / prototype | RGR skip — 매 throw away 후 RGR 로 재작성 |
| Refactor only | Red 없이 Refactor hat — 매 test 가 이미 green |
**기본값**: Inside-out RGR + Tidy First commit 분리.
## 🔗 Graph
- 부모: [[TDD]] · [[Refactoring_Best_Practices]]
- 변형: [[BDD]]
- 응용: [[Pair Programming]] · [[Mob Programming]]
- Adjacent: [[Rule of Three (3의 법칙)]] · [[Code Smell]] · [[XP]]
## 🤖 LLM 활용
**언제**: 매 well-bounded pure function, 매 algorithm, 매 domain logic. Claude Code 의 Green phase delegation 효율 높음.
**언제 X**: 매 UI snapshot, 매 distributed system race, 매 exploration spike — RGR overhead 가 가치 초과.
## ❌ 안티패턴
- **Test after**: 매 implementation 후 test 추가 — 매 design feedback loss, 매 false-confidence test.
- **Commit at Red**: 매 broken main, 매 bisect 불가.
- **Skipping Refactor**: 매 green-and-go — 매 debt 누적.
- **Big Red**: 매 30분 짜리 test → step too big, 매 walking skeleton 으로 분해.
- **Refactor with failing test**: 매 두 hat 의 violation — 매 behavior change 와 cleanup 혼합.
## 🧪 검증 / 중복
- Verified: Beck *TDD: By Example* (2002), *Tidy First* (2023). Martin Fowler *Refactoring 2e* (2018) Ch.1.
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — full RGR cycle, patterns, Tidy First/TCR variants |