Skip to content

Lesson 8 of 17 · The Forces That Shape Code

Premature Abstraction

Intermediate•
lesson•
September 3, 2026•
~9 min read

The duplication you removed too early

Imagine this scenario: You had two functions that looked alike. You pulled out the shared part, called it something sensible, and felt good about it. The pull request was small. The reviewer approved it. You shipped.

Once enough callers depend on it, the function you extracted has a comment near the top that says "do not change this without checking with billing". Nobody knows who billing is anymore.

The duplication you removed became the abstraction nobody can change. The same instinct that made the code "DRY" is what made the code unmodifiable.

What premature abstraction actually is

Premature abstraction is extracting a shared shape from things that look similar before you've seen enough cases to know what's actually shared.

The trap has three steps:

  1. You see two pieces of code that look alike.

  2. You pull out the shared shape into a function, class, or interface.

  3. The next case doesn't quite fit. You add a parameter, a flag, or a branch to make it fit.

You repeat step three a few more times. Each addition is small. Each one feels reasonable. After enough of them, the "shared" code is something nobody understands and nobody dares change. It's also nothing like what the original two cases needed. It's a worst-of-all-worlds compromise that serves none of the callers well.

Why the instinct is so strong

Three things push us toward early abstraction harder than they should:

  • DRY is taught as a virtue, without the caveat. "Don't Repeat Yourself" is one of the first rules a developer hears. The original Pragmatic Programmer framing is about not duplicating knowledge, not about not duplicating characters. The character version is what got remembered. The knowledge version is the one that mattered.

  • Duplicated code feels embarrassing. Two near-identical blocks look like a junior mistake. The instinct is to clean them up before someone else sees them. That instinct rewards visual neatness, not correctness.

  • Pulling out a function looks like progress in code review. Reviewers like seeing extracted helpers. They feel the diff is "more professional". The cost of the abstraction is invisible to a reviewer who isn't going to be the person changing it later.

None of these three account for the real cost of getting the abstraction wrong, which is paid quietly, by every person who has to extend or change the extracted thing later.

The line worth memorizing

There's a popular saying:

Duplication is far cheaper than the wrong abstraction.

Duplication is local pain. You can see it in two files, side by side. If you decide to merge them later, you can do it any time, with full knowledge of how each one is actually used.

A wrong abstraction is distributed pain. The damage is spread across every caller. It's invisible until you try to change something and find that you can't, because the change you want to make doesn't fit the assumptions baked into the shared shape. By that point, ten or twenty pieces of code depend on the abstraction, and removing it is a project.

A worked example

Imagine three email-sending features, added one after another.

First, a welcome email when a member signs up.

PHP
<?php declare(strict_types=1); namespace App\Mail; use App\Members\Member; use Illuminate\Contracts\Mail\Mailer; use Illuminate\Mail\Message; final class SendWelcomeEmail { public function __construct( private Mailer $mailer, ) {} public function __invoke(Member $member): void { $this->mailer->send( 'emails.welcome', ['name' => $member->fullName], fn (Message $message) => $message->to($member->email), ); } }

Second, a password reset email.

PHP
<?php declare(strict_types=1); namespace App\Mail; use App\Members\Member; use Illuminate\Contracts\Mail\Mailer; use Illuminate\Mail\Message; final class SendPasswordResetEmail { public function __construct( private Mailer $mailer, ) {} public function __invoke(Member $member, string $token): void { $this->mailer->send( 'emails.password-reset', ['name' => $member->fullName, 'token' => $token], fn (Message $message) => $message->to($member->email), ); } }

Third, an order confirmation email.

PHP
<?php declare(strict_types=1); namespace App\Mail; use App\Members\Member; use App\Orders\Order; use Illuminate\Contracts\Mail\Mailer; use Illuminate\Mail\Message; final class SendOrderConfirmationEmail { public function __construct( private Mailer $mailer, ) {} public function __invoke(Member $member, Order $order): void { $this->mailer->send( 'emails.order-confirmation', ['name' => $member->fullName, 'order' => $order->toArray()], fn (Message $message) => $message->to($member->email), ); } }

Three classes, almost identical lines, three different feature areas. Someone on the team flags the duplication in review. The "fix" looks obvious:

PHP
<?php declare(strict_types=1); namespace App\Mail; use App\Members\Member; use Illuminate\Contracts\Mail\Mailer; use Illuminate\Mail\Message; final class EmailSender { public function __construct( private Mailer $mailer, ) {} public function send(Member $member, string $view, array $data = []): void { $this->mailer->send( $view, array_merge(['name' => $member->fullName], $data), fn (Message $message) => $message->to($member->email), ); } }

Then the requirements arrive.

  • Welcome email needs a referral code, but only when the member came from a partner link. A flag and an optional argument get added.

  • Password reset needs a token expiry. The data array grows. Then there's a requirement that password resets must never be queued, while welcome emails should always be queued. Another flag.

  • Order confirmation needs an invoice attachment. The signature grows again, this time with optional file attachments.

  • Marketing wants A/B testing, but only on welcome and order confirmation, not on password reset. A branch inside EmailSender decides which template variant to pick.

  • Compliance requires that password reset emails go through a different SMTP relay. Another conditional inside the abstraction.

By the time those requirements have all landed, EmailSender::send has eight parameters, four optional arguments, three conditional branches, and a comment at the top that nobody is sure is still accurate. Every change requires checking three feature areas. Every test for one feature accidentally exercises code paths that belong to another.

The thing that was supposed to remove duplication is now the most expensive file to change in the codebase. The original three classes, kept apart, would have aged better. Each would have grown the parts it actually needed, with no obligation to make those parts work for the others.

Why the wrong abstraction is harder than duplication

  • Duplication doesn't lie about intent. The wrong abstraction does. Two functions that look alike are honest: they're two things that happen to share lines. A bad abstraction claims those two things are "the same", and every future reader has to argue with the claim.

  • Duplication has a clear fix when it becomes painful. A bad abstraction has no clear fix; you have to inline it back to its original duplicates first, which feels like undoing work.

The rule of three

The most reliable defense is also the simplest. Wait for the third occurrence.

The first time you write something, you write it. The second time you write something similar, you let it stay duplicated. The duplication is uncomfortable, and that's fine. By the third occurrence, you have enough information to see which parts are actually shared and which only looked shared from a distance.

Two cases lie to you. Two functions that look the same can be two completely different things that happen to be at the same level of detail today. Three cases tell the truth. By the third one, the parts that genuinely repeat have a recognizable shape, and the parts that don't have started to diverge. The abstraction you build at three is almost always smaller, sharper, and more honest than the one you'd have built at two.

The cost of waiting is small: one extra duplicated block, in plain sight, easy to find when the time comes. The cost of not waiting is the one we just walked through.

None of this is a case for permanent copy-paste, either. Copy-paste has its own costs, and real bugs hide in subtle differences between copies. The argument is for waiting until the duplication has told you what it actually is, before deciding what to do with it.

A better trigger: abstract on the second change, not the second case

The rule of three is the default. This sharper trigger overrides it whenever you have the information to use it.

Don't abstract because you see the pattern again. Abstract because you're changing the same thing in two places and they should change together.

Visual similarity is a weak signal. Reason-to-change is a strong one. Say you're updating a piece of logic in three different files because one business rule changed. Those three files are the same thing in disguise, and the abstraction is overdue. Say three files look identical but change for different reasons. They're not the same thing, and any abstraction you put around them will be wrong the moment one of them needs to change without the others.

The two triggers agree most of the time. When they don't, reason-to-change wins. If you're only on the second occurrence but you already know both places are changing for the same reason, abstract now, don't wait for a third you don't need. If you can't yet tell why something is changing, or whether it'll change again, fall back to the rule of three and wait. Occurrence count is a stand-in for information you don't have yet.

This is the same idea as cohesion, applied to abstractions: things that change together belong together; things that change for different reasons belong apart.

What "looks the same" really means

Two functions can look identical and not be the same. Two functions can look completely different and be the same.

The two welcome / password reset functions earlier looked very similar. They aren't the same thing. They send different content, on different schedules, with different reliability and security expectations. Their resemblance is at the wrong level: the level of "what code we typed", not "what the code is for".

The opposite case is also common. Two pieces of code that read very differently can be expressing the same underlying rule, sometimes a critical business rule, scattered across the codebase. Those duplications are the ones worth chasing. Visual similarity won't tell you they're there; only domain knowledge will.

Recovering from a wrong abstraction

If you're already in a codebase with a few of these, the recovery move is uncomfortable but mechanical:

  • Inline first. Take the abstraction and copy its body back into each caller. The codebase looks worse for a moment. It's also honest for the first time in a while.

  • Resist patching. The temptation, every time, is to "just add one more flag" to keep the abstraction working for the new case. That's how four-parameter abstractions become eight-parameter ones. The honest move is to inline.

  • Re-extract from a stronger position. Once each caller has its own copy, look at the parts that are actually shared, in the way they're actually used today. You'll usually find that the new abstraction is smaller, sharper, or that there are two abstractions where you thought there was one, or sometimes that there's no shared shape at all and the duplication was always healthier.

The one thing to remember

Premature abstraction makes code feel cleaner today and harder to change from then on. Duplication makes code feel uglier today and easier to change indefinitely.

Be willing to live with the second one for longer than feels comfortable. Wait for the third occurrence, unless you already know why it's changing - then abstract on the second, not the third. Until one of those signals shows up, leave the duplication where it is.

Course Content