I'll Be Backend
Principles · TypeScript

DRY: one rule, one place

Change a business rule once and have every feature pick it up.

12 steps4 chapters~3 min at 1.25×#duplication#constants#single source of truth
The lesson plays right here. Pause anywhere, step back, or jump to a step below. Watch the TypeScript video on YouTube Code on GitHub

Watch the narrated video

Every step

Problem

Steps 1–3

Refactor

Steps 4–9

Payoff

Step 10

Recap

Steps 11–12

The code: before and after

Before

function shippingFee(cart: Cart): number {
  if (cart.subtotal >= 50) {
    return 0;
  }
  return 4.99;
}

function bannerText(cart: Cart): string {
  if (cart.subtotal < 50) {
    const missing = 50 - cart.subtotal;
    return `Add $${missing.toFixed(2)} to ship free`;
  }
  return "Your order ships free";
}

function receiptLine(cart: Cart): string {
  const free = cart.subtotal >= 50;
  return free ? "Shipping: free" : "Shipping: $4.99";
}

After

const FREE_SHIPPING_FROM = 50;
const MEMBER_FREE_SHIPPING_FROM = 25;
const SHIPPING_FEE = 4.99;

function freeShippingFrom(cart: Cart): number {
  return cart.member ? MEMBER_FREE_SHIPPING_FROM : FREE_SHIPPING_FROM;
}

function shipsFree(cart: Cart): boolean {
  return cart.subtotal >= freeShippingFrom(cart);
}

function amountToFreeShipping(cart: Cart): number {
  return freeShippingFrom(cart) - cart.subtotal;
}

function shippingFee(cart: Cart): number {
  if (shipsFree(cart)) {
    return 0;
  }
  return SHIPPING_FEE;
}

function bannerText(cart: Cart): string {
  if (!shipsFree(cart)) {
    const missing = amountToFreeShipping(cart);
    return `Add $${missing.toFixed(2)} to ship free`;
  }
  return "Your order ships free";
}

function receiptLine(cart: Cart): string {
  const free = shipsFree(cart);
  return free ? "Shipping: free" : `Shipping: $${SHIPPING_FEE}`;
}

Transcript, step by step

Problem

Step 1: Three features, one rule Before

Orders over fifty dollars ship free in this shop. Shipping fee uses that rule to charge the fee, and banner text uses it to tell you how much more to add. Receipt line checks it a third time to print the shipping line on the receipt. All three work, and the tests pass.

Step 2: Members ship free from 25 Code smell

Marketing now wants members to ship free from twenty-five dollars. So you find the rule in the fee and in the banner, and you change it in both. This is duplicated knowledge, where one business rule is written out in several places. The free shipping limit lives here, here, and here, in the receipt nobody remembered to change. So a member who paid nothing for shipping gets a receipt that says four ninety-nine. And the next change to the limit starts with a search for every copy, because nothing tells you where they are.

Step 3: Undo, then give it a home Plan

Let's back that change out. DRY, short for don't repeat yourself, says every rule should have exactly one home in the code. So we'll build that home first, and then the member rule should take one edit.

Refactor

Step 4: Name the limit Replace Magic Number with Constant

First, the number fifty gets a name, free shipping from, at the top of the module. Every copy of fifty now reads that constant, so the limit itself lives in one place. The comparison, though, is still written out three times.

Step 5: Name the fee Replace Magic Number with Constant

The fee of four ninety-nine was typed twice too, so it becomes a constant named shipping fee. Now the fee and the receipt can't quote two different prices.

Step 6: Name the check Extract Function

Every feature keeps asking whether this cart ships free, so that question gets a function of its own. Ships free holds the comparison, and the fee calls it.

Step 7: The banner asks too Replace Inline Code with Function Call

The banner had the same check turned around, so now it calls ships free and negates the answer. A check that's flipped by hand is an easy place for an off-by-one bug to hide, and now there's nothing to flip.

Step 8: The receipt asks too Replace Inline Code with Function Call

The receipt gets the same call. Now the comparison is written once, in ships free, and all three features ask it.

Step 9: Name the gap Extract Function

The banner still did its own math with the limit, so that subtraction moves up as amount to free shipping. Now only the two functions at the top read the limit, and the features below them only ask.

Payoff

Step 10: Members, in one helper Payoff

Now we add the member limit again. It goes into one helper, free shipping from, and both rule functions use it. The fee, the banner and the receipt ask the same questions they asked before, here, here, and here. None of them changed, and the receipt can't fall behind again, because it gets its answer from the same place the fee does.

Recap

Step 11: Where we started, where we ended Before / after

On the left, each function carried its own copy of the fifty dollar limit. Changing the rule meant finding every copy, and missing one was easy. On the right, the limit and the fee have names, and the check lives in ships free. The fee asks ships free, and so do the banner and the receipt.

Step 12: When to reach for it Takeaways

Give a rule one home as soon as a second feature needs the same decision. Leave code alone when it only looks the same, like two fees that both happen to cost four ninety-nine, because they'll change for different reasons. You'll spot the smell when the same number or condition turns up in several functions, and a small change starts with a search.