Agent Wranglers

ARC-30Recommended

Make fields required unless absence is valid

Every constructor argument and data object field is required and non-nullable, unless "not there" is a valid state of the business.

How to do it

Ask for each optional field what it means when it is empty. If it is always set in practice, make it required. If absence has a meaning, such as "not shipped yet", name that state with its own type or an optional field. Build objects fully in the constructor, never half-filled with setters. Make the same fields NOT NULL in the database, as Enforce rules with constraints says.

Examples

PHP example

// Bad: everything is nullable, so nothing can be trusted
final class Shipment
{
    public function __construct(
        public ?string $orderId = null,
        public ?string $address = null,
        public ?DateTimeImmutable $shippedAt = null,
    ) {}
}

// Good: only shippedAt may be missing, because "not shipped yet" is a valid state
final class Shipment
{
    public function __construct(
        public readonly OrderId $orderId,
        public readonly Address $address,
        public readonly ?DateTimeImmutable $shippedAt,
    ) {}
}

TypeScript example

// Good: only shippedAt may be missing
class Shipment {
  constructor(
    readonly orderId: OrderId,
    readonly address: Address,
    readonly shippedAt: Date | undefined,
  ) {}
}

How to check

A type checker rejects missing fields, such as TypeScript strict, PHPStan level max or mypy --strict. Reviewers fail any nullable field that no code path ever leaves empty.

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