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

Show parent comment

4

u/w0ut Oct 25 '26

In this case, I would side with the customer. Build for today, make money, and if tomorrow is different, only then we start adapting. I would not recommend thinking ahead too far and try to make a design that can handle all eventualities, because that costs money, and when the software isn't ready yet, he isn't making money with it. The money side and the software side have to grow side by side.

You can just handle the e-mail logic in one place, so that when tomorrow comes, you are ready to redesign that part, instead of having it all over the code base.

3

u/james_pic Oct 25 '26

I'd also add that if you try to build for all eventualities, more often than not, the eventualities you end up aren't even the ones you built for, and you end up having to refactor anyway, and your refactoring needs to include the redundant code.

1

u/w0ut 24d ago

Yes, we can't really foresee 100% which way things are going to turn.

I think in my own software product, the more damaging behavior is postponing refactoring/reorganizing as soon as it's apparent that the current design no longer holds up, and I patch something on top of this design, instead of stopping and investing in a redesign/refactor right away. If you don't, the technical debt builds up, and you pay a higher price later as there will be even more to untangle.

2

u/james_pic 24d ago

Yes, 100%.

One of the most instructive projects of my career was a brief, failed, spare time project to write a decompiler. One of the neat things about writing a decompiler is that everything is affected by everything else, so if you try and push on and "I'll do it this way for now and refactor it in a bit" rather than refactoring the moment you discover the need to, the complexity has spiralled out of control in hours, not the months or years it takes in an enterprise project. Speedrunning this failure mode was eye-opening.