Skip to content
Katabench
Try free

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 trap

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.

Your job

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.

How it's graded

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:

  • EnrollmentResult exists, is sealed and immutable, exposes no public setter, and has only readonly instance fields.
  • EnrollmentService and Solution both depend on EnrollmentResult and neither references CourseFullException.
  • EnrollmentService is 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.

SOLID principles exercises →

Related guides

What it practices

Result types
Return expected failures as values instead of throwing exceptions for control flow.

Related katas