The Long Build

The case against the clever abstraction

Duplication is cheaper than the wrong abstraction, and the bill goes to the reader.

4

The first abstraction I ever regretted was a beauty. It took three different kinds of report, found the shape they shared, and folded them into one configurable engine with eleven options. It was elegant. It was also the reason a two line change took a week, because every change had to be argued past all the other things the engine could now do.

The shape of the regret

Clever abstractions share a signature. They are born from the pleasing discovery that two things are alike. They grow options the way a coat rack grows coats. And one day somebody needs a case that is almost, but not quite, like the others, and the abstraction has to bend.

Duplication is far cheaper than the wrong abstraction.

That line, from Sandi Metz, is repeated so often it has lost its sting. So here is the version that stuck with me after the report engine: the cost of the wrong abstraction is not paid by the person who wrote it. It is paid by every person who has to understand it, forever, one confusing conditional at a time.

A laptop showing dense code on a dark screen
A laptop showing dense code on a dark screen

Wait for the third

My rule now is to tolerate duplication until I see the same thing three times, and even then to check that the three are alike for the same reason. Two functions that happen to share code today may diverge next quarter because their jobs are different. If they are alike by coincidence, merging them couples two futures that had nothing to do with each other.

A quick test I use: describe the shared piece without using the word "and". If I need "and", it is two things wearing one name.

What I reach for instead

When the urge to abstract arrives, I try these in order:

  1. Do nothing. Leave the duplication. Write a short comment linking the two places, so a reader knows they are cousins.

  2. Extract data, not behavior. A table of values is almost always safer to share than a function that interprets options.

  3. Extract a small, boring function. Something with one verb and no flags.

  4. Only then, a real abstraction. With a name that says what it is, and a boundary that says what it refuses to do.

Notice that the list gets more dangerous as it goes. Each step adds a thing others must learn.

Flags are the smell

The most reliable symptom of a doomed abstraction is the boolean parameter. A function called with true, false, true is a function nobody can read at the call site. Every flag is a place where two behaviors were glued together instead of separated.

When I meet one, I try splitting it into two functions with honest names. Nine times out of ten the two share a few lines, and those lines can stay duplicated without anyone suffering.

The kindness of plain code

Plain code is a courtesy to the reader. It says: here is what happens, in the order it happens, and you need to know nothing else. It will be a little longer than the clever version. It will be much shorter than the time spent decoding the clever version at two in the morning during an outage.

I still love a good abstraction. I just no longer trust my first feeling that I have found one. These days I let the code sit, watch what changes, and promote a pattern only after it has survived a few seasons of real use. By then it has usually told me what it wants to be.

built with unnamed