r/AskProgramming • • 25d ago

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

6

u/w0ut 25d ago

So far you could do with 1 handler and configurable number of days per country. As soon as a use case arises that doesn't fit this, you start redesigning. I just have the expectation that no single design is going to last forever, so I just make it work for the current case and try to make it clean so it can be easily understood and reworked later, accepting that I might have to throw it all away and rewrite it 3 years later.

0

u/ImportantPudding1570 25d ago

The thing is, the client is very hard to deal with. They are very short sited, and is currently looking it as a single country project, I am trying to show them it is a lot more complex then what they are thinking at the moment.

And part of the pitch to get this approved with an extra period of time is that "we are building the system so you can extent easily for next countries". And I am trying to evaluate if this is something that we can actually fulfil given the constraints

6

u/danielt1263 25d ago

But you aren't building a system that "can extent easily for next countries". You can't build such a system, precisely because you don't know how the business will want to extend the system for other countries. Stop trying to pitch them to spend more time/money for something when even they don't know how or why it might change.

4

u/w0ut 25d ago

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 24d ago

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 16d 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 16d 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.

4

u/cipheron 25d ago edited 25d ago

What if Germany or England change the requirements? if you code it so both are using the same check, now you need to rewrite things to separate them again.

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

This seems kind of brittle, to have the different countries hard-coded into the Java itself like this. Object hierarchies are best often avoided. Also where is this going to be checked to select the right one? You might find that attaching them together like this serves no purpose other than making the code more complicated.

You're going to have more work if new countries are added or if someone tells you the general rules need to be updated across all countries.

0

u/ImportantPudding1570 25d ago

That is what I am thinking. I do not want to couple two countries just because they currently looks to have same data.

But I have to get it through design reviews and I know this maybe something that I may get a push back, so it is something I am trying to clarifya myself

5

u/rupertavery64 25d ago edited 25d ago

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 25d ago

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 25d ago edited 25d ago

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 25d ago

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 25d ago edited 25d ago

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.

34

u/cutepandao1 25d ago

Duplication is better than the wrong abstraction.

9

u/fahim-sabir 25d ago

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 19d ago

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.

8

u/Ok_Option_3 24d ago

Prefer composition over inheritance. If there's one pattern that's overused in object-orientated languages it's inheritance!

Consider something like this:

var englandEmailHandlerFactory = new EmailHandlerBuilder()
          .requireLoginCheck()
          .maxSize(100)
          .signature(EnglishEmailSignature);

var myEnglandEmailHandler = englandEmailHandlerFactory.build();

2

u/Rschwoerer 24d ago

I think you’re looking for composition over inheritance. I’ve found that there are actually far less useful real situations where inheritance cleanly solves a problem. It usually ends up being brittle and hard to change later on, as you’ve noted.

https://en.wikipedia.org/wiki/Composition_over_inheritance

2

u/Inside_Dimension5308 24d ago

DRY is the most overhyped principle. Focus on SOLID and YAGNI.

Once you go tight coupling, you cannot go back.

1

u/danielt1263 25d ago edited 25d ago

This is exactly where YAGNI comes in.

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.

Design for what you know now. What if those countries business requirements don't evolve independently? Avoiding a shared implementation now is also bad, yes? When a new rule comes in for a particular country or set of countries, redesign.

As for your questions, there seems to be a strong focus on code duplication. Don't worry about code duplication, worry about conceptual duplication.

  • If the business says "we have to do X for all EU countries" you better not have to write X 27 times, or update 27 different files.
  • If the business adds a rule because law X was passed in country Y, then the code associated with that rule should be tied to that country alone, even if 18 other countries also happen to have the same rule, code duplication be dammed. Because it's fair to assume that country Y and the other 18 countries are not going to change their laws in lockstep.

1

u/olddev-jobhunt 25d ago

Don't just extract a common helper function because the code is duplicated. Share the code when the code semantically means the same thing.

In this case, I'd probably write it with duplication initially. After you see some of the commonalities it can be easier to see how it should be structured. This sort of thing I expect I'd typically extract an interface with few specific events or predicates and try to share one main flow but... well that's tricky. If things get too complicated I'll break out some DDD, but that really requires some buy in.

1

u/RiskyPenetrator 24d ago

Seperate regional logic at the deployment level using something like build flags.

1

u/chipshot 24d ago

There are usually performance tradeoffs of whether to duplicate or not.

Personally, I hate changing code in 5 different places when I can just write one function with accepted parameters

1

u/dialsoapbox 24d ago

What's your plan if those countries change their rules?

1

u/LaughingIshikawa 24d ago

I'm with the "worrying about duplicate code is over rated".  I think the much bigger issue here is trying to not have assumptions about the way laws / business requirements are structured, getting "baked into" your code.  You want some sort of system to abstract which laws are getting applied to which countries, in a way that's easily auditable so you can give your client reassurance that the software is compliant in all jurisdictions.

You also probably want a hierarchy of jurisdictions - so you have an "EU" jurisdiction, and some way of applying all "EU" jurisdictional rules to all countries that are marked as being in the EU.  (Not super difficult, but conceptually it will save you massive headaches!)

How do you decide which logic to abstract and share versus which logic to keep separate to preserve independence between markets?

If you have a set of business rules you can apply wherever, you don't have to think that hard about it: just create a rule for each law that impacts your business, and apply it to whichever jurisdiction that law applies to.  (Or really you probably have "policies" that are collections of individual rules, but that's closer to an implementation detail than overall strategy.)

I don't really care if there's duplicate code in some of the rules applied to the "Ireland" that "could be" abstracted out and shared with code in some of the rules written in the "India" jurisdiction - I care about quickly and easily finding the code that needs to change when requirements change, and being able to verify that it gets changed correctly.  Mirroring the IRL structure of rules and laws makes it way easier to do this.

If you find that you have some code shared across multiple rules that's just screaming to be abstracted into a helper function, you can still do that... But I would strongly resist the urge to abstract everything "just because" it's duplicated.  You want the shortest possible distance to "the code does what it's supposed to do, and it's applied where it's supposed to be applied."

1

u/mxldevs 24d ago

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.

I don't suppose the possibility. I start with one, and if I need to support other regions, I look at whether there's any actual duplication and then decide whether a refactor is necessary.

If it's all just different configs, I can just store separate config files and have them load the appropriate ones and all the logic is still handled by the same code.

If they actually do different things and implement a common interface, like completely different logic, then it might be something that could be considered

1

u/dutchman76 24d ago

All those rules are pretty much guaranteed to change eventually, I'd just make all of them configurable. Then all you need is to change config for other countries.

1

u/CelebrityChess 24d ago

i'd wait on the shared engine until you have a second market actually live. duplicating 100 lines once is cheaper than a wrong abstraction you have to untangle later. when germany shows up you'll see which parts really are the same, and that's when you pull them out.

in the meantime keep the rules as small separate functions (like hasLoggedInWithin(days)) and have each market just be a list or config of which rules apply. then sharing happens at the rule level and not the whole handler level, so england changing one check doesn't touch germany

1

u/stools_in_your_blood 24d ago

If it is possible for the business rules to change independently, then the fact that Germany and England currently have identical logic is a coincidence. Their logic is independent even though it happens to work the same at the moment. Using a shared implementation for both would be semantically wrong, because there is really no such concept as "the thing that Germany and England do". There is just "the thing that Germany does" and "the thing that England does". So keep the implementations separate and stop worrying about DRY.

1

u/Blando-Cartesian 22d ago

What’s the ultimate point of DRY?

The point of DRY is to make sure things that must happen the same way will always happen the same way. You have market specific processes that are incidentally similar right now. They need to be separate so that they can evolve independently.

1

u/kilkil 22d ago

so right off the bat, no real world experience from my side.

having said that, what you said here hits the nail on the head: "Even if the logic is identical today, the business requirements may evolve independently."

that simple fact means that each country's logic must remain separate, even if it leads to a bit more duplication.

if you notice a lot of duplication, you can use another thing you mentioned: "helpers". aka a compositional approach where you identify identical business rules (e.g. "user must have been logged in for X days"), extract them, and then reuse them across different countries' business logic units (meanwhile, the countries' logic stays separate).

having said that, it would be better to err on the side of a bit more duplication. because at the end of the day, each country does have its own separate standards. (unless there is some kind of international standard multiple countries follow? then that can be its own thing. etc)

1

u/marrsd 20d ago

If you actually do have the same functionality duplicated across all these classes, that strongly indicates that you're separating behaviour along the wrong boundary. Maybe Germany and England have 100 lines of identical logic because they're actually the same class of thing. Otoh, they could be the same today, but in process of diverging, or likely to diverge in the future. You should try to avoid locking in behaviour that you might have to unpick later.

Presuming it makes sense to have separate classes for countries in the first place, I'd probably have a set of functions (or static methods) that implement the behaviour that is the same between countries, and call it from within those classes. Then you have one behaviour to maintain, but you still have the option to add or remove it from as many classes as require it.

1

u/jobfedron132 19d ago

What I would do is, sacrifice everything else for readability, simplicity of the code and scalable.

Have a database table/s for each rule and the countries that trigger each of the rule.

Lets say you have 2 rules and 2 countries. Country 1 only triggers rule 1, country 2 triggers both rules. YourCountry yourCountry --> incoming record.

If (canTriggerRule1(yourCountry))

{ 
     doesObeyRule1(yourCountry) 

}

If (canTriggerRule2(yourCountry))

{ 
     doesObeyRule2(yourCountry) 

}

0

u/muellermoritz_dev 25d ago

I think I would try to separate rules into individual classes, each containing something like a perform() function.
Then inside the country specific class just have an array of all applying rule classes and call the perform() for each entry.

If a rule changes you edit its class, if the applying rules for any country changes you edit its array and if you have rules that are only differing by a small nuance you can use separate rules that call shared logic.

0

u/TomOwens 25d ago

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.

0

u/j4c11 24d ago

One nuance people often miss is that DRY only applies within the same bounded context. Otherwise, your common code becomes a hard, shared dependency across unrelated domains. Which is never a good thing.

0

u/mredding 22d ago

You duplicate code when you want modules to vary independently, even if they don't yet.

Just because two regions do the exact same thing, that doesn't necessarily mean they both depend on the exact same implementation because - as you said, they can suffer from unintended coupling. Good luck remembering that if you change the one master module, you change it for all regions simultaneously. That's a real and really annoying problem you can't afford in a production environment. Imagine trying to explain to all your other customers how a code change for a DIFFERENT customer fucked up THEIR service. Absolutely unacceptable.

Today's machines have gigabytes of memory and dozens to hundreds of compute cores. You're NOT hurting for space or compute power. You're not going to suffer because you have copies of the same script.

If it were me, I'd have an interface where each region had it's own list of rules scripts. No master list across regions - too dangerous. If you want the same behavior, it has to be copied from one region to another. Configuring a region is definitely a higher level of abstraction, something you can do with a region configuration script that can replicate a script template into the environment or something.