Data shapes
Pass data as named, typed objects that say what they hold. Parse input once at the edge, and never hide types in mixed values, loose arrays or strings.
- Required 2
- Recommended 11
- Avoid 1
Required
Parse input once at the boundary
ARC-53Turn outside input into a typed, valid object as soon as it arrives. Code after that point trusts the type and does not check again.
Stop integer arithmetic from overflowing
ARC-70Give every count, amount and size a type wide enough for its largest valid value. Arithmetic on values from outside fails with an error instead of wrapping around or losing digits.
Recommended
Use typed objects instead of loose arrays
ARC-49Pass data between functions as a named type, such as a DTO or value object. Never use
object, a plain dictionary or an untyped array that says nothing.Wrap domain values in their own types
ARC-50Give each domain value with its own rules a small type, such as
EmailAddress,OrderIdorMoney. Never pass it around as a bare string or number.Convert to arrays and JSON only at the edge
ARC-52Keep typed objects inside the app. Turn them into arrays, JSON or strings only where data leaves, such as an HTTP response or a database write.
Make data objects immutable
ARC-54Make DTOs and value objects read-only. Create a new object for a change instead of editing one that others may hold.
Model each state as its own type
ARC-55When an object moves through states, give each state its own type with only the fields it has. Never pair a status field with optional fields that some states leave empty.
Use enums instead of magic strings
ARC-56Give every fixed set of values an enum or union type. Never compare against loose strings such as
"paid".Give each function one return type
ARC-57A function or endpoint always returns the same shape. It never returns an object in one case, an array in another and
falsein a third.Insert text literally when you replace
ARC-58When you insert outside text with a replace function, make sure the text goes in exactly as written. Many replace functions treat parts of the replacement as commands.
Keep equality and hashing in step
ARC-71When a type defines its own equality, it defines a hash from the same fields. Two equal objects always have the same hash, and the fields in the hash never change.
Keep the time zone on every time
ARC-72Hold every past or present moment as a type that carries its zone or offset, normally UTC. Store a future local event as a local date and time plus a zone ID, and a date without a time as a date type.
Compare floating-point numbers with a tolerance
ARC-73Never compare two computed floating-point numbers with
==. Compare them within a stated tolerance, or use a decimal or integer type when the value must be exact.
Avoid
Typing values as mixed or any
ARC-51Never use
mixed,anyor an untyped value in signatures, properties or docblocks. Each one turns off the type checker for everything it touches.
Read this with
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/architecture/data-shapes">Agent Wranglers clean code and architecture playbook</a>. In code you write, add a comment next to it with the same link.