DRY Is a Principle, Not a Rule

DRY Is a Principle, Not a Rule

We learn DRY early: do not repeat yourself. So we extract, generalize, and parameterize. And sometimes that is exactly right. But a principle is not a rule. DRY tells you duplication has a cost. It does not tell you abstraction is free. Every abstraction has a cost too, and the wrong one costs more than the duplication it removed.

我们很早就学会了 DRY 原则:不要重复自己(Don’t Repeat Yourself)。因此,我们进行提取、泛化和参数化。有时这确实是正确的做法。但原则不是规则。DRY 告诉你重复是有代价的,但它没告诉你抽象是免费的。每种抽象也都有代价,而且错误的抽象比它所消除的重复代价更高。

Duplication is visible. A wrong abstraction is not. Duplicated code is easy to see, easy to find, and easy to delete later. Two similar functions sitting side by side are honest about what they are. You can read each one on its own and understand it. A shared helper with six flags is a different story. Nobody dares touch it, because changing one flag’s behavior might break five callers nobody remembers.

重复是显而易见的,而错误的抽象则不然。重复的代码很容易被看到、找到,以后也容易删除。两个并排的相似函数诚实地展示了它们的功能,你可以独立阅读并理解每一个。但一个带有六个标志位(flag)的共享辅助函数则完全不同。没人敢动它,因为改变一个标志位的行为可能会破坏五个没人记得的调用者。

The duplication is gone, but the complexity moved somewhere harder to see: into the branching, the parameter combinations, and the mental model you need before you can change anything. That is the trade most DRY discussions skip. Duplication costs you keystrokes and a wider edit when something changes. A premature abstraction costs you understanding, and understanding is the more expensive currency.

重复消失了,但复杂性转移到了更难察觉的地方:转移到了分支、参数组合,以及你在进行任何修改前必须构建的思维模型中。这是大多数关于 DRY 的讨论所忽略的权衡。重复会消耗你的击键次数,并在发生变更时需要更广泛的编辑。而过早的抽象会消耗你的理解力,而理解力是更昂贵的货币。

When duplication is the better choice: Duplicate first when the two copies only look alike. Similar code is not the same code. Two blocks that happen to do the same thing today, for different reasons, will change for different reasons tomorrow. Merge them now and the next change has to fight the abstraction: a new flag, a special case, an if statement that exists only because two callers disagree.

当重复是更好的选择时:当两份代码只是看起来相似时,先进行重复。相似的代码并不等同于相同的代码。两个今天恰好做同样事情的代码块,如果是因为不同的原因而存在,那么明天它们也会因为不同的原因而改变。现在合并它们,下一次变更时就必须与抽象作斗争:增加一个新的标志位、一个特殊情况,或者一个仅仅因为两个调用者意见不一致而存在的 if 语句。

Duplicate first when you do not yet know what is shared. The first time you write something, you are guessing at the shape of it. The second time, you have a comparison. It is usually the third time that the real pattern shows itself, the part that is genuinely shared rather than coincidentally similar.

当你还不确定什么是共享内容时,先进行重复。第一次编写某段代码时,你是在猜测它的形态。第二次时,你有了对比。通常到了第三次,真正的模式才会显现出来,即那些真正共享的部分,而不是巧合下的相似。

Duplicate first when the copies are small and stable. Three lines repeated in two places is not a maintenance crisis. It is three lines. The abstraction that replaces it can easily be longer than the duplication it removed, and longer to understand.

当重复的代码简短且稳定时,先进行重复。在两处重复三行代码并不是什么维护危机,那仅仅是三行代码。用来替代它的抽象往往比它所消除的重复代码更长,也更难理解。

When abstraction earns itself: Abstraction earns itself when the shared part is stable and the differences are explicit. A good abstraction has few parameters, a clear name, and callers that all want the same thing from it. If you can describe what it does in one sentence without saying “except when,” it is probably ready.

当抽象物有所值时:当共享部分稳定且差异明确时,抽象才物有所值。一个好的抽象应该参数少、命名清晰,且所有调用者都从中获取相同的东西。如果你能用一句话描述它的功能,且不需要说“除非……”,那么它可能就准备好了。

It also earns itself when the cost of getting it wrong is high. A validation rule, a security check, a calculation that must agree everywhere: that is one idea, and it should live in one place. Duplicating that is not pragmatism, it is a bug waiting for one copy to drift.

当出错代价高昂时,抽象也物有所值。验证规则、安全检查、必须在各处保持一致的计算:这些都是单一的逻辑,应该只存在于一个地方。复制这些逻辑不是务实,而是等待其中一份副本发生偏差的 Bug。

The difference is whether the copies share a reason to change. If they change together, for the same reason, they are one idea wearing two costumes, and one place is right. If they change separately, for different reasons, they are two ideas that happen to look alike, and merging them creates a coupling that was not there before.

区别在于这些副本是否共享同一个变更原因。如果它们因为相同的原因一起改变,那么它们就是一个穿着两套外衣的单一逻辑,放在一个地方是正确的。如果它们因为不同的原因分别改变,那么它们就是两个恰好看起来相似的逻辑,合并它们会产生之前不存在的耦合。

A test that actually helps: Before extracting, ask one question: what changes next? If the answer is “both copies, in the same way, for the same reason,” extract. If the answer is “one of them, and I am not sure which,” wait. Write the duplicate, let the pattern earn its abstraction, and revisit it when the third copy arrives. By then you will know what is actually shared, because you will have seen it change.

一个真正有用的测试:在提取之前,问一个问题:接下来会发生什么变化?如果答案是“两份副本都会以相同的方式、为了相同的原因而改变”,那就提取。如果答案是“其中一份会变,但我不知道是哪一份”,那就等等。先写重复的代码,让模式通过实践证明其抽象的价值,等到第三份副本出现时再重新审视。到那时,你就会知道什么是真正共享的,因为你已经见证了它的变化。

That is the version of DRY worth keeping: do not repeat knowledge, not do not repeat text. Two copies of the same text are a smell. Two copies of the same idea, kept in sync by discipline, are a choice. And a choice you can revisit is cheaper than an abstraction you cannot unwind.

这才是值得保留的 DRY 版本:不要重复知识,而不是不要重复文本。两份相同的文本是一种代码异味(smell)。两份相同的逻辑,通过纪律保持同步,是一种选择。而一个可以重新审视的选择,比一个无法撤销的抽象要廉价得多。

Your turn: Where do you stand: abstract early, or duplicate first? And what is the worst premature abstraction you have had to unwind, the helper with too many flags, the base class that fit nothing quite right? What made it so hard to undo? I would rather hear one specific story than a general opinion. Which abstraction, and what did it cost?

轮到你了:你的立场是什么:尽早抽象,还是先重复?你遇到过最糟糕的过早抽象是什么,是带有太多标志位的辅助函数,还是那个什么都不太适配的基类?是什么让它如此难以撤销?我更想听一个具体的故事,而不是泛泛的观点。是哪种抽象,它付出了什么代价?