Copy it before you share it
A shared component feels tidy until every new case needs another flag. Sometimes the honest move is a copy.
Three screens show a card: a profile, a pricing plan and a search result. They look alike, so the tidy instinct is to build one Card and share it.
That works until the requirements stop looking alike.
How the flags pile up
Step through the requirements in the study. Each one is small and reasonable. The profile wants an avatar, pricing wants a highlighted border, search wants the whole card to be a link. In the shared version every request becomes a prop, and most props bring a condition that only one screen cares about. Five requirements in, the type looks like this:
type CardProps = {
title: string;
body: string;
showAvatar?: boolean; // profile
highlighted?: boolean; // pricing
asLink?: boolean; // search
avatarOnMobile?: boolean; // profile hides it, search shows it
linkTarget?: "checkout" | "article"; // pricing or search
};
Sandi Metz described this sequence in The Wrong Abstraction (2016). Someone extracts duplication into a shared piece, later requirements almost fit, and each change adds a parameter and a conditional until the code is hard to follow. Her summary is blunt: "duplication is far cheaper than the wrong abstraction."
The copies in the study drift apart, and that's fine. When pricing changes, one file changes, and nobody has to check whether the search card still renders the same way.
The way back
The most useful part of Metz's argument is the remedy. When an abstraction has gone wrong, inline it back into each caller, delete what each caller doesn't need, and look again. The duplication you bring back shows what the screens really share, which is often less than you assumed. "Inline it back" in the study makes that move.
It can feel like going backwards. I think of it as writing down what you've learned.
When to share
A button used in forty places should be one button. The question is timing.
Martin Fowler's Refactoring credits Don Roberts with a rule of three: the first time, just do it; the second time, wince and duplicate; the third time, refactor. By the third use you have enough examples to see the real shape of the thing. With two you're often guessing, and a guess baked into a shared API is expensive to undo.
So I let similar code sit side by side for a while. When three copies keep changing together, I share them. When they keep drifting apart, they were never the same component.