Own the Lease Until the Work Ends
Medium About 20 min Free, no account needed
Keep acquisition and deterministic disposal in one scope, including exception paths, while accepting any lease factory.
The brief
LeaseRunner acquires a work lease and processes one value, but never closes the lease. Successful output is correct, so a string comparison alone cannot reveal the leaked ownership. Closing only after the call would still leak when processing throws.
Introduce IWorkLease : IDisposable with string Apply(int value), ILeaseFactory with IWorkLease Acquire(), and a LocalLeaseFactory whose Acquire returns a new LocalLease. LeaseRunner must accept a factory in its constructor, acquire exactly one fresh lease for each Run, call its Apply exactly once, and dispose that lease exactly once before returning or propagating a processing exception. The runner owns leases it acquires; it does not dispose the factory. Do not cache a lease across invocations.
Keep LeaseRunner sealed, hold its factory readonly, and remove its dependency on LocalLease without adding one on LocalLeaseFactory. The root (Solution) passes a LocalLeaseFactory to the runner. LocalLease implements the lease contract. Preserve the behavior of Solution.Run(int value): nonnegative values produce handled:<value>; negative values produce failed:negative from the root's existing exception handler.
The trusted seam supplies counting leases that succeed or throw. It invokes the same runner twice and checks acquisition, processing, and disposal counts, so constructor injection without correct ownership remains incomplete.
How it is graded
Executable structural checks grade the shape of your types as well as their behavior.
What it practices
- Resource lifetime
- Acquire and dispose deterministically, including on exception paths.
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.