DRY = Don’t repeat yourself

  • William@lemmy.world
    link
    fedilink
    arrow-up
    17
    arrow-down
    3
    ·
    11 months ago

    First off, I generally don’t worry about DRY until there are 3 instances, not 2. With only 2, it’s really easy to over-generalize or have a bad structure for the abstraction.

    But otherwise, I disagree with the article. If it’s complicated enough to bother abstracting the logic, the worst that can happen in the above situation is that you just duplicate that whole class once you discover that it’s not the same. And if that never happens, you only have 1 copy to maintain.

    The code in the article isn’t complicated enough that I’d bother. It even ends up with about the same number of lines of code, hinting that you probably haven’t simplified things much.

    • robinm@programming.dev
      link
      fedilink
      arrow-up
      3
      ·
      11 months ago

      I personally factorize as soon as there are two copies, but do not hesitate to inline the code and redo the abstraction when there is a 3rd use if it doesn’t fit. I find it much easier to inline and re-abstact a bad abstraction, than check if two copies are indeed identical.

      The exception is business logic. Usually I want all of them to be dupplicates because there is a very high chance that it’s just accidental that part of the logic is similar. I take great care to have good primitives but the actual business logic that glue those primitives together is written as many time as needed.

    • esa@discuss.tchncs.de
      link
      fedilink
      arrow-up
      1
      ·
      11 months ago

      Yeah, I’m reminded of how Germanic languages used to have singular, dual and plural. If we’d still had dual, we’d probably also be talking about not abstracting until we actually have a plural.