Skip to content
Katabench
Try free

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:

C#
book.Place(orderId, customerId);   // correct
book.Place(customerId, orderId);   // also compiles
book.CustomerOf(customerId);       // also compiles

Last 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.

Your job

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.

How it's graded

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.

SOLID principles exercises →

Related guides

What it practices

Value objects
Immutable types with value equality, including strongly typed identifiers.

Related katas