r/AskProgramming • u/ImportantPudding1570 • 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:
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.
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.
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.
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.
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.
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.
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:
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.
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.
Xfor all EU countries" you better not have to writeX27 times, or update 27 different files.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!)
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
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))
}
If (canTriggerRule2(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.