r/AskProgramming • • Oct 25 '26

Is this considered duplicate code?

How do you decide when code duplication across different markets is actually worth eliminating?

I'm building a framework to automate complex business logic for incoming customer support emails. The system is currently being implemented for England, but it will eventually expand to Germany, the USA, India, and other markets.

The challenge is that many business rules are shared across markets, while others are market-specific.

For example:

  • Germany and the USA require checking whether a customer has logged in within the last 30 days.
  • England and India don't require this check.
  • Other parts of the email-processing workflow may be common across all markets, while some may differ.

My initial approach was to create a common interface with separate implementations for each market:

interface EmailHandler {
    void processEmail();
}

class EmailHandlerEngland implements EmailHandler { }
class EmailHandlerGermany implements EmailHandler { }
class EmailHandlerUSA implements EmailHandler { }
class EmailHandlerIndia implements EmailHandler { }

However, I'm concerned that this could lead to significant code duplication as the framework grows.

On the other hand, suppose Germany and England currently have 100 lines of identical logic. Even if the logic is identical today, the business requirements for those countries may evolve independently. Sharing the implementation could introduce unintended coupling, where a change intended for one market affects another.

I am offcourse planning on building common "Helpers" and whatever can be configurable, will be configurable. However, we currently will only be getting requirements for one market, we can not tell from start if something that we will use, will be reused by other markets.

My question is: How do you approach this trade-off in practice?

  • If I go with the initial approach, will these same line of code will actually be considered as a duplicate and will break the DRY principle?
  • If Yes, would you consider identical code across markets to be duplication worth eliminating, even when the underlying business rules may evolve independently?
  • Would you build a shared processing engine with market-specific configuration or strategies, or maintain separate implementations for each market?
  • How do you decide which logic to abstract and share versus which logic to keep separate to preserve independence between markets?

I'm particularly interested in the reasoning behind these decisions and any real-world experience building systems with complex, evolving market-specific business rules.

4 Upvotes

39 comments sorted by

View all comments

9

u/fahim-sabir Oct 25 '26

Principles are not rules. Treat DRY as a guideline. Not as a law.

Focus on readability even that leads to repetition.

Don’t abstract out for the sake of future simplification until you actually need it. Abstraction enhances readability when the number of cases is wide, but until then it adds unnecessary cognitive load. Given the limited number of cases, I wouldn’t be looking to build “rules” and an “engine” at this juncture.

2

u/balefrost Oct 30 '26

Abstraction enhances readability when the number of cases is wide, but until then it adds unnecessary cognitive load.

Disagree that abstraction adds cognitive load. Bad abstractions add load. Good abstractions reduce it.

As an example, it's sometimes useful to extract a function even if it will only be called in one place. It lets you give a name to a chunk of code. It reduces the amount of code at the callsite that the reader has to keep in their head. And if the extracted function is a pure function, then it makes it easier to reason about mutable local variables in the calling function.

I would bet that you, like me and like most developers, routinely use third party libraries (including the standard library of your language). I'd also bet that you, like me, have not spent a lot of time studying the implementation of those libraries. They provide functionality at a level that is useful in your application by abstracting away implementation details that are not interesting to you. That reduces your cognitive load.