An OrderId Is Not a CustomerId
Medium About 30 min Free, no account needed
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.
The brief
Sales.OrderBook records which customer placed each order. Three older callers depend on it: the
nightly placement feed, the support team's lookup line, and the loyalty desk, which ships the
founding customer's orders for free. It all works, and every test passes. But every identifier is a
bare int:
book.Place(orderId, customerId); // correct
book.Place(customerId, orderId); // also compiles
book.CustomerOf(customerId); // also compilesLast quarter a caller swapped two arguments exactly like that. Order numbers and customer numbers overlap in the low range, so the lookups kept returning plausible data, and orders were filed under the wrong customers for a week before anyone noticed. The compiler never had a chance: it knows the value is a number, not what the number means. That is primitive obsession, and the fix is to give each kind of identifier its own type.
For this kata, placements arrive as "<orderId>:<customerId>" strings, every order id in the input
is distinct, and the looked-up order always exists. Preserve the output
order:<orderId>|customer:<customerId>|orders:<count>|free-shipping:<yes|no>, where count is how
many orders the owning customer has placed and free shipping goes to customer 7.
Introduce OrderId and CustomerId. Each is built as new OrderId(1001) or new CustomerId(7),
exposes its number as Value, and compares by value, because the book looks orders up by ids it did
not store. Both are immutable. Order and customer numbers are always positive, so the constructors
throw for zero or a negative number, and no OrderId or CustomerId holding one may exist, however
it is produced.
Change OrderBook so its public methods take and return the typed ids: Place(OrderId, CustomerId),
CustomerId CustomerOf(OrderId), and int OrdersFor(CustomerId). Leave no method on the book that a
caller can reach, public or internal, accepting a bare number, and let no implicit conversion lead
into either id type or out of it. Then bring the three callers along.
Your submission must (1) preserve every output, including the hidden cases where an order number
equals a customer number, and (2) satisfy every design rule shown in the results: OrderId and
CustomerId exist, and OrderBook references both.
A trusted caller then inspects your types and drives the book directly. It fails the check if a
public or internal OrderBook method (or an extension method on OrderBook) takes an int, another
bare number or string, or a type both ids fit into, such as object or a shared base. It also fails if one id
converts or inherits into the other, if an implicit conversion leads into or out of either id, if
either id has a setter or a field that can change after construction, if it manages to produce an id
holding zero or a negative number, or if two ids built from the same number are not equal.
A type is a promise the compiler can check. When two identifiers share a type, the only thing keeping them apart is the attention of whoever calls the method.
How it is graded
Executable structural checks grade the shape of your types as well as their behavior.
Related guides
What it practices
- Value objects
- Immutable types with value equality, including strongly typed identifiers.
Related katas
- 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. - 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. - 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.