I’ve watched DRY get applied as a reflex more than as a rule. Two similar lines appear and a helper is extracted, and six months later that helper is a flag for every caller and a name that no longer covers any of them. The failure isn’t that the team deduplicated. It’s that they deduplicated on appearance and skipped the second sentence Hunt and Thomas wrote, the one with the word knowledge in it.

Code that looks alike isn’t necessarily the same fact. Two loops that happen to iterate the same way for different reasons are two facts. Merge them and the next change to one becomes a change to both, which is exactly the rot DRY exists to stop. The test I use is whether the two copies would have to change together. If they would, they’re one fact. If they wouldn’t, they’re two facts that currently rhyme.

The boundary matters as much as the count. Inside a bounded context, deduplication is cheap insurance, because both copies answer to the same owner and the same change. Across a context boundary, a shared helper fuses two products into a single change, and now every team that touches one has to think about the other. The coupling costs more than the duplication did.

So I extract on the second occurrence inside a context. A little copied code is cheaper than a helper with a name that means three things.

Fair to the reflex: the reflex exists because duplicated knowledge really does rot, and neglected copies drift until there’s no way to tell which is right. The rule is sound. The trigger is wrong.

The full write-up is at https://prickles.org/tenet/dont-repeat-yourself/F3

  • Homosexual sapiens@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    1
    ·
    18 hours ago

    Wow, the structure of that article is infuriating to me as a reader. And the site keeps thinking that my horizontal scrolling in the code views means I want to swipe to a different article.