Agent Wranglers

ARC-56Recommended

Use enums instead of magic strings

Give every fixed set of values an enum or union type. Never compare against loose strings such as "paid".

How to do it

Define the set once, and use it in signatures, database mapping and tests. The type checker then catches typos and missing cases. At an API boundary, also follow use enums for fixed values, so clients survive values added later.

Examples

PHP example

// Bad: a typo compiles and fails quietly
if ($order->status === 'payed') { /* ... */ }

// Good: the set of values is a type
enum OrderStatus: string { case Pending = 'pending'; case Paid = 'paid'; case Refunded = 'refunded'; }
if ($order->status === OrderStatus::Paid) { /* ... */ }

Python example

from enum import StrEnum

class OrderStatus(StrEnum):
    PENDING = "pending"
    PAID = "paid"
    REFUNDED = "refunded"

if order.status is OrderStatus.PAID: ...

How to check

The tools flag unhandled cases. Examples are typescript-eslint switch-exhaustiveness-check and PHPStan's check that a match covers every enum case. Reviewers fail comparisons of status fields with string literals.

Sources

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