Files
2nd/10_Wiki/Topic_Programming/Programming & Language/스파게티 코드 (Spaghetti Code).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

4.0 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-스파게티-코드-spaghetti-code 스파게티 코드 (Spaghetti Code) 10_Wiki/Topics verified self
spaghetti code
tangled code
none A 0.95 applied
anti-pattern
code-quality
refactoring
2026-05-10 pending
language framework
any any

스파게티 코드 (Spaghetti Code)

매 한 줄

"매 control flow 가 noodle 처럼 entangled 되어 trace 불가". 매 unstructured GOTO · deeply nested conditions · global state mutation · circular dependency 가 만든 maintenance nightmare. 매 SOLID 위반 의 most visible symptom.

매 핵심

매 증상

  • Tangled control flow: 함수 한 개가 500+ lines, nested if 5+.
  • Global state: any 함수 가 any 변수 mutate.
  • Circular dep: A→B→C→A.
  • Magic numbers/strings: meaning unclear.
  • Copy-paste: same logic in 7 places.

매 원인

  • Time pressure → "just make it work".
  • No code review → drift 누적.
  • Premature optimization → unnecessary complexity.
  • Lack of architecture → ad-hoc additions.

매 응용 (refactor 전략)

  1. Extract method · extract class.
  2. Replace conditional with polymorphism.
  3. Introduce parameter object.
  4. Strangler fig migration.

💻 패턴

Before (spaghetti)

function processOrder(o: any) {
  if (o.type == "A") {
    if (o.user.isVip) {
      o.discount = 0.2;
      if (o.country == "KR") o.tax = 0.1;
      else if (o.country == "US") o.tax = 0.07;
      // ... 200 more lines
    }
  }
}

After (refactored)

type Order = { type: OrderType; user: User; country: Country; items: Item[] };

const discountStrategy: Record<OrderType, (u: User) => number> = {
  A: u => u.isVip ? 0.2 : 0.05,
  B: () => 0.1,
};

const taxRate: Record<Country, number> = { KR: 0.1, US: 0.07, JP: 0.08 };

function processOrder(o: Order) {
  const discount = discountStrategy[o.type](o.user);
  const tax = taxRate[o.country];
  return finalizePricing(o, discount, tax);
}

Replace conditional with polymorphism

abstract class Payment {
  abstract process(amount: number): Promise<void>;
}
class CardPayment extends Payment { async process(a: number) { /* card */ } }
class CryptoPayment extends Payment { async process(a: number) { /* btc */ } }

Extract method

// before: 1 huge function
// after: 5 small functions, each <20 lines, single responsibility
function checkout(cart: Cart) {
  validateCart(cart);
  const total = calculateTotal(cart);
  const tax = applyTax(total, cart.country);
  return submitPayment(cart.user, total + tax);
}

Detect with cyclomatic complexity

npx eslint . --rule 'complexity: ["error", 10]'
npx code-complexity ./src --max 10

매 결정 기준

상황 Approach
Function > 50 lines extract method
Nested if > 3 early return / strategy map
Same logic in 3+ places DRY (extract function)
Module circular dep dependency inversion
Cyclomatic > 10 refactor split

기본값: function 30 lines 미만 · cyclomatic 10 미만 · single responsibility.

🔗 Graph

  • 부모: SOLID 원칙 · 관심사의 분리 (Separation of Concerns)
  • Adjacent: 단일 책임 원칙 (SRP) · 기본 타입에의 집착 (Primitive Obsession)

🤖 LLM 활용

언제: legacy code refactor · cyclomatic 감소 제안 · extract method 자동화. 언제 X: domain semantics 가 unclear → human review 필수.

안티패턴

  • Big rewrite: incremental refactor 가 safe.
  • Refactor without tests: regression 보장 없음.
  • Premature abstraction: 2번째 use case 까지 기다림 (rule of three).

🧪 검증 / 중복

  • Verified (Fowler "Refactoring" · Martin "Clean Code").
  • 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — spaghetti code refactor 패턴