Agent Wranglers

ARC-36Recommended

Tell objects what to do

Give an object a command instead of reading its state and deciding for it. The rule that uses the data lives next to the data.

How to do it

When you see if (x.getStatus() === …) x.setStatus(…), move that decision into a method on x. The object then guards its own rules, and no caller can break them.

Examples

PHP example

// Bad: every caller must repeat the rule
if ($invoice->getStatus() === 'open' && $invoice->getDueDate() < $today) {
    $invoice->setStatus('overdue');
}

// Good: the invoice keeps its own rule
$invoice->markOverdueIfLate($today);

Python example

# Bad
if invoice.status == "open" and invoice.due_date < today:
    invoice.status = "overdue"

# Good
invoice.mark_overdue_if_late(today)

How to check

Reviewers fail code that reads an object's fields only to change the same object. Tests call the new method directly and cover every rule.

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