Skip to content

Lesson 7 of 45 · Principles of Good Object Design

Tell, Don't Ask

Intermediate•
lesson•
October 12, 2026•
~9 min read

The pattern that's everywhere

In the codebases I've worked on, service classes often contain some version of this:

PHP
<?php declare(strict_types=1); namespace App\Reservations; use App\Reservations\Mail\ReservationCancelledEmail; use Illuminate\Contracts\Mail\Mailer; use Psr\Log\LoggerInterface; final class CancelReservationService { public function __construct( private Mailer $mailer, private LoggerInterface $logger, ) {} public function cancel(Reservation $reservation): void { if ($reservation->status === 'confirmed' && $reservation->check_in > now()) { $reservation->status = 'cancelled'; $reservation->cancelled_at = now(); $reservation->save(); $this->mailer->send(new ReservationCancelledEmail($reservation)); $this->logger->info('reservation.cancelled', ['id' => $reservation->id]); } } }

Innocent on its own, but repeated in slight variations wherever the codebase needs to know whether a reservation can be cancelled and what to do about it.

The pattern is: ask the object for its state, branch on it, and act based on what you found.

The pattern's quiet cost is that the rule "what does it mean to cancel a reservation?" is now scattered across every place that asks. Change the rule (require the cancellation policy to allow it, block cancellations within 48 hours of check-in, let pending reservations cancel too) and you have to find every place that asked the question and update it. Miss one, and a reservation that should cancel doesn't, or one that shouldn't does.

The fix is an object-oriented design principle called tell, don't ask.

What it actually means

The principle is most often associated with Andy Hunt and Dave Thomas (the Pragmatic Programmers), as Martin Fowler's bliki entry on it notes. In one sentence, paraphrased:

Don't ask an object for data and act on it. Tell the object what to do, and let it decide.

You don't pull state out of an object so the calling code can apply rules to it. You hand the responsibility to the object that already owns the state, and it does the work in one place.

Fowler's entry gives the reason behind it:

object-orientation is about bundling data with the functions that operate on that data

If a piece of code keeps reaching into one object's internals to make decisions, that's a sign the decision lives in the wrong place. Move the decision inside the object. The calling code shrinks. The object grows the responsibility it always implicitly had.

A small worked example

The "cancel reservation" code from above, refactored. The Reservation here is no longer the Eloquent model from the first listing; it is a plain domain class (the Repository Pattern lesson shows how one gets persisted). The service now depends on the Reservations repository, an event dispatcher and a clock. The cancellation email and the log line that the first version handled inline are not part of this class anymore; a listener on ReservationCancelled takes them over. ReservationCancelled is an event that carries the reservation id, and it is not shown:

PHP
<?php declare(strict_types=1); namespace App\Reservations; use Illuminate\Contracts\Events\Dispatcher; use Psr\Clock\ClockInterface; final class CancelReservationService { public function __construct( private Reservations $reservations, private Dispatcher $events, private ClockInterface $clock, ) {} public function cancel(Reservation $reservation, string $reason): void { $now = $this->clock->now(); if ($reservation->canCancel($now)) { $reservation->cancel($reason, $now); $this->reservations->save($reservation); $this->events->dispatch(new ReservationCancelled($reservation->id)); } } }

With Reservation looking something like this (trimmed to what this lesson needs; the CannotCancelReservation exception is not shown):

PHP
<?php declare(strict_types=1); namespace App\Reservations; use App\Reservations\Exceptions\CannotCancelReservation; use DateTimeImmutable; final class Reservation { public function __construct( public readonly ReservationId $id, private ReservationStatus $status, private DateRange $stayDates, private ?DateTimeImmutable $cancelledAt = null, private ?string $cancellationReason = null, ) {} public function canCancel(DateTimeImmutable $now): bool { return $this->status === ReservationStatus::Confirmed && $this->stayDates->checkIn > $now; } public function cancel(string $reason, DateTimeImmutable $now): void { if (!$this->canCancel($now)) { throw new CannotCancelReservation($this->id); } $this->status = ReservationStatus::Cancelled; $this->cancelledAt = $now; $this->cancellationReason = $reason; } }

The two lines that ask "is this thing in this state?" and "transition it to that state" now live on Reservation. The service is left with the orchestration: check, do, persist, announce. It still asks one question, canCancel(), but the answer is a decision the reservation made, not raw state the service has to interpret.

The rule "reservations can be cancelled before check-in while still confirmed" now lives in one place. Change it once, and every caller, whether a controller, a console command, or a queued job, picks up the new rule for free.

Why this matters

Three concrete things you get out of telling instead of asking:

  • Encapsulation actually means something. When the only way to change the reservation's state is through methods on the reservation, the reservation can enforce its own invariants. It can't be left in an impossible state, because there's no path to an impossible state.

  • Behavior is locatable. "Where does the cancellation rule live?" has one answer. The reader doesn't have to read every service to find every place the rule was applied. The class that owns the data owns the rule.

  • Refactors get cheaper. Business logic that lives with the business, not scattered across the framework, is one of the things that keep the cost-of-change curve flat. Tell-don't-ask is a practical way to get there, because it keeps behavior local.

This connects directly to cohesion. High cohesion means a class's contents change for the same reason; tell-don't-ask is what high cohesion looks like at the method level. The data and the operations on the data live together, because they change together.

Asking is fine when you're just asking

To stay honest with the "tools, not rules" framing: not every getX() is an anti-pattern. Reading a value to display it, log it, or pass it along is fine. The principle bites when the calling code reads a value to make a decision the object should have made. Fowler's entry says the same: "there are times when objects collaborate effectively by providing information."

A useful test: after you read the value, what do you do with it?

  • "I'm formatting it for an email" → asking is fine.

  • "I'm including it in a JSON response" → asking is fine.

  • "I'm using it to decide whether to take an action" → that's the moment the decision wants to move into the object.

Value objects are also fine to read directly. $money->amountInCents and $email->domain() are queries by design. The object exists to be read; the rules around it (validation, immutability) are already enforced by its construction.

Command-query separation

A useful companion principle is Bertrand Meyer's command-query separation (CQS), paraphrased:

A method should either change state or return a value, not both.

Methods like popLastItem() that mutate the structure and return the popped value mix the two. A caller who only wanted the value changes the state as a side effect. Meyer applies the rule strictly. Fowler's entry on CQS is looser: he calls popping a stack "a good example of a query that modifies state" and is "prepared to break it to get my pop." Reservation::expire() later in this lesson breaks the rule the same way, on purpose: it changes state and returns the payment the caller must refund.

Tell-don't-ask plus CQS gives you a clean shape:

  • Commands mutate state, return nothing.

  • Queries return state, mutate nothing.

Reservation::canCancel() is a query. Reservation::cancel() is a command. They're separate methods.

PHP doesn't enforce this, so the team has to. The discipline pays back when somebody reads $reservation->something() and knows from the name whether the world is about to change.

Pitfalls

Fluent setters are not tell-don't-ask. $reservation->setStatus(...)->setCancelledAt(...)->setUpdatedAt(...) is the same thing as the asking pattern, just dressed up. The chain is still the calling code orchestrating the rule. A single method that means something (cancel) is the right shape.

Vague method names hide the same scattered logic. $reservation->process() that secretly accepts six string flags is a fat method, and the decision is still scattered across the flags. The decision has to move into the object under a name that says what it does.

Not every method has to live on the entity. Some operations don't belong on the data class. A Mailer knows how to send. A Repository knows how to persist. A ReservationPricer knows how to combine inputs into a price. Those are services, and they're allowed to take values, transform them, and return results. Tell-don't-ask applies to the data objects whose state you'd otherwise be reading and branching on. It doesn't ban services or pure functions.

A common refactor

Here's a pattern you'll see in long-lived codebases. The "before", where Reservation is still the Eloquent model:

PHP
<?php declare(strict_types=1); namespace App\Reservations; use App\Billing\Currency; use App\Billing\Money; use App\Payments\PaymentId; use App\Payments\Payments; final class ExpireReservationService { public function __construct( private Reservations $reservations, private Payments $payments, ) {} public function expire(ReservationId $reservationId): void { $reservation = $this->reservations->withId($reservationId); if ($reservation->status === 'pending' && $reservation->expires_at < now()) { if ($reservation->payment_id !== null) { $this->payments->refund( new PaymentId($reservation->payment_id), Money::fromCents($reservation->price_cents, new Currency($reservation->currency)), ); } $reservation->status = 'cancelled'; $reservation->cancelled_at = now(); $reservation->cancellation_reason = 'expired'; } $this->reservations->save($reservation); } }

The same operation, with tell-don't-ask. In this example a pending reservation only has a payment_id once the guest has paid a deposit equal to the quoted price, so expiry gives that money back with refund. The ReservationExpired event and the CannotExpireReservation exception are not shown. First the reason:

PHP
<?php declare(strict_types=1); namespace App\Reservations; enum ExpirationReason: string { case Timeout = 'timeout'; }

Then the reservation, again trimmed to what this section needs:

PHP
<?php declare(strict_types=1); namespace App\Reservations; use App\Billing\Money; use App\Payments\PaymentId; use App\Reservations\Exceptions\CannotExpireReservation; use DateTimeImmutable; final class Reservation { public function __construct( public readonly ReservationId $id, public readonly Money $quotedPrice, private ReservationStatus $status, private DateTimeImmutable $expiresAt, private ?PaymentId $paymentId = null, private ?DateTimeImmutable $cancelledAt = null, private ?ExpirationReason $expirationReason = null, ) {} public function canExpire(DateTimeImmutable $now): bool { return $this->status === ReservationStatus::Pending && $this->expiresAt < $now; } /** Returns the payment the caller must refund, if there is one. */ public function expire(ExpirationReason $reason, DateTimeImmutable $now): ?PaymentId { if (!$this->canExpire($now)) { throw new CannotExpireReservation($this->id); } $this->status = ReservationStatus::Cancelled; $this->cancelledAt = $now; $this->expirationReason = $reason; return $this->paymentId; } }

Then the service:

PHP
<?php declare(strict_types=1); namespace App\Reservations; use App\Payments\Payments; use Illuminate\Contracts\Events\Dispatcher; use Psr\Clock\ClockInterface; final class ExpireReservationService { public function __construct( private Reservations $reservations, private Payments $payments, private Dispatcher $events, private ClockInterface $clock, ) {} public function expire(ReservationId $reservationId): void { $reservation = $this->reservations->withId($reservationId); $now = $this->clock->now(); if ($reservation->canExpire($now)) { $paymentId = $reservation->expire(ExpirationReason::Timeout, $now); if ($paymentId) { $this->payments->refund($paymentId, $reservation->quotedPrice); } $this->events->dispatch(new ReservationExpired($reservation->id)); } $this->reservations->save($reservation); } }

The reservation owns the logic of when it can expire and what expiring means to its own state. It also tells the service which payment is left over, so the service never reaches into the reservation to find the payment id. The service still owns the side effects that touch other systems (payments, events, persistence), because those don't belong on the reservation.

Where this pushes you

Once tell-don't-ask becomes a habit, several other things in this course start to feel natural.

  • Anemic models stop fitting. The Anemic Domain Model lesson will go deeper on this, but the short version is that an Eloquent model used purely as a database row, with all the rules in services, is the asking pattern in disguise. Tell-don't-ask wants the rules where the data is.

  • Domain exceptions appear. If a method like cancel() is called when the reservation can't be cancelled, what does it do? It throws a domain exception. That's the topic of the Failure and Validation chapter.

  • Naming gets sharper. Methods named with intent (cancel, confirm, checkIn) replace methods that hint at internals (updateStatus, setFlag, processData). This is naming things by intent in action.

The one thing to remember

If you find yourself reading a value out of an object and immediately branching on it, that's the moment to stop and ask whether the decision belongs inside the object instead.

Course Content