Shared Solution vs. Duplication for Cross-Cutting Concerns
Tests your judgment on trade-offs between duplication, coupling, and velocity. A good answer weighs the cost of premature abstraction against future refactoring, using triggers like the 'Rule of Three' and evaluating the concern's volatility.
What's really being asked
This question tests your ability to make pragmatic architectural trade-off decisions in an agile or emergent design environment. The interviewer is not looking for a textbook definition of a cross-cutting concern, but for a nuanced decision-making framework. They want to see how you balance short-term velocity (allowing duplication) against long-term maintainability and consistency (creating a shared solution). It's a test of senior-level judgment, not dogmatic rule-following.
The full answer
A strong answer presents a framework for this decision. First, state that duplication is often acceptable or even preferable initially, aligning with YAGNI (You Ain't Gonna Need It) principles. This avoids premature, and often incorrect, abstractions. Second, introduce a trigger for re-evaluation, most commonly the 'Rule of Three'. Once a pattern is duplicated for the third time, it's time to formally consider abstraction. Third, describe the evaluation criteria for that decision. This should include: 1) Volatility: How stable is the requirement? Abstracting unstable logic creates a bottleneck. 2) Cost of Inconsistency: What is the business impact if implementations diverge? An inconsistent authentication check is a security flaw; an inconsistent logging format is an inconvenience. 3) Complexity: Is the logic trivial or complex? The more complex, the more value in a single, well-tested implementation. 4) Organizational Fit: Does a team have the charter and resources to own and maintain a shared solution?
The mistakes people make
The most common red flag is dogmatism. One wrong answer is the 'DRY Purist' who insists all duplication must be eliminated immediately, ignoring the high cost of creating, versioning, and distributing a shared library and the coupling it introduces. The opposite is the 'Decoupling Absolutist' who argues for permanent duplication to maintain service independence, ignoring the massive operational cost and risk when a critical change (like a security patch) must be applied across dozens of repositories. Another failure is ignoring the 'emergent design' context and proposing a large, upfront shared framework before the requirements are stable.
What usually comes next
Expect follow-ups that force you to apply your framework. For example: 'Let's say you have five microservices that all need to report metrics. Walk me through your decision process.' or 'You've decided to build a shared library for authentication. How do you manage its versioning and rollout without blocking all five consumer teams?' Another common one is, 'What happens when two teams have a legitimate disagreement on how the shared solution should work?'
A concrete example
Imagine two services need to validate an incoming JWT. Initially, each team implements its own validation logic, perhaps by copying a 25-line utility function. This is fast and decoupled. When a third service needs the same logic, the 'Rule of Three' is triggered. We evaluate: the volatility is low (JWT specs are stable), but the cost of inconsistency is catastrophic (a validation error is a P0 security vulnerability). The complexity is moderate and easy to get wrong. The conclusion is clear: the high cost of inconsistency justifies the effort to create a formal, shared authentication-validation library that becomes a mandatory dependency for all services handling these tokens.
Interview question
When is it most appropriate to transition from duplicated code to a shared solution for a cross-cutting concern?
- a.After the concern has been duplicated in at least three separate contexts and its requirements are well-understood and stable.Correct
- b.Immediately, to strictly adhere to the DRY principle and prevent any code duplication.
- c.Only when the duplicated logic is extremely complex, regardless of how many times it appears.
- d.Never, as shared solutions introduce coupling and hinder independent service development.
Why? this is the answer
The card advocates for allowing initial duplication (YAGNI) and using the 'Rule of Three' as a trigger, combined with evaluating the stability (volatility) of the concern before abstracting. Option B represents the 'DRY Purist' fallacy, promoting premature abstraction which can be costly and incorrect.
Just read this? Test yourself on what you have been reading.
Read the original → en.wikipedia.org
- #architecture
- #agile
- #emergent design
- #dry
- #coupling
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on architecture — each one lists the topics its interview covers.
See open roles