Blog: usual
DRY: Don't Repeat Yourself

DRY: Don't Repeat Yourself

링크 복사
링크가 복사되었습니다

같은 코드를 두 번 쓰지 마라.

모든 지식과 로직은 시스템 내에서 단 하나의 명확한 표현을 가져야 한다.

❌ 중복

function calculateTotalPrice(items: Item[]) {
  return items.reduce((sum, item) => sum + item.price * item.quantity, 0);
}

function calculateDiscountedPrice(items: Item[]) {
  const total = items.reduce((sum, item) => sum + item.price * item.quantity, 0);
  return total * 0.9;
}

✅ 추상화

function calculateTotalPrice(items: Item[]) {
  return items.reduce((sum, item) => sum + item.price * item.quantity, 0);
}

function calculateDiscountedPrice(items: Item[]) {
  return calculateTotalPrice(items) * 0.9;
}

다만,

개념적으로 같은 로직이라면, 코드 내에서도 같은 로직을 지나야 한다