Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

A shared validation library evaluates many predicate rules combined with && and ||. Product teams want rule ordering to be configurable. What design concerns do you raise?

With short-circuit && and ||, the order of rules changes only which rules run, not the result, but that is true only if every rule is pure: no side effects, no exceptions, and safe on any input including null. So before allowing configurable ordering I would require rules to be side-effect free and null-safe, treat order as a cost decision with per-rule metrics, offer a separate evaluate-all mode for teams that need every failure reported, and document these semantics in the library's API rather than leaving them in the code.

Why this question is asked

A shared validation library is a small platform: many teams depend on its behaviour, often without reading its code. Making rule order configurable sounds like a harmless feature, but it turns an implementation detail, the order in which && and || evaluate, into a public behaviour, and every rule that is not pure makes that order observable. Interviewers ask this design question to see whether we can see the hidden contract in a simple feature request and design the library so the feature is safe. In this answer, we will recap why order is invisible for pure predicates, see the three ways impure rules make it visible, set the requirements for rules, treat ordering as a performance decision with data, add an evaluate-all mode, and write the semantics into the API.

1A Quick Recap: When Does Order Not Matter?

A predicate is a function that returns true or false. It is pure if it depends only on its input and does nothing else: no logging, no metrics, no database writes, no changes to the input, and no exceptions for valid input. For pure predicates, a && b and b && a always give the same result, because logical and is commutative. Short-circuiting changes only how much work is done, never the answer.

So the product teams' request is safe in a world of pure rules. The design question is how to make the real rule set behave like that world, and what to do for the cases where it cannot.

2How Impure Rules Make Order Observable

There are three ways a rule can make order visible. The first is side effects: a rule that increments a metric, writes an audit record or caches a lookup runs or does not run depending on its position, as we saw with short-circuited metrics. The second is exceptions: a rule that throws on some input aborts the whole evaluation, but only if it is reached, so moving it earlier turns a rejected request into an error. The third, and most common, is hidden dependencies between rules: a rule that calls order.getCustomer().isActive() silently relies on an earlier rule having checked that the customer is not null.

Java
Predicate<Order> hasCustomer = o -> o.customer() != null;
Predicate<Order> customerActive = o -> o.customer().isActive(); // assumes hasCustomer ran first
Order o = new Order(null, List.of());
hasCustomer.and(customerActive).test(o); // false: second rule skipped
customerActive.and(hasCustomer).test(o); // NullPointerException: order now matters
What reordering can change
Rule propertyResult changes?Observable effect of reordering
Pure and null-safenoonly cost changes
Records metrics or logsnometrics and logs change
Throws on some inputsyesvalidation error becomes an exception
Assumes an earlier rule passedyesNullPointerException or wrong answer
Changes the inputyeslater rules see different data
Only the first row makes configurable ordering safe.

Notice that each rule looks fine on its own, and the default order works. A team that reorders them in configuration, perhaps to put a rule they consider important first, turns invalid input into a NullPointerException and a 500 error. Nothing in the configuration file warned them.

3Require Rules to Be Pure and Null-Safe

The first design decision is a rule contract: every rule must be side-effect free, must not modify its input, and must handle missing data itself by returning a result, not by throwing. A rule about customer activity therefore checks for the customer too, or declares the dependency explicitly so the library can enforce it.

Java
public interface Rule<T> {
String id(); // stable name, used in config and metrics
RuleResult check(T input); // must be pure; must not throw for any input, including nulls
default Set<String> requires() { return Set.of(); } // ids of rules that must pass first
}
Rule<Order> customerActive = new Rule<>() {
public String id() { return "customer-active"; }
public RuleResult check(Order o) {
return (o.customer() != null && o.customer().isActive())
? RuleResult.pass(id()) : RuleResult.fail(id(), "customer is missing or inactive");
}
};

The library can then validate a configured order: if a team puts customer-active before a rule it requires, loading the configuration fails at startup with a clear message, rather than at runtime with an exception. The library itself catches any exception a rule throws and converts it into a failed result tagged with the rule id, so a buggy rule cannot turn validation into an outage, and the defect shows up in metrics. Purity is enforced by review and by tests in the library's test kit, which run each rule against null fields and empty collections and check that nothing is thrown.

4Order Is a Cost Decision: Measure It

Once rules are pure, order only affects cost, and the right order follows a known principle: for a chain of && rules, put rules that are cheap and likely to fail first, because the first failure ends the evaluation; for || chains, put cheap rules that are likely to pass first. A rule that checks a field for blankness costs nanoseconds; a rule that calls a fraud service costs milliseconds and should run last, so it runs only for orders that passed everything else.

Teams can only choose well with data, so the library records metrics per rule: how often it was evaluated, how often it failed, and how long it took, tagged with the rule id. Notice that these metrics live in the library's evaluation loop, not inside the rules, so they stay accurate whatever the order. A team can then see that the fraud check runs for 99 percent of orders because the cheap rules that would reject them are placed after it, and fix the configuration.

Step throughLibrary evaluation loop in short-circuit mode
  1. Load configured ordercheck requires() dependencies at startup
  2. Take the next rulestart a timer
  3. Run check(input)exceptions become failed results
  4. Record metricsevaluated, failed, duration per rule id
  5. Rule failed?
    YesStop and return the failureNoContinue with the next rule

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

Metrics live in the loop, so they stay correct whatever order a team configures.

5Offer an Evaluate-All Mode

Short-circuiting reports only the first failure, which is right for a fast accept or reject decision but wrong for a form where a user wants to see every problem at once, or for a batch import report. So the library provides a second mode that evaluates every rule and collects all results. It costs more, since every rule runs, but with pure rules the results are the same set regardless of order, which also makes it a useful test oracle: in tests, the first failure from short-circuit mode must always be one of the failures from evaluate-all mode.

The mode is chosen explicitly by the caller, for example validator.firstFailure(order) or validator.allFailures(order), rather than by configuration, so a reader of the calling code can see which semantics it gets.

6Document the Semantics in the API

Finally, the contract goes where users will see it: the Javadoc of Rule, the validator methods and the configuration schema. It states that rules must be pure, null-safe and non-throwing, that the library converts exceptions into failures, that order affects only cost and which rule is reported first in short-circuit mode, that dependencies must be declared through requires(), and that metrics are recorded per rule by the library. A short page in the library's guide explains how to choose an order using the metrics, with an example. Without this, the semantics live only in the implementation, and every team rediscovers them through an incident.

7Follow-up questions

Why not just forbid configurable ordering?

Because the teams have a real need: their rule costs and failure rates differ, and a fixed order wastes expensive calls. With a pure-rule contract, dependency checks and metrics, ordering becomes a safe tuning knob. Forbidding it would push teams to fork the library or wrap rules in their own combinators, which is worse.

What if a rule genuinely needs a side effect, such as caching a lookup?

Caching that does not change results is acceptable if it is idempotent and thread-safe, because running the rule twice or not at all gives the same answers. Anything that records business events, such as audit entries, belongs outside the rule, in the caller, after the decision is made.

How do you migrate existing impure rules?

Run the test kit against every existing rule to find ones that throw on nulls or depend on others, fix those or declare their dependencies, move metrics into the library loop, and only then enable configurable ordering, per team, behind a flag.

8Conclusion

In this answer, we saw that short-circuit evaluation makes rule order invisible only for pure, null-safe predicates. Side effects, exceptions, hidden dependencies and mutations make order observable, so configurable ordering needs a rule contract: pure, non-throwing, null-safe rules with declared dependencies checked at startup. Order then becomes a cost decision, guided by per-rule metrics recorded in the library loop. An explicit evaluate-all mode serves teams that need every failure, and all of these semantics are documented in the library's API.

What to say in the interviewAbout 100 seconds aloud

Open with

My main concern is that configurable ordering turns evaluation order into a public behaviour. With && and ||, order does not change the result only if every rule is pure: no side effects, no exceptions, safe for any input.

Then, if they want more

Real rules often break that. A rule that records metrics runs or not depending on position; a rule that throws on some input turns a rejection into a 500 when moved earlier; and the most common case, a rule like customer is active silently assumes an earlier rule checked the customer is not null.

So first I would set a rule contract: rules are side-effect free, do not modify input, handle nulls themselves, and declare any dependency on other rules, which the library checks when it loads the configuration, so a bad order fails at startup instead of in production.

The library also catches exceptions and turns them into failed results with the rule id. Second, I would frame ordering as a cost decision: cheap, likely-to-fail rules first in an and-chain, expensive remote checks last, and the library records per-rule metrics, evaluated, failed and duration, in its own loop rather than inside rules, so teams can tune with data.

Third, for teams that need all failures, like a form, I would add an explicit evaluate-all mode chosen in code, not config. And finally, all of this goes into the Javadoc and the configuration schema, because otherwise each team learns the semantics through an incident.

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 shared validation library evaluates many predicate rules combined with && and ||....": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/8881b03af0/