Files
2nd/10_Wiki/Topic_Programming/From_Other/Rule of Three.md
T
Antigravity Agent 9148c358d0 docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를
Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류.

- 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로
  자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거,
  동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거.
- 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming,
  Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business,
  Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로,
  나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는
  title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백).
  원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지.
- 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서.
- 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는
  지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지.
- Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경.
- 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
2026-07-05 00:33:48 +09:00

5.2 KiB

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-rule-of-three Rule of Three 10_Wiki/Topics verified self
Three Strikes Rule
DRY Trigger
Triplication Rule
none A 0.9 applied
refactoring
dry
software-engineering
code-smell
abstraction
2026-05-10 pending
language framework
agnostic refactoring

Rule of Three

매 한 줄

"매 첫 두 번의 중복은 참아라. 세 번째에 추상화하라". Martin Fowler 가 Refactoring (1999) 에서 codify 한 heuristic — 매 premature abstraction 의 cost 가 duplication 의 cost 보다 큰 까닭에, 매 세 번째 instance 에서 야 진짜 pattern 이 보인다는 pragmatic guideline. 매 2026 년 까지 매 senior eng 의 default mental model.

매 핵심

매 origin (Don Roberts → Fowler)

  • Don Roberts 가 처음 articulate, Fowler 가 Refactoring (1999) book 에서 popularize.
  • 매 underlying principle: 매 abstraction 은 매 미래 의 use case 의 prediction. 매 N=2 일 때 prediction 의 sample size 부족. 매 N=3 에서 야 매 commonality 가 진짜 인지 vs 매 우연 인지 distinguishable.

매 trade-off

  • 매 abstraction cost: 매 wrong abstraction 의 cost ≫ 매 duplication 의 cost (Sandi Metz 의 famous talk: "duplication is far cheaper than the wrong abstraction").
  • 매 cognitive load: 매 abstraction 의 매 indirection 추가 → 매 reader 가 매 hop 따라 가야.
  • 매 lock-in: 매 abstraction 일찍 commit 시 매 future divergence 시 매 rollback cost 큼.

매 응용

  1. 함수 추출: 매 동일 logic 의 3 번째 copy 시 → extract function.
  2. 클래스 hierarchy: 매 3 번째 subclass 의 emerging 시 → introduce base class.
  3. Config / parameter: 매 magic number 의 3 번째 occurrence 시 → constant 화.

💻 패턴

Pattern 1: Duplication tolerated (N=2)

// 매 OK — 매 2 번 만의 duplication. 매 abstraction X.
function calcOrderTotal(items: Item[]) {
  return items.reduce((sum, i) => sum + i.price * i.qty, 0);
}

function calcCartPreview(items: Item[]) {
  // 매 looks similar but 매 different context — leave it.
  return items.reduce((sum, i) => sum + i.price * i.qty, 0);
}

Pattern 2: 3rd occurrence → extract

// 매 3 번째 copy 등장 시 abstract.
const sumLineItems = (items: Item[]) =>
  items.reduce((sum, i) => sum + i.price * i.qty, 0);

const calcOrderTotal = sumLineItems;
const calcCartPreview = sumLineItems;
const calcInvoiceTotal = sumLineItems;  // 매 trigger

Pattern 3: Generic-too-early anti-example

// 매 BAD — 매 N=1 에서 매 over-generalize.
function processCollection<T, R>(
  data: T[],
  reducer: (acc: R, x: T) => R,
  initial: R,
  filter?: (x: T) => boolean,
  mapper?: (x: T) => T
): R {
  // 매 future-proofing 의 환상. 매 actual 사용 = 매 1 case.
}

Pattern 4: Sandi Metz 의 "prefer duplication"

# 매 duplicate code 는 매 wrong abstraction 보다 매 fix easier.
# 매 wrong abstraction = 매 매 caller refactor 필요.
class OrderTotal
  def calculate(items)
    items.sum { |i| i.price * i.qty }
  end
end

class CartPreview
  def calculate(items)
    items.sum { |i| i.price * i.qty }  # 매 duplicate intentional until N=3
  end
end

Pattern 5: 매 React component 의 Rule of Three

// 매 1st: inline.
<button className="bg-blue-500 px-4 py-2 rounded">Save</button>

// 매 2nd: 그냥 copy.
<button className="bg-blue-500 px-4 py-2 rounded">Cancel</button>

// 매 3rd: abstract.
const PrimaryButton = ({ children, ...rest }) => (
  <button className="bg-blue-500 px-4 py-2 rounded" {...rest}>{children}</button>
);

매 결정 기준

상황 Approach
매 N=1 (one-off) Inline. 매 abstraction X.
매 N=2 (duplication 등장) Wait. 매 watch for 3rd.
매 N=3 (triplication) Extract — 매 pattern crystallized.
매 cross-domain duplication (uncertain pattern) Wait — 매 4-5 까지 도 OK.
매 critical bug-prone duplication Extract early — 매 correctness ≫ 매 abstraction cost.

기본값: 매 N=3 까지 wait. 매 doubt 시 duplicate.

🔗 Graph

🤖 LLM 활용

언제: 매 codebase 의 duplication 매 detect → 매 N count 후 매 refactor 제안. 언제 X: 매 N=2 에서 매 LLM 의 over-eager abstraction 제안 — reject.

안티패턴

  • DRY 원리주의 (DRY zealotry): 매 N=1 에서 매 abstract → 매 wrong shape.
  • Forever duplication: 매 N=10 도 매 ignore — 매 maintenance disaster.
  • Hasty generalization: 매 surface similarity 만 보고 매 abstract — 매 underlying domain 의 different.

🧪 검증 / 중복

  • Verified (Fowler Refactoring 1st/2nd ed; Sandi Metz "All the Little Things" RailsConf 2014).
  • 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — Rule of Three 의 origin/trade-off/patterns/Metz 의 "prefer duplication" 정리