Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

A validation method is written as return checkA() || checkB() || checkC(); where each check also records metrics. Dashboards show checkC is almost never recorded. Is this a bug?

The || operator stops at the first operand that returns true, so checkB runs only when checkA returned false, and checkC only when both returned false. If checkA or checkB is usually true, checkC almost never runs, and neither do its metrics. The validation result is correct, so whether this is a bug depends on what the metrics are meant to show: if every check must record its result, the side effects do not belong inside a short-circuited expression, and we should evaluate all checks into variables first or separate the metrics from the decision.

Why this question is asked

Dashboards are how we learn what code does in production, so when a metric is nearly flat we usually suspect the metric. Here the metric is accurate: it faithfully reports that checkC hardly ever runs. Interviewers ask this because it tests whether we connect a familiar language rule, short-circuit evaluation, with a design problem, mixing side effects into a boolean expression. In this answer, we will recap how || evaluates its operands, reproduce the missing metrics, decide whether it is a bug, look at two fixes and what each changes, and finish with how the order of checks affects performance.

1A Quick Recap of Short-Circuit ||

a || b is true when at least one side is true. Java evaluates a first, and if a is true, the result is already known, so b is never evaluated. A chain a || b || c groups as (a || b) || c, so evaluation goes left to right and stops at the first operand that is true. Each later operand runs only if every operand before it returned false.

This skipping is guaranteed by the Java Language Specification. It is not an optimisation that might happen; code can rely on it, and in this case the metrics code is unknowingly depending on it.

2Reproducing the Missing Metrics

A small version of the method makes the effect visible. Each check returns its result and increments a counter.

Java
boolean checkA(Request r) { metrics.counter("check.a").increment(); return r.hasToken(); }
boolean checkB(Request r) { metrics.counter("check.b").increment(); return r.isInternal(); }
boolean checkC(Request r) { metrics.counter("check.c").increment(); return r.hasApiKey(); }
boolean isAllowed(Request r) { return checkA(r) || checkB(r) || checkC(r); }
// 1,000 requests: 950 have a token, 40 more are internal, the last 10 have only an API key
// check.a = 1000 check.b = 50 check.c = 10
Step throughEvaluating checkA(r) || checkB(r) || checkC(r)
  1. checkA true?
    YesReturn true; B and C never runNoRun checkB
  2. checkB true?
    YesReturn true; C never runsNoRun checkC
  3. Return checkC's resultonly now is check.c counted

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

Each metric counts how often its check was reached, not how often it would have passed.

Notice how the counts fall. checkA runs for every request. checkB runs only for the 50 requests without a token. checkC runs only for the 10 requests that failed both earlier checks. On a dashboard, check.c sits close to zero, which looks like a broken metric or an unused feature, while in fact it is doing exactly what the expression tells it to.

3Is It a Bug?

The decision itself is correct: a request is allowed if any of the three conditions holds, and short-circuiting gives the same answer as evaluating everything. The question is what the metrics were meant to say. If they are meant to show how often each check was evaluated, they are correct as they are, and the dashboard just needs a clearer label. If they are meant to show how often each condition is true across all requests, for example to decide whether API keys can be retired, the numbers are badly misleading: many of the 990 requests that were decided earlier may also carry an API key, say 300 of them, but the counter only ever sees the 10 where the key was the deciding factor.

Usually, metrics on a check are meant as the second kind. In that case the bug is a design one: side effects that must always happen have been placed in an expression whose operands may not run. That is fragile in a second way too, because a later refactoring that reorders the checks for performance would silently change every metric.

4Fix 1: Evaluate Every Check Into a Variable

If each check must run and record its result, we make that explicit by evaluating all of them before combining them.

Java
boolean isAllowed(Request r) {
boolean a = checkA(r); // always runs
boolean b = checkB(r); // always runs
boolean c = checkC(r); // always runs
return a || b || c; // combines values; no calls left to skip
}
// 1,000 requests: check.a = 1000 check.b = 1000 check.c = 1000

Now every counter counts every request, and the expression at the end has no side effects, so its order no longer matters. The cost is that every check always runs, which matters if a check is expensive, for example a remote call to an API key service. That leads to the second fix.

5Fix 2: Separate the Metrics From the Decision

The cleaner design is for each check to be a pure function that only returns a result, and for the caller to record metrics about results explicitly. Then the decision can keep short-circuiting, and the metrics describe exactly what we decide they should describe.

Java
boolean hasToken(Request r) { return r.hasToken(); } // pure: no metrics inside
boolean isInternal(Request r) { return r.isInternal(); }
boolean hasApiKey(Request r) { return r.hasApiKey(); }
boolean isAllowed(Request r) {
String reason = hasToken(r) ? "token" : isInternal(r) ? "internal" : hasApiKey(r) ? "api_key" : "denied";
metrics.counter("auth.decision", "reason", reason).increment(); // one metric, tagged
return !reason.equals("denied");
}
// 1,000 requests: reason=token 950, internal 40, api_key 10, denied 0

Here one tagged counter records which condition decided each request, which is usually the question the dashboard is really asking. If we also need to know how many requests carry an API key at all, we add a separate cheap metric for that property, or sample a fraction of requests and evaluate every check for them. Either way, the meaning of each number is now chosen deliberately instead of falling out of operator semantics.

6Ordering Checks for Performance

Short-circuiting is also a performance tool, and the order of the operands is where we use it. For ||, the expression can stop at the first true, so we put checks that are cheap and likely to be true first; for &&, which stops at the first false, cheap checks that are likely to be false go first. In the example, the token check is an in-memory lookup that decides 95 percent of requests, so placing it first means the expensive API key lookup, perhaps a database or cache call, runs for only 1 percent of traffic.

This is exactly why side effects inside such expressions are risky. Someone who reorders the operands to save latency is making a correct performance change to the decision, and should not have to worry that it changes every metric on the dashboard.

7Follow-up questions

Would replacing || with | fix the metrics?

It would make every check run, so the counters would be complete, but it hides the intent: the next reader may think | is a typo and restore ||. Separate statements, as in the first fix, say clearly that every check must run.

How would you have caught this earlier?

A unit test that asserts metric values for a request decided by the first check would show check.c not being incremented. More generally, code review should question any method that both returns a decision and records a side effect, if it is called inside a boolean expression.

Does the same apply to logging?

Yes. Log statements inside checks combined with && or || only appear when that operand runs, which produces confusing logs that seem to skip steps. The same separation, pure checks and explicit logging by the caller, solves it.

8Conclusion

In this answer, we saw that checkA() || checkB() || checkC() stops at the first true operand, so checkC and its metrics run only when the first two fail, which is why the dashboard shows it nearly flat. The decision is correct; the design is the problem when metrics are meant to describe every request. We fixed it either by evaluating all checks into variables before combining them, or better by keeping checks pure and recording metrics explicitly in the caller. We also saw that ordering cheap, likely-decisive checks first is a valid optimisation, which is one more reason to keep side effects out of short-circuited expressions.

What to say in the interviewAbout 100 seconds aloud

Open with

The validation itself is not buggy, but the metrics are misleading, and that is a design bug. The double pipe operator short-circuits: Java evaluates checkA first, and if it returns true, the result is known and checkB and checkC never run. checkC only runs when both earlier checks returned false.

Then, if they want more

So if most requests pass checkA or checkB, checkC's counter barely moves, because it counts how often the check was reached, not how often its condition is true. Whether that matters depends on what the dashboard is for. If people use it to decide, say, whether API keys are still used, it is giving them the wrong answer.

The root cause is putting side effects that must always happen into an expression whose operands may be skipped. There are two fixes. If every check must run, evaluate each into a local variable first, then combine the variables, so the final expression has no side effects. That costs running every check every time.

The cleaner fix is to make the checks pure and move metrics into the caller, for example one counter tagged with the reason that decided the request, plus a separate metric for any property we want counted across all requests.

That keeps the short-circuit, which is also a performance tool: putting cheap, likely-true checks first means an expensive lookup in checkC runs for only a small share of traffic, and with pure checks, reordering them for speed no longer changes the dashboards.

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 validation method is written as return checkA() || checkB() || checkC(); where each...": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/e8c78eb12b/