Clean code: extract until it reads
Turn a long method into named steps you can read from top to bottom.
Watch the narrated video
Every step
Problem
Steps 1–3Refactor
Steps 4–7Payoff
Step 8Recap
Steps 9–10The code: before and after
Before
final class Checkout
{
public function confirm(Order $order): string
{
if ($order->items === []) {
throw new InvalidArgumentException('No items');
}
$total = 0.0;
foreach ($order->items as $item) {
$total += $item->price * $item->qty;
}
$total += $total < 50.0 ? 4.99 : 0.0;
$total += $order->express ? 7.50 : 0.0;
return sprintf('%d items, $%.2f',
count($order->items), $total);
}
}After
final class Checkout
{
public function confirm(Order $order): string
{
$this->requireItems($order);
$goods = $this->subtotal($order);
$discount = $this->memberDiscount($order, $goods);
$total = $goods - $discount + $this->deliveryFee($order, $goods);
return sprintf('%d items, $%.2f',
count($order->items), $total);
}
private function requireItems(Order $order): void
{
if ($order->items === []) {
throw new InvalidArgumentException('No items');
}
}
private function subtotal(Order $order): float
{
$sum = 0.0;
foreach ($order->items as $item) {
$sum += $item->price * $item->qty;
}
return $sum;
}
private function memberDiscount(Order $order, float $goods): float
{
return $order->member ? $goods * 0.10 : 0.0;
}
private function deliveryFee(Order $order, float $goods): float
{
$fee = 0.0;
$fee += $goods < 50.0 ? 4.99 : 0.0;
$fee += $order->express ? 7.50 : 0.0;
return $fee;
}
}Transcript, step by step
Problem
Step 1: A checkout that works Before
This method confirms a grocery order and returns the line the customer sees. It checks there are items, adds up the prices, adds delivery, and builds the message. Delivery is free from fifty dollars, express adds seven fifty, and the tests pass.
Step 2: A discount in the wrong place Code smell
Members now get ten percent off their groceries. Someone adds the discount near the end, because total looks like the number to change. By that line, total already includes delivery, so members get ten percent off the delivery fees too, and nobody asked for that. This is a long method, one body doing several jobs, so you have to read every line to find the one you need. It checks the order here, adds up the goods here, adds delivery here, and builds the message here. Total means the goods at the top and the goods plus fees at the bottom, and only reading every line tells you which one you've got.
Step 3: Undo, then extract Plan
Let's back that change out. Extract Method takes a few lines that do one job and moves them into a method named after that job. We'll keep extracting until confirm reads like a list of steps, and then the discount should have an obvious place.
Refactor
Step 4: Pull out the guard Extract Method
The first lines guard against an empty order, so they move into a method called require items. Confirm now opens with one line that says what it checks, and you only read the error handling when you care about it.
Step 5: Pull out the subtotal Extract Method
The loop adds up what the groceries cost, so it becomes a method called subtotal. Inside it, the running number gets its own name, sum, so it can't be mixed up with the total in confirm. Back in confirm, total now starts from the subtotal in a single line.
Step 6: One name per meaning Split Variable
Total was doing two jobs, holding the goods first and the amount to pay after that. So the goods get a variable of their own, and total starts from it. The free delivery check now reads the goods, which is what it meant all along.
Step 7: Pull out delivery Extract Method
The two delivery lines move into a method called delivery fee, and it takes the goods to check the free delivery limit. Total is now the goods plus the delivery fee, and you can read that straight off the line. Read confirm from the top and you get the steps in order, because it requires items, adds up the goods, adds the fee, and returns the message. The message line already says what it does, so it stays where it is, and that's the point where you stop extracting.
Payoff
Step 8: The discount, in the right place Payoff
Now the discount comes back, as a method called member discount that takes ten percent off the goods. In confirm, it comes off the goods and the delivery fee is added after, so members only save on what they buy. The jobs that were tangled together now sit apart, with the check here, the goods here, delivery here, and the message here. Require items, subtotal and delivery fee didn't change, so the fees can't pick up the discount by accident.
Recap
Step 9: Where we started, where we ended Before / after
On the left, one method checked the order, added up the prices, charged delivery and built the message. Total changed meaning halfway down, and that's why the discount landed on the fees. On the right, confirm reads as the steps of a checkout. Each step it calls is a small method below, named for its job.
Step 10: When to reach for it Takeaways
Extract a method when you have to read all of one to find the part you need to change. Leave lines alone when they already say what they do, because a method named after them would only repeat them. You'll spot the need in long methods, in comments that label each section, and in one variable that changes meaning on the way down.