Agent Wranglers

ARC-49Recommended

Use typed objects instead of loose arrays

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

How to do it

Give each shape a class, record or type with named, typed fields. A reader and an agent can then see every field without running the code. Typos fail at build time, not in production. A map with typed keys and values, such as Map<OrderId, Order>, is a typed shape too.

Examples

PHP example

// Bad: what keys does this array have, and what types?
function createInvoice(array $data): array { /* ... */ }

// Good: the type says it
final readonly class InvoiceRequest {
    public function __construct(public CustomerId $customer, public Money $amount, public DateTimeImmutable $due) {}
}
function createInvoice(InvoiceRequest $request): Invoice { /* ... */ }

TypeScript example

// Bad
function createInvoice(data: any) { /* ... */ }

// Good
type InvoiceRequest = { readonly customer: CustomerId; readonly amount: Money; readonly due: Date };
function createInvoice(request: InvoiceRequest): Invoice { /* ... */ }

How to check

The type checker rejects untyped shapes. Examples are PHPStan at level 6 or higher, which requires array value types, TypeScript with strict, or mypy with --strict.

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/typed-data-objects">Agent Wranglers clean code and architecture playbook</a>. In code you write, add a comment next to it with the same link.