Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

A code reviewer rejects a pull request for violating the open/closed principle because it adds a case to a switch. When is that criticism right and when is it wrong?

The criticism is right when the switch covers variation that changes often or that other teams extend, because then every new case edits shared code and a strategy interface would let new cases arrive as new classes. It is wrong when the set of cases is closed and stable: a switch, especially an exhaustive switch over a sealed type or an enum, is simpler, and the compiler finds every switch that misses a case. Adding abstraction for hypothetical variation costs more than it saves, so we judge by the switch's change history and treat SOLID principles as guidelines, not mechanical rules.

Why this question is asked

Code review comments that cite a principle carry authority, and 'this violates open/closed' can stop a perfectly reasonable pull request. Sometimes the reviewer is right and the switch is the tenth edit to the same place this year; sometimes the switch covers four order states that have not changed since the system was built. Interviewers ask this scenario to see whether we understand the principle well enough to know when it applies, and whether we can settle a design disagreement with evidence instead of slogans. In this answer, we will recap what open/closed promises, look at a case where the criticism is right, a case where it is wrong, see how modern Java's exhaustive switches change the picture, and then use change history to decide.

1A Quick Recap of Open/Closed

The open/closed principle says a module should be open for extension and closed for modification: when requirements add a new variant, we should be able to add new code rather than edit existing, tested code. In Java this usually means an interface with one implementation per variant, chosen at run time, often injected by Spring. The principle's value is real but specific: it reduces the cost and risk of a particular kind of change, adding variants, and it pays off only if that kind of change actually happens.

2When the Criticism Is Right

Imagine a pricing service where the marketing team adds a new promotion type almost every sprint. The pull request adds a seventh case to this switch.

Java
Money discount(Order o, PromoType type) {
return switch (type) {
case PERCENT -> o.total().times(o.promo().rate());
case BUY_TWO_GET_ONE -> cheapestOfThree(o);
case FREE_SHIPPING -> o.shippingCost();
case LOYALTY_TIER -> loyalty.discountFor(o.customer());
case BUNDLE -> bundles.discount(o);
case STUDENT -> o.total().times(new BigDecimal("0.15"));
case FLASH_SALE -> flashSales.discount(o, clock.instant()); // the new case in this PR
};
}

Here the reviewer has a point. Every promotion edits this method, its file and its test class, and each case pulls in its own dependency, loyalty, bundles, flash sales, so the class grows a constructor with ever more parameters. Two teams adding promotions in the same sprint get merge conflicts in the same lines. A strategy interface, one implementation per promotion type, would let each new promotion arrive as its own class with its own tests and dependencies, and Spring could collect all implementations into a map by type.

3The Strategy Version for Frequently Changing Variants

This is what the extraction looks like. The existing switch disappears and each case becomes a bean.

Java
interface PromotionRule {
PromoType type();
Money discount(Order o);
}
@Component
class FlashSaleRule implements PromotionRule {
private final FlashSales flashSales; private final Clock clock;
FlashSaleRule(FlashSales f, Clock c) { this.flashSales = f; this.clock = c; }
public PromoType type() { return PromoType.FLASH_SALE; }
public Money discount(Order o) { return flashSales.discount(o, clock.instant()); }
}
@Service
class PricingService {
private final Map<PromoType, PromotionRule> rules;
PricingService(List<PromotionRule> all) {
this.rules = all.stream().collect(Collectors.toMap(PromotionRule::type, r -> r));
}
Money discount(Order o) { return rules.get(o.promoType()).discount(o); } // FLASH_SALE -> 12.50
}

Notice what we gained and what we paid. The next promotion is one new class; PricingService does not change. But we lost something too: nothing now checks at compile time that every PromoType has a rule, so we add a startup check that fails if a type has no rule. That trade is worth it here because promotions change every sprint.

4When the Criticism Is Wrong

Now imagine a different switch: the label shown for an order's state. The states are owned by one team, they are part of the core domain, and the last one was added three years ago.

Java
sealed interface OrderState permits Draft, Placed, Shipped, Cancelled { }
record Draft() implements OrderState { }
record Placed(Instant at) implements OrderState { }
record Shipped(String tracking) implements OrderState { }
record Cancelled(String reason) implements OrderState { }
static String label(OrderState s) {
return switch (s) {
case Draft d -> "Draft";
case Placed p -> "Placed";
case Shipped sh -> "Shipped, tracking " + sh.tracking();
case Cancelled c -> "Cancelled: " + c.reason();
}; // exhaustive: no default branch
}
label(new Shipped("1Z999")); // Shipped, tracking 1Z999
Strategy interface versus exhaustive switch
QuestionStrategy interfaceExhaustive switch (sealed or enum)
Adding a variantone new class, no editsedit each switch; compiler lists them
Adding an operationedit every implementationone new switch
Missing case detectedonly by a runtime or startup checkat compile time
Who can add variantsany team, any modulethe owner of the type
Best fitvariants change often or come from othersclosed, stable set of cases

Here a strategy per state would add four classes and a registry to replace eight readable lines, for a kind of change that almost never happens. And the switch has a safety property the strategy version lacks: because OrderState is sealed and the switch has no default, adding a fifth state makes every switch that does not handle it fail to compile. The compiler produces the exact list of places to update. An exhaustive switch over an enum gives the same check when written as a switch expression without default. For a closed, stable set of cases, this is simpler and safer, and adding abstraction for hypothetical variation would cost readability without any benefit.

5Deciding With Change History

The disagreement in the review is really a prediction about future change, so the best evidence is past change. Version control tells us how often this switch has been edited.

Shell
$ git log --oneline --since=2025-11-01 -- src/main/java/com/acme/pricing/PricingService.java | wc -l
23 # edited in 23 commits in a year: extract a strategy
$ git log --format=%h -L '/static String label/,+8:src/main/java/com/acme/order/OrderLabels.java' | grep -cE '^[0-9a-f]{7,12}$'
2 # the label switch changed twice in its life: keep it

A switch that changed in many pull requests, by several teams, for new variants, is the case open/closed was made for, and the reviewer is right. A switch that has barely changed is not, and the reviewer is applying a principle mechanically. In the second case I would reply politely with the history, mention that the switch is exhaustive so a new case cannot be forgotten, and offer to revisit if the pattern of change shifts. SOLID principles are guidelines for managing change, and their value depends on the change we actually see.

6Follow-up questions

What if the switch has a default branch?

Then the compiler can no longer help, because a default silently handles any new case, often wrongly. For sealed types and enums in our own domain I leave out default so that a new variant breaks the build at every switch. If a switch must tolerate unknown values, for example an enum from another service's API, a default that logs and fails clearly is better than one that guesses.

Can we start with a switch and extract a strategy later?

Yes, and that is often the best path. Extracting a strategy from an existing switch is a mechanical refactoring: each case body becomes a class. Starting simple and extracting when the change history shows real churn avoids paying for abstraction that may never be needed.

7Conclusion

In this answer, we saw that the open/closed principle reduces the cost of adding variants, so the reviewer is right when a switch keeps growing because promotions or other variants change often or come from other teams, and a strategy interface lets each one arrive as its own class. The reviewer is wrong for a closed, stable set of cases, where an exhaustive switch over a sealed type or enum is simpler and the compiler finds every missing case. We used the switch's change history to decide, and treated SOLID principles as guidelines for managing real change rather than rules to apply mechanically.

What to say in the interviewAbout 90 seconds aloud

Open with

It depends on what kind of variation the switch covers. Open/closed is about making it cheap to add variants without editing tested code, so it matters where variants change often or are added by other teams.

Then, if they want more

If this switch is, say, the promotion types and marketing adds one every sprint, the reviewer is right: every new case edits the same method, its tests and its dependencies, and teams collide in the same file.

Then I would extract a strategy interface, one class per promotion, collected by Spring into a map, plus a startup check that every type has a rule. But if the switch is over a closed, stable set, like order states owned by our team that last changed years ago, a switch is simpler and actually safer.

With a sealed interface or an enum and a switch expression with no default, the compiler checks exhaustiveness, so adding a state makes every incomplete switch fail to compile. Creating classes and a registry for hypothetical variation adds cost with no benefit.

To settle it, I would look at the change history: if git shows this switch changed in twenty pull requests last year, extract; if it changed twice in its life, keep it. SOLID principles are guidelines for managing the change we actually see, not rules to apply mechanically, and I would explain that in the review with the history attached.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A code reviewer rejects a pull request for violating the open/closed principle because...": https://scrivio-interview-library.netlify.app/java/oop-principles/adefc14e3f/