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

0

u/TomOwens Oct 25 '26

Duplicate code isn't just code that's the same. It's code that's the same that needs to change at the same time.

For example, just because Germany and the USA require checking that a customer has logged in within the last 30 days doesn't mean that the check is duplicate code. The reasons for the code are different. One piece of code implements rules and regulations in Germany, while the other implements rules and regulations in the USA. Nothing ties German rules and regulations to American rules and regulations. Even if the code is the same today, I wouldn't consider it duplicate code because a change in one locale wouldn't necessitate a change in the other, and forcing the change could be a problem.

However, consider the generic problem of checking if a customer has logged in within the last N days. That is tied to your system architecture and design. Changing how you check whether a customer has logged in would be driven by changes to your data storage system. So it could make sense to remove duplication around how to check whether a customer has logged in to a central place, with appropriate parameterization.

Of course, there's always the tradeoff of decoupling too early. Implementing the rule in two places to decouple it also takes time, both to develop and to test those rules. Personally, since I understand that rules and regulations change independently, I'd take the extra time and effort now. But not all cases of potential duplication are readily apparent - sometimes, you'll only see it when you go to make a change and need to make it in multiple places.