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

5

u/rupertavery64 Oct 25 '26 edited Oct 25 '26

It seems like what you need is a rule engine.

That way you can keep related code in one place, and apply it generally to the pipeline. Then perhaps have a method to check if the rule applies, and another to apply the rule.

public abstract class Rule {
    public abstract bool RuleApplies(Email email);
    public abstract void ApplyRule(Email email);
}

Then have a list of rules and apply then to the email.

It makes it easy to add rules, whilst keeping the knowledge about the rule close together, rather than spread out across several files.

You can implement different rules across different markets.

And the neat thing about this is since the rules are separate you can manage them programmatically. i.e. you can configure rulesets through a UI instead of hardcoding them.

0

u/ImportantPudding1570 Oct 25 '26

Yes a "Rule" class will be there, but then, do I need an "Engine"?
Or can I just hardcode the rules to check / execute on each country's implementation?

5

u/rupertavery64 Oct 25 '26 edited Oct 25 '26

Well the point is that you are moving away from per-country configuration. And if you actually already did that, then that's a problem. because you aren't writing rules, you are configuring countries.

To be clear, the country check is in the rule.

Why? Because you've literally written the rule down like that.

It is a rule about checking whether a customer has logged in within the last 30 days.

Consider this:

public class HasCustomerLoggedInWithinTheLast30DaysRule : Rule {

    public override bool RuleApplies(Email email)
    {
        var country = email.Customer.Country;
        return country == "Germany" || country == "USA"; 
    }

    public override  void ApplyRule(Email email)
    {
       // perform check...
    }
}

All the logic related to that rule, as well as the implementation is in one place. If the rule changes, you only change it in one place.

There is no per-country implementation. Instead, you apply the rule to all emails. You create a List<Rule> and apply them.

Now, if you have a different market, you can have a different list of rules. And since some or most of the rules might be the same, you get to reuse a lot of code efficiently.

The trick is making the rules granular enough so you can build on top of them.

You can also possibly pass in a configuration object to the rule so that the rule can be reused and still be flexible.

1

u/ImportantPudding1570 Oct 25 '26

Nothing has been "done" yet. it is still something in designing phase only. Here are my argument against the rule engine. I am not saying I am against having a rule engine. In fact it is something that I initially thought, and then after thinking though it felt overkill for my case, but I am happy to look into this more

  1. Once it is configured, admins are hardly going to change it. maybe once or twice a year. unless they want to add a new functionality all together.
  2. The whole system is complex, and it is very likely that adding a new rule will mean that a developer will need to implement a class. It is definitely not something that would be like "Here are 100 rules, now you can add as many countries as you like".
  3. There are nested rules as well, for example, for germany if a customer is active then check for this and do this. and for USA, if a custom is active, check the other thing, and do the same thing as for germany
  4. Creating the Rules Engine with this complexity, will mean a lot of effort will need a significant amount of development effort and operational overhead. But it is not something that a non developer is going to configure and maintain, it is not like an admin is ever going to change the rules on whim. we are talking about adding / removed 2-3 rules maximum in an year. I think the rule "engine" maybe an overkill. and You ain't gonna need it

2

u/rupertavery64 Oct 25 '26 edited Oct 25 '26

Well, you asked for my advice, and I gave it.

  • Would you build a shared processing engine with market-specific configuration or strategies, or maintain separate implementations for each market? - Yes
  • How do you decide which logic to abstract and share versus which logic to keep separate to preserve independence between markets? - By using Rules

The rule engine addresses all your problems.

  • Identical code across markets? Rulesets
  • Avoiding code duplication

What you haven't considered at all is maintenance. In your approach of hardcoding, all the logic will be spread out and you don't know except by documentation somewhere what rules apply where. Or testability. Can you test a single rule is working?

You can hardcode rulesets if you believe they are never going to be changed. I'm not saying you should build an elaborate and ultraconfigurable UI that users are going up update on a daily basis. I merely proposed that as a benefit.

The rule engine in itself is just the architecture and the list of rules.

YAGNI is overused.

A rule engine like this is simple and you are just afraid of "complexity" but ignoring architecture. There can be simplicity from "complexity". (Honestly it's not complex at all)

If you are still fretting about how complex it is, it's not. You're literally putting the code you would put per country (duplicated) into a method (deduplicating), then moving the method into a class.

You can absolutely create Rules that are very specfic.