Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

You want to make the pricing logic of an order service easier to test and reason about. How would you apply functional core, imperative shell?

I would move all pricing calculations into pure functions that receive everything they need, prices, rules, promotions and the current time, as arguments, and return an immutable result such as a PriceBreakdown record. Database reads, HTTP calls and clock reads stay in a thin outer layer, the imperative shell, which loads the inputs, calls the core and saves the output. The core is then covered by fast unit tests and property-based tests, and the shell by a few integration tests.

Why this question is asked

Pricing logic tends to grow into the hardest code in an order service to change. Every rule, discount and tax exception is mixed with repository calls and clock reads, so a test of one discount needs a database, a stubbed tax service and a fixed date. Interviewers give this scenario to see whether we can apply "functional core, imperative shell" to real code rather than describe it in general terms. In this answer, we will look at a typical tangled pricing method, explain the pattern, extract the pure core step by step, write the shell, see how testing changes, and note what to watch for.

1The Starting Point: Calculations Mixed With I/O

Here is a shortened version of pricing code as it often looks after a few years.

Java
@Service
class PricingService {
BigDecimal price(long orderId) {
Order order = orders.findById(orderId).orElseThrow(); // database
BigDecimal total = BigDecimal.ZERO;
for (OrderLine line : order.lines()) {
Product p = products.findById(line.sku()).orElseThrow(); // database, per line
total = total.add(p.price().multiply(BigDecimal.valueOf(line.qty())));
}
if (LocalDateTime.now().getDayOfWeek() == DayOfWeek.FRIDAY) { // clock
total = total.multiply(new BigDecimal("0.90"));
}
BigDecimal rate = taxClient.rateFor(order.country()); // HTTP
order.setTotal(total.multiply(BigDecimal.ONE.add(rate))); // mutation
orders.save(order); // database
return order.getTotal();
}
}

The arithmetic is only a few lines, but it is woven between five kinds of I/O. To test the Friday discount, we need a database or mocks for two repositories, a stubbed tax client, and a way to make it Friday. Each new rule makes this worse, and the method cannot be reused, for example to show a price preview without saving.

2The Idea: A Pure Core Inside a Thin Shell

Functional core, imperative shell splits a feature into two layers. The core contains the decisions and calculations, written as pure functions: they take all inputs as arguments, return results as values, and never read the clock, call a database or change anything. The shell surrounds it: it gathers inputs from the outside world, calls the core, and acts on the result by saving, sending or returning it. The shell contains as little logic as possible, ideally none worth unit testing.

Step throughFunctional core, imperative shell for pricing
  1. HTTP controller / message listenerreceives the request
  2. Imperative shell: PricingServiceloads order, products, tax rate; reads the clock; saves
  3. Functional core: Pricing.calculatepure: inputs in, PriceBreakdown out
  4. Immutable inputs and outputsrecords: PricingInput, PriceBreakdown

4 layers, from the top down. Step through them one at a time.

All I/O lives in one thin layer; everything below it is plain calculation.

3Extracting the Pure Core

First we define immutable inputs and outputs with records. Then the calculation becomes a static function whose only inputs are its parameters. Notice that the time is a parameter too, and that the result says how the price was built, which is useful for receipts and debugging.

Java
record PricedLine(String sku, int qty, BigDecimal unitPrice) {}
record PricingInput(List<PricedLine> lines, BigDecimal taxRate, LocalDateTime at) {}
record PriceBreakdown(BigDecimal subtotal, BigDecimal discount, BigDecimal tax, BigDecimal total) {}
final class Pricing {
static PriceBreakdown calculate(PricingInput in) {
BigDecimal subtotal = in.lines().stream()
.map(l -> l.unitPrice().multiply(BigDecimal.valueOf(l.qty())))
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal discount = in.at().getDayOfWeek() == DayOfWeek.FRIDAY
? subtotal.multiply(new BigDecimal("0.10")).setScale(2, RoundingMode.HALF_EVEN)
: BigDecimal.ZERO;
BigDecimal taxable = subtotal.subtract(discount);
BigDecimal tax = taxable.multiply(in.taxRate()).setScale(2, RoundingMode.HALF_EVEN);
return new PriceBreakdown(subtotal, discount, tax, taxable.add(tax));
}
}
Pricing.calculate(new PricingInput(
List.of(new PricedLine("A", 2, new BigDecimal("10.00"))),
new BigDecimal("0.20"),
LocalDateTime.of(2026, 11, 6, 12, 0))); // a Friday
// PriceBreakdown[subtotal=20.00, discount=2.00, tax=3.60, total=21.60]

The core is now a plain function of plain data. Notice that rounding is explicit: BigDecimal keeps the combined scale of each multiplication, so without setScale the discount would come out as 2.0000. In a pure function, a one-line test makes details like this visible immediately, instead of after an order has been saved with the wrong total.

4The Thin Shell

The service becomes a short sequence: gather inputs, calculate, act on the result.

Java
@Service
class PricingService {
PriceBreakdown price(long orderId) {
Order order = orders.findById(orderId).orElseThrow();
Map<String, Product> products = productRepo.findAllBySku(order.skus()); // one query
BigDecimal taxRate = taxClient.rateFor(order.country());
PricingInput input = PricingInputs.from(order, products, taxRate, LocalDateTime.now(clock));
PriceBreakdown result = Pricing.calculate(input); // pure core
orders.save(order.withTotal(result.total()));
return result;
}
}
Step throughOne pricing request
PricingService (shell)RepositoriesTax clientPricing (core)
load order and products
Order, Map<sku, Product>
rateFor(country)
0.20
read clock, build PricingInput
calculate(input)
PriceBreakdown
save order with total

8 messages in order. Play them, or step through them one at a time.

Notice that the shell has no branches about discounts or tax; all business decisions live in the core. As a side benefit, gathering inputs up front made it natural to load all products in one query instead of one per line. And a price preview is now just the same shell without the save call.

5How Testing Changes

The core can now be tested with ordinary unit tests that build a PricingInput and check the PriceBreakdown, with no Spring context, mocks or database. Each rule gets a few fast tests, and the Friday rule is tested by passing a Friday, not by changing the system clock.

Pure functions also suit property-based testing, where a library such as jqwik generates hundreds of random inputs and checks rules that must hold for all of them: the total is never negative, the discount never exceeds the subtotal, and the total equals subtotal minus discount plus tax. These tests often find rounding and edge-case bugs that hand-picked examples miss. The shell, meanwhile, is covered by a few integration tests that check it loads the right data and saves the result, since it contains no business logic to test in depth.

6What to Watch For

The main risk is the core quietly becoming impure again, for example someone injecting a repository into Pricing "just for one rule". I would keep the core in its own package with no Spring annotations and add an ArchUnit rule that it may not depend on repositories, clients or java.time.Clock.

The second risk is input size. If a rule needs data that is expensive to load, such as a customer's whole order history, the shell should load a summary, such as "orders in the last 30 days: 4", rather than everything. Designing those inputs is where most of the thinking goes, and it usually clarifies what each rule really depends on.

7Follow-up questions

Isn't passing the time as an argument awkward for callers?

Only the shell passes it, once per request, using an injected Clock. That one line replaces scattered LocalDateTime.now() calls inside the logic, and it makes every time-dependent rule testable by simply choosing the time.

What if pricing needs to call an external service halfway through, depending on earlier results?

Then the core can be split into two pure steps: the first returns a decision, such as "needs a fraud score", the shell fetches the score, and the second pure step finishes the calculation. The shell orchestrates; the core still decides.

How do you migrate existing code without a big rewrite?

One rule at a time. Extract the arithmetic of one rule into a pure function with characterization tests that capture current behaviour, call it from the existing method, and repeat. The shell shape emerges gradually, and each step is small enough to review.

8Conclusion

In this answer, we saw that functional core, imperative shell separates pricing into pure calculations and a thin layer of I/O. The core takes everything it needs, prices, rates, promotions and the time, as immutable records and returns an immutable PriceBreakdown; the shell loads data, reads the clock, calls the core and saves the result. The core is then covered by fast unit and property-based tests, the shell by a few integration tests, and an architecture rule keeps the core from drifting back into I/O.

What to say in the interviewAbout 90 seconds aloud

Open with

I'd split pricing into a functional core and an imperative shell. The core is pure functions: everything they need comes in as arguments, the order lines with prices, the tax rate, active promotions and the current time, ideally as immutable records like a PricingInput, and they return an immutable PriceBreakdown with subtotal, discount, tax and total.

Then, if they want more

They never touch the database, the HTTP tax client or the clock. The shell is the service method, and it becomes thin: load the order, load all products in one query, fetch the tax rate, read the time from an injected Clock, call Pricing.calculate, and save the total. It has no business branches. The payoff is mostly in testing.

Today, testing a Friday discount needs mocks for two repositories, a stubbed tax client and a fake date. With the core, it's a unit test that passes a Friday date and checks the result, no Spring context.

Because the core is pure, I can also use property-based tests with something like jqwik: the total is never negative, the discount never exceeds the subtotal. The shell then gets a few integration tests that check it loads and saves correctly.

To keep it that way, I'd put the core in its own package and add an ArchUnit rule that it can't depend on repositories, clients or Clock, and I'd migrate one rule at a time with characterization tests.

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 "You want to make the pricing logic of an order service easier to test and reason about....": https://scrivio-interview-library.netlify.app/java/functional-design/5b7aa7d0ed/