Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

A pricing service has a 300-line if-else chain over customer type, region and product category that grows every month and causes regressions. How would you restructure it, and when is a long conditional actually fine?

We first pin the current behaviour with characterisation tests, then model the variation explicitly instead of in branches: a sealed interface with records and an exhaustive switch when the variation is a closed set of types, strategy objects in a map keyed by the dimensions when behaviour differs per combination, or a rules table in configuration or the database when the business owns rules that change often, each rule with its own test. Exhaustive switches over sealed types turn a forgotten case into a compile error. And a long conditional is actually fine when it is stable, small in reach and well tested, because every abstraction has a cost.

Why this question is asked

Almost every long-lived business system has one method like this: a pricing or eligibility function that started as five lines and grew one if at a time, because each new rule was urgent and adding a branch was the fastest change. Interviewers ask about it because the right answer is not a pattern name. It is a process: make the change safe, find the real shape of the variation, pick the structure that matches it, and know when to leave the code alone. In this answer, we will look at why the chain keeps causing regressions, how to capture its behaviour before touching it, three ways to restructure it and when each fits, and the cases where a long conditional is the right design.

1Why the Chain Keeps Causing Regressions

Here is the shape of the problem in miniature. The real method has 300 lines, but the pattern is the same: conditions on customer type, region and category, nested and repeated, with special cases added near the top.

Java
BigDecimal price(Customer c, Product p) {
BigDecimal base = p.listPrice();
if (c.type() == RETAIL) {
if (c.region() == EU && p.category() == BOOKS) return base.multiply(new BigDecimal("0.95"));
if (c.region() == EU) return base;
if (p.category() == BOOKS) return base.multiply(new BigDecimal("0.90"));
return base;
} else if (c.type() == WHOLESALE) {
if (p.category() == BOOKS && c.region() != APAC) return base.multiply(new BigDecimal("0.70"));
// ... 280 more lines
}
return base;
}

The reason it breaks is that the order of the branches is part of the logic. A new rule added in the wrong place shadows an older one, and nobody can tell which of the hundreds of combinations a change affects. Each fix is local, but the effects are global, so regressions are inevitable.

2First, Capture the Current Behaviour

Before changing anything, we write characterisation tests: tests that record what the code does today, right or wrong, so we notice any change in behaviour. With four customer types, six regions and ten categories there are only 240 combinations, so we can run all of them through the old method, save the results, and assert that the new code gives the same answer for every one. Where the result looks wrong, we ask the business, but we fix it in a separate change, so a refactoring never hides a behaviour change.

Java
@ParameterizedTest
@MethodSource("allCombinations") // 4 types x 6 regions x 10 categories = 240 cases
void sameAsLegacy(CustomerType t, Region r, Category cat) {
Customer c = new Customer(t, r);
Product p = new Product(cat, new BigDecimal("100.00"));
assertEquals(legacy.price(c, p), pricing.price(c, p));
}

Notice that the legacy class stays in the test code during the migration. It becomes the specification we compare against, and it is deleted only when the new structure has replaced it.

3Option 1: Sealed Types and an Exhaustive Switch

If the main variation is a closed set of kinds of customer, each with its own data and its own rules, we can model them as a sealed interface with records and price them with an exhaustive switch. Each kind's rules sit in one arm, and when a new kind of customer is added, every switch over the interface stops compiling until it is handled.

Java
sealed interface Customer permits Retail, Wholesale, Partner { Region region(); }
record Retail(Region region) implements Customer { }
record Wholesale(Region region, int volumeTier) implements Customer { }
record Partner(Region region, BigDecimal agreedDiscount) implements Customer { }
BigDecimal price(Customer c, Product p) {
return switch (c) {
case Retail r -> retailPrice(r, p);
case Wholesale w -> wholesalePrice(w, p);
case Partner pa -> p.listPrice().multiply(BigDecimal.ONE.subtract(pa.agreedDiscount()));
}; // no default: adding a new Customer type is a compile error here
}

This fits when the kinds change rarely and are owned by developers. It does not help much when the number of rules is large and they change monthly, because every rule change is still a code change.

4Option 2: Strategies or a Rules Table

When behaviour depends on combinations of dimensions, we can make the combination the key. A small record of customer type and region keys a map of strategy objects, each a class or lambda with one pricing rule. Finding the rule becomes a lookup instead of a walk through branches.

When the rules are business-owned and change often, the next step is to move them out of code into a rules table, in configuration or a database table that the business can review. Each row says which combination it matches, with a wildcard for any value, a priority and an adjustment. The code becomes a small engine: find the matching rows, pick the most specific or the highest priority, and apply it. Each rule gets its own test case, often written straight from the row.

Java
record Rule(CustomerType type, Region region, Category category, int priority, BigDecimal factor) {
boolean matches(Customer c, Product p) {
return (type == null || type == c.type())
&& (region == null || region == c.region())
&& (category == null || category == p.category()); // null means any
}
}
BigDecimal price(Customer c, Product p, List<Rule> rules) {
return rules.stream()
.filter(r -> r.matches(c, p))
.max(Comparator.comparingInt(Rule::priority))
.map(r -> p.listPrice().multiply(r.factor()))
.orElse(p.listPrice());
}
// retail, EU, books with the table below: factor 0.95, price 95.00
Part of a pricing rules table
Customer typeRegionCategoryPriorityFactor
RETAILEUBOOKS300.95
RETAILEUany201.00
RETAILanyBOOKS100.90
WHOLESALEanyBOOKS100.70
WHOLESALEAPACBOOKS200.75
Order no longer hides the logic: specificity is an explicit priority, and each row can be reviewed and tested.

5Choosing Between Them

The choice follows the shape of the change. If new kinds of customer appear and each brings different data, sealed types and a switch give the strongest checking. If rules multiply across combinations but are still written by developers, strategies keyed by the dimensions keep each rule small. If the business changes discounts every month, a rules table lets those changes ship without a deployment, but it needs validation, versioning and an audit trail, because a bad row is now a production change that skipped code review. Often the answer is a mix: sealed types for the kinds of customer, and a table for the discounts inside each kind.

Step throughPicking a structure for the pricing logic
  1. Pin today's behaviourcharacterisation tests over every combination
  2. Is the conditional stable and well tested?
    YesLeave it, perhaps tidy itNoFind the shape of the variation
  3. Is the variation a closed set of types?
    YesSealed interface, records, exhaustive switchNoNext question
  4. Do business users change the rules often?
    YesRules table with validation and tests per ruleNoStrategies in a map keyed by the dimensions

4 steps in order. Play them, or step through them one at a time.

6When a Long Conditional Is Fine

Not every long if-else chain is a problem. A conditional that has not changed in two years, is called from one place, and is fully covered by tests costs nothing to keep, and replacing it with strategies, a rules engine and a configuration format adds code, indirection and new ways to fail. The trigger for restructuring is the one in this question: frequent changes and repeated regressions. Without that pain, the honest answer is often to leave the code alone, or just to split it into a few well-named methods.

7Follow-up questions

How do you migrate the rules without a big bang?

Run the new structure in shadow: compute both prices in production, return the legacy one, and log every difference. When the differences are zero, or all explained and approved, switch to the new result behind a feature flag, and delete the legacy method once the flag has been on for a full pricing cycle.

What are the risks of a rules table?

Rules become data that can be changed without code review, so a bad row can misprice thousands of orders. We need validation when rules are saved, versioned rules with an effective date, an audit log of who changed what, and tests that run the full rule set before it goes live.

8Conclusion

In this answer, we saw that a growing if-else chain causes regressions because the order of branches hides the logic and every change has global effects. We first captured its behaviour with characterisation tests over every combination, then compared three structures: sealed types with an exhaustive switch for a closed set of kinds, strategies keyed by the dimensions, and a rules table for business-owned rules that change often, with a test per rule. We also saw that a stable, small, well-tested conditional is often best left alone.

What to say in the interviewAbout 90 seconds aloud

Open with

First I would make the change safe. Before touching the 300 lines, I would write characterisation tests that run every combination of customer type, region and category through the current code, which is only a few hundred cases, and assert the new code gives the same results.

Then, if they want more

Then I would look at the shape of the variation and model it explicitly instead of in branch order, which is what causes the regressions, since a new branch in the wrong place shadows an old one.

If the variation is a closed set of customer kinds with their own data, I would use a sealed interface with records and an exhaustive switch, so adding a kind becomes a compile error until it is priced. If behaviour varies by combinations, I would use strategy objects in a map keyed by those dimensions.

If the business changes discounts every month, I would move the rules into a table, in configuration or the database, with explicit priorities instead of order, validation and versioning, and a test per rule. Often it is a mix. I would migrate in shadow mode, comparing old and new prices in production before switching.

And I would say clearly that a long conditional is fine when it is stable, small in reach and well tested, because each abstraction has a cost.

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 pricing service has a 300-line if-else chain over customer type, region and product...": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/e70a57341c/