The decision
A seat-booking widget must distinguish a request for zero seats from a request exceeding stock. The former needs a new count, the latter should display what remains. A negative inventory supplied by the caller is a programming error.
Read the Kotlin
Walk the booking branches in order
After the input invariant passes, a nonpositive request is invalid. A request above availability returns a shortage carrying the observed inventory. All remaining requests succeed with the number left over. Equality belongs in the success branch: the final seats may be booked, leaving a perfectly valid balance of zero.
book(available = 4, requested = 4) // Confirmed(0)
book(available = 4, requested = 5) // Shortage(4)Make the caller’s next action possible
A customer with an oversized request can choose a smaller quantity if the refusal carries the number available. An invalid request needs a different message. Returning null for both loses the reason as well as the available count. A sealed result gives the client a named branch and precisely the payload for that action.
Check caller invariants before domain refusals
require(condition) throws IllegalArgumentException when the condition is false and execution leaves the function there. Negative inventory violates the upstream contract. Even if the requested quantity also happens to be invalid, the later when never runs. Moving the guard after a domain return could hide a broken stock feed.
The full example
class Lesson {
sealed interface Booking permits Confirmed, Shortage, InvalidCount {}
record Confirmed(int remaining) implements Booking {}
record Shortage(int available) implements Booking {}
record InvalidCount() implements Booking {}
static Booking book(int available, int requested) {
if (available < 0) throw new IllegalArgumentException("Negative inventory");
if (requested < 1) return new InvalidCount();
if (requested > available) return new Shortage(available);
return new Confirmed(available - requested);
}
}sealed interface Booking {
data class Confirmed(val remaining: Int) : Booking
data class Shortage(val available: Int) : Booking
data object InvalidCount : Booking
}
fun book(available: Int, requested: Int): Booking {
require(available >= 0) { "Negative inventory" }
return when {
requested < 1 -> Booking.InvalidCount
requested > available -> Booking.Shortage(available)
else -> Booking.Confirmed(available - requested)
}
}Inputs and outcomes
| Input | Outcome | Why it matters |
|---|---|---|
6 seats available, 2 requested | Confirmed with 4 remaining | Return the balance after accepting the request. |
2 seats available, 3 requested | Shortage with 2 available | The UI can offer a smaller booking without a second lookup. |
0 available, 0 requested | InvalidCount | Validate a positive request before considering availability. |
Walk through the code
- require checks a caller invariant before any domain decision. A negative inventory throws IllegalArgumentException in both languages.
- The two ordinary refusals are values. One carries the observed availability. Consumers can branch on these outcomes without parsing exception text.
- Booking.Confirmed(0) is a success. A nullable number could represent that, but a single null could not explain which of the two refusals occurred.
The Java habit
Wrapping every outcome in runCatching would turn an invalid internal inventory into an ordinary failure value. It would also leave callers discriminating Throwables unless you explicitly model the refusals.
When to choose differently
For a lookup with exactly one expected absence and no action-specific reason, a nullable result is smaller. Introduce a hierarchy when callers really handle distinct outcomes.