Files
2nd/10_Wiki/Topic_Programming/Architecture/Switch Statements (Switch 문).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.5 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-switch-statements-switch-문 Switch Statements (Switch 문) 10_Wiki/Topics verified self
Switch Statement
Pattern Matching
Replace Switch
none A 0.9 applied
refactoring
code-smell
polymorphism
2026-05-10 pending
language framework
multi refactoring

Switch Statements (Switch 문)

매 한 줄

"매 type-tagged switch 의 polymorphism 의 missed opportunity". 매 classic code smell 의 — 매 type 의 switch 의 새 type 의 추가 시 매 모든 switch 의 update 의 필요. 2026 의 modern lang 의 sealed types + pattern matching 의 acceptable.

매 핵심

매 왜 smell 인가

  • OCP 위반: 매 새 type 의 추가 의 매 switch 의 modify 의 필요.
  • Shotgun surgery: 매 동일 switch 의 codebase 의 scatter.
  • Type safety hole: 매 default 의 fallthrough — 매 unhandled case 의 silent.

매 acceptable cases

  • Sealed/exhaustive matching (Kotlin, Rust, Swift, Java 21+).
  • Single switch — 매 dispatch 의 한 곳.
  • Performance-critical hot path (jump table).
  • Parsing / state machines.

매 refactoring 전략

  • Replace with polymorphism: type-tagged switch → method override.
  • Replace with strategy: behavior-tagged switch → strategy injection.
  • Replace with map: data-tagged switch → lookup table.
  • Sealed pattern match: 매 modern lang 의 exhaustive 의 보장.

매 응용

  1. AST visitor (legitimate switch).
  2. State machine (legitimate).
  3. Domain logic dispatch (refactor to polymorphism).

💻 패턴

Smell — type-tagged switch

// BAD
double area(Shape s) {
    switch (s.type) {
        case CIRCLE: return Math.PI * s.radius * s.radius;
        case SQUARE: return s.side * s.side;
        case TRIANGLE: return 0.5 * s.base * s.height;
        default: throw new IllegalStateException();
    }
}

Refactor — polymorphism

sealed interface Shape permits Circle, Square, Triangle {
    double area();
}
record Circle(double r) implements Shape {
    public double area() { return Math.PI * r * r; }
}
record Square(double side) implements Shape {
    public double area() { return side * side; }
}
record Triangle(double base, double h) implements Shape {
    public double area() { return 0.5 * base * h; }
}
// caller: shape.area()

Java 21 — pattern matching switch (exhaustive)

double area(Shape s) {
    return switch (s) {
        case Circle c   -> Math.PI * c.r() * c.r();
        case Square sq  -> sq.side() * sq.side();
        case Triangle t -> 0.5 * t.base() * t.h();
        // no default — compiler proves exhaustive
    };
}

Kotlin — sealed when

sealed class Event
data class Click(val x: Int, val y: Int) : Event()
data class Key(val code: Int) : Event()
object Close : Event()

fun handle(e: Event) = when (e) {
    is Click -> "click at ${e.x},${e.y}"
    is Key   -> "key ${e.code}"
    Close    -> "bye"
}  // exhaustive — no else needed

Rust — match

enum Msg { Quit, Move { x: i32, y: i32 }, Write(String) }

fn process(m: Msg) {
    match m {
        Msg::Quit => println!("quit"),
        Msg::Move { x, y } => println!("move to {x},{y}"),
        Msg::Write(s) => println!("write {s}"),
    }
}

Replace with map (data dispatch)

// BAD
function fmt(t: string, v: number): string {
  switch (t) {
    case "usd": return `$${v}`;
    case "eur": return `€${v}`;
    case "krw": return `₩${v}`;
  }
}

// GOOD
const PREFIX: Record<string, string> = { usd: "$", eur: "€", krw: "₩" };
const fmt = (t: string, v: number) => `${PREFIX[t] ?? ""}${v}`;

Replace with strategy

class Discount(Protocol):
    def apply(self, price: float) -> float: ...

class Pct(Discount):
    def __init__(self, p): self.p = p
    def apply(self, price): return price * (1 - self.p)

class Fixed(Discount):
    def __init__(self, amt): self.amt = amt
    def apply(self, price): return max(0, price - self.amt)

# caller injects Discount, no switch

매 결정 기준

상황 Approach
Type dispatch (open set) Polymorphism
Type dispatch (closed set) Sealed + pattern match
Data lookup Map / dict
Complex behavior swap Strategy pattern
AST / state machine Switch (legitimate)

기본값: 매 sealed types + exhaustive pattern matching — 매 modern lang 의 ergonomic + safe.

🔗 Graph

🤖 LLM 활용

언제: 매 switch 의 detect 의 refactor suggestion, type 의 sealed 의 propose. 언제 X: legitimate switch (AST, state machine) 의 전체 refactor 의 — 매 false positive.

안티패턴

  • Refactor every switch: 매 legitimate cases 의 보존 — AST, parser.
  • Visitor for trivial dispatch: 매 over-engineering — 매 polymorphism 의 충분.
  • Strategy without need: 매 single-impl 의 strategy 의 yagni.

🧪 검증 / 중복

  • Verified (Fowler, "Refactoring" 2nd ed.; Effective Java 3rd ed.; JEP 441).
  • 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — refactor patterns, modern pattern matching