A Preview Must Not Consume Work
Easy About 15 min Free, no account needed
Give queue observers a read-only capability and keep consumption behind an explicit command; repeated previews must leave the next item unchanged.
The brief
A queue preview should answer what comes next without doing the work. The current QueuePreview reaches into WorkQueue.Items, and the composition root removes items through the same public list. Any preview client can accidentally consume or rewrite the queue because observation and mutation share one exposed representation.
Refactor the types in Work while preserving Run(int[] items) output. Inputs contain nonnegative work ids. The result is before:<next>|after:<next> around one consumption, with empty when no item exists. Consumption on an empty queue is a no-op. Never mutate the input array.
Introduce IQueueView with int? Peek(). WorkQueue implements that read-only capability and separately exposes void Consume() and a get-only int Count. QueuePreview.Render accepts only IQueueView; it must neither depend on the concrete queue nor gain access to its backing collection. Keep the queue sealed and expose no mutable collection or public setter. The root owns consumption and creates the queue.
The distinction is observable: repeated Peek and Render calls must return the same item until Consume is explicitly called. A trusted caller previews twice, consumes once, and previews again. An interface with a consuming Peek does not satisfy the contract.
How it is graded
Executable structural checks grade the shape of your types as well as their behavior.
Related guides
What it practices
- Command-query separation
- Methods either change state or answer a question, never both.
- Encapsulation
- Protect invariants: no exposed mutable collections, shared static state or train-wreck navigation.
Related katas
- Stop the Train Wreck Easy Free A label printer reaches order.Buyer.HomeAddress.City to print one line, coupling itself to three classes for one string. Ask the order directly and talk to one neighbor, not a chain of strangers. Shortening the chain with a local variable does not shorten the coupling.
- 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.
- Kill the Global Counter Medium Free
Counterkeeps its running total in apublic staticfield every caller shares, and it's an open, inheritable class on top. Give the state one explicit owner instead of just hiding the field, and don't let the reflexprivate staticfix trick you into leaving the second smell standing. - Make It a Value Object Hard Free A Reservation is a mutable two-phase DTO that also hands out its backing guest list. Make it a real value object, frozen at construction, and don't let the obvious
private setfix fool you into leaving the collection leak wide open.