Make Failure a Result
Medium About 25 min Free, no account needed
EnrollmentService throws for an expected full-course decision, forcing every caller to catch normal control flow. Return a sealed immutable EnrollmentResult, and do not leave an unused result beside the old exception path.
The brief
Enrollment.EnrollmentService enrolls a requested number of students into a course. The behavior is
correct, but the design treats an ordinary business decision as a crash: when the course has too few
seats, Enroll throws CourseFullException. Solution must catch that exception just to produce the
normal rejection response. Every future caller inherits the same hidden two-path protocol.
For this kata, seatsAvailable and requestedSeats are non-negative integers. Preserve these exact
outcomes:
- When the request fits, return
enrolled:<requested>:remaining:<remaining>. - When the request exceeds capacity, return
rejected:<requested>:available:<available>. - An exact-capacity request succeeds, and requesting zero seats is a successful no-op.
A full course is expected. The caller asked a question and received "no". Exceptions are valuable for genuinely exceptional failures, but using one for a routine outcome hides part of the method's contract outside its return type.
The tempting half-fix is to add an EnrollmentResult class, perhaps with public setters, while
leaving EnrollmentService throwing and Solution catching. That creates a result-shaped ornament,
not result-based control flow. The service and caller still reference CourseFullException, and
neither depends on the new result, so the important rules stay red. Making the unused class mutable
also fails the value-shape rules.
Another partial move is to leave EnrollmentService throwing and catch it in a new outer helper
that returns a result. That only moves the exception boundary. The expected outcome must leave
EnrollmentService.Enroll as data, not as a throw that some caller is required to translate.
Replace exception-driven expected flow with one concrete, nongeneric EnrollmentResult returned by
EnrollmentService. The result must represent both acceptance and rejection, carry the seat counts
needed to produce the existing strings, and be sealed and immutable. Solution should inspect that
result and project exactly the same output as before. Once nothing references
CourseFullException, remove it. Seal EnrollmentService as well; it is one enrollment policy, not
a base class with an override-shaped escape hatch.
Your submission must (1) preserve every output, including exact capacity, zero, and the hidden one-seat boundary, and (2) satisfy every design rule shown in the results:
EnrollmentResultexists, is sealed and immutable, exposes no public setter, and has only readonly instance fields.EnrollmentServiceandSolutionboth depend onEnrollmentResultand neither referencesCourseFullException.EnrollmentServiceis sealed.
The grader also calls EnrollmentService.Enroll directly from a trusted caller, assigns the return
value to EnrollmentResult, and drives a rejected request without a catch block. A dead service-side
reference, an exception rename that still escapes Enroll, or an outer result wrapper around the old
throwing protocol cannot satisfy that seam.
An unused or mutable result can clear one visible symptom while leaving the old exception protocol intact. The design is complete only when the expected failure is part of the return value from the service all the way to the caller.
Expected failure is still an answer. Put it in the method's result so every caller can see the complete contract.
How it is graded
Executable structural checks grade the shape of your types as well as their behavior.
Related guides
What it practices
- Result types
- Return expected failures as values instead of throwing exceptions for control flow.
Related katas
- History Keeps a Checkpoint, Not Editor Internals Medium Free Capture immutable mementos that can be restored repeatedly while the history caretaker stays unaware of the editor’s stored representation.
- Four Layers for One Formula Medium Free A report-card service reaches its one weighted formula through a factory, a calculator contract, a base class, and a selector, each added so it could be swapped one day. Take out every layer nothing varies behind and keep the one abstraction that really varies, without changing a single report card.
- Freeze the Money Medium Free Money pretends to be a value object: its arithmetic returns new instances, but its Amount and Currency are still publicly settable over a mutable field. Make it structurally immutable, and don't let the reflex
private setfool you; it hides the setter but leaves the backing field writable. - An OrderId Is Not a CustomerId Medium Free OrderBook and its three callers identify orders and customers with bare ints, so a swapped argument compiles and quietly files an order under the wrong customer. Give each identifier its own value type that only ever holds a positive number, and bring the callers along without reopening the hole.