メシのタネ

メシのタネになる、AIや技術総合情報のサイト


DRY vs. YAGNI: When Is an Abstraction Worth It?


  1. English
  2. DRY vs. YAGNI: When Is an Abstraction Worth It?
Two wooden joinery pieces beside a complex brass adjustment jig.

広告

日本語版

“Leaving that duplication in is lazy.” “Ever heard of YAGNI?” Somewhere, someone is having this code review right now.

DRY stands for “Don't Repeat Yourself.” YAGNI stands for “You Aren't Gonna Need It.” Both sound like a principle you can win an argument with. My first question would be how much work this abstraction will save us, and what we're signing up to maintain in the meantime.

What we buy when we abstract

Here I'm talking about putting the common parts of several implementations behind one interface. Callers get a consistent way to use them, and fewer details to hold in their heads. When those callers all follow one business rule, changing that rule in one place is a real benefit.

There's a bill attached. Somebody has to find the shared shape, name it, teach it, and work out what a later change will affect. An interface can hide incidental details or hide the very difference someone needs to debug. The distinction tends to show up later, when a person who didn't write it has to change it.

When the investment turns into debt

I look for three things.

  • A way to earn it back. What change is this supposed to make cheaper? How likely is it, and when?
  • Someone to pay the carrying cost. Who will review changes to the abstraction, explain it to new teammates, and untangle a new caller that almost fits?
  • An exit. If the callers start diverging, can we split them apart? If only the original author knows how, the team is already paying interest.

Say you spend three days on a reusable layer. The new use case arrives six months later and forces you to redesign the layer before you can use it. That was three days of extra work. Compare that with a rule you've had to fix in three places this month, always for the same reason. You have something concrete to base the decision on.

DRY and YAGNI answer different questions

DRY asks where the same knowledge lives today. Two similar-looking blocks of code may embody different rules. Two very different implementations may have to obey one rule. Which would you expect to change together?

YAGNI asks how much of a future requirement you're willing to pay for now. You can give two bits of code good names, keep their responsibilities clear, and revisit the abstraction when you have another real use case. That's a design decision too.

Before extracting something, try writing down the change it will pay for, when you expect it, who will maintain it, and how you'll back it out. If nobody can answer when it pays for itself, I'm comfortable calling it a cost.

Q&A

Should I wait until I've copied something three times?

Use the third occurrence as a cue to look again. A clear shared invariant might belong in one place as soon as you find it. Identical-looking code that serves two independent rules can stay separate even after a third copy makes you pause.

How can I tell whether two implementations will change together?

Look at the reasons for past changes and the examples stakeholders expect. If a business-rule change would require both implementations to move in lockstep, that's evidence of shared knowledge. If they can reasonably diverge, name that difference before deciding.

How do I make an abstraction easier to remove?

Keep its contract small, minimize the number of callers that depend on its internals, and give features tests that describe their own behavior. Then you can split a caller away and check that it still works.

For a way to capture the assumptions behind that call, see How far does this design decision apply?.


コメントを残す

メールアドレスが公開されることはありません。 ※ が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください。