Lesson 11 of 17 · How We Think About Code
Thinking Data vs Thinking Business Processes
How most of us learn to build software
Tutorials, bootcamps, and formal courses introduce developers to the CRUD mindset first. It's highly technical and focused on data manipulation.
This is understandable, because introductory material to programming needs to be simple and easy to consume. The trouble starts later, when real-world projects are built. They're more complex than what's shown in the learning material.
Developers focus on the database and what columns it will have, what data types they'll be, and then they write the code based on the data in the database. The mental model is "tables with handlers on top". Everything else is built around that model.
Why "tables with handlers" stops working
The problem with the CRUD mindset is that businesses have their own processes.
Whenever a business process is executed, the process doesn't just save data in the database. It may also:
Validate the data based on specific business rules and requirements.
Talk to a third-party service integration.
Store data into multiple databases or database tables.
As a new member on a project, you'd probably have to guess what happens in the business process unless you ask questions of the product team or other engineers. The CRUD code in front of you doesn't tell you. It tells you what the database looks like, which is the wrong thing to learn first.
For example, think about what needs to happen whenever a guest reserves a room at the hotel from the earlier lessons. It does not just create a row in the reservations table.
What the business actually does
The alternative is to start from the business process and the behavior of what's actually happening, from a business point of view.
The people who design the processes of a hotel booking system think about use cases like:
Choose the property and the room for a date range.
Apply a rate plan or coupon (if applicable).
Authorize the guest's payment.
Confirm the reservation.
Check the guest in on arrival.
Check the guest out on departure.
Cancel the reservation and compute any refund.
There's no technical jargon in that list, and no mention of which table, which column, which API, or which queue. Those are decisions that come later, in service of these steps.
To use this way of thinking, you, the developer, need to understand the business domain and how it works before you can write the code. That's a much bigger commitment than learning a database schema.
A side benefit: shared language
It's very useful if you name the classes, methods, and properties exactly the same as the business people use them, so that everyone can talk the same language and understand each other, instead of business people having to learn technical terms.
Naming becomes a lot easier. You don't have to invent class names and method names. They're already there. The product manager calls it "confirming a reservation". You call it Reservation::confirm(). The support team talks about "checking a guest in". You call it Reservation::checkIn(). A new engineer reading the code learns the business by reading the code, and a business person describing a bug uses the same words you have to look for in the codebase.
This is sometimes called ubiquitous language. The fancy name is from Domain-Driven Design. The idea is older than the name, and you don't need DDD to do it. You just need to stop renaming the business's concepts on the way into the code.
A small example: confirming a reservation
The CRUD version, the way most tutorials and most early codebases write it:
<?php
declare(strict_types=1);
namespace App\Reservations\ConfirmReservation;
use App\Reservations\Reservation\Reservation;
final class ConfirmReservationCommandHandler
{
public function handle(ConfirmReservationCommand $command): void
{
$reservation = Reservation::findOrFail($command->reservationId);
$reservation->status = 'confirmed';
$reservation->payment_authorization_id = $command->paymentAuthorizationId;
$reservation->confirmed_at = now();
$reservation->save();
}
}In this first example, the focus is on updating a row in the reservations table. Which columns to write, and what values to put in them. Those are implementation details. ActiveRecord is being used here, where each database column is linked to a property on the object, so Reservation is the Eloquent model.
If a product manager came to you and said "what happens when a reservation is confirmed?", you'd have to read every line of this method, decode the conventions, and translate the column writes into a description of behavior. None of those steps add value. They're translation overhead.
The business-process version of the same use case:
<?php
declare(strict_types=1);
namespace App\Reservations\ConfirmReservation;
use App\Reservations\Clock;
use App\Reservations\DomainEvents;
use App\Reservations\Reservation\Reservations;
final readonly class ConfirmReservationCommandHandler
{
public function __construct(
private Reservations $reservations,
private Clock $clock,
private DomainEvents $events,
) {}
public function handle(ConfirmReservationCommand $command): void
{
$reservation = $this->reservations->withId($command->reservationId);
$reservation->confirm(
paymentAuthorizationId: $command->paymentAuthorizationId,
confirmedAt: $this->clock->now(),
);
$this->reservations->save($reservation);
$this->events->dispatch(new ReservationConfirmed($reservation->id));
}
}With Reservation::confirm looking something like:
<?php
declare(strict_types=1);
namespace App\Reservations\Reservation;
use App\Reservations\Stay;
use DateTimeImmutable;
final class Reservation
{
public function __construct(
public readonly int $id,
public readonly int $roomId,
public readonly int $guestId,
public readonly Stay $stay,
public readonly int $totalPriceCents,
private ReservationStatus $status = ReservationStatus::PENDING,
private ?int $paymentAuthorizationId = null,
private ?DateTimeImmutable $confirmedAt = null,
) {}
public function confirm(int $paymentAuthorizationId, DateTimeImmutable $confirmedAt): void
{
if ($this->status !== ReservationStatus::PENDING) {
throw new ReservationNotPendingException($this);
}
$this->status = ReservationStatus::CONFIRMED;
$this->paymentAuthorizationId = $paymentAuthorizationId;
$this->confirmedAt = $confirmedAt;
}
}In this second example, the method has a business-related name. If you care about how it's implemented, you can read the method itself.
This is the plain Reservation that Room::reserve() returned, with a status added. Reservations and DomainEvents are the ports from the previous lesson, and Reservations gains withId() so the handler can load a stored reservation. Clock is a port of the same kind: the handler asks it for the time instead of reading the system clock, so a test decides what confirmedAt is.
<?php
declare(strict_types=1);
namespace App\Reservations\Reservation;
interface Reservations
{
public function nextId(): int;
public function withId(int $id): Reservation;
public function save(Reservation $reservation): void;
}<?php
declare(strict_types=1);
namespace App\Reservations;
use DateTimeImmutable;
interface Clock
{
public function now(): DateTimeImmutable;
}<?php
declare(strict_types=1);
namespace App\Reservations\Reservation;
enum ReservationStatus: string
{
case PENDING = 'pending';
case CONFIRMED = 'confirmed';
case CHECKED_IN = 'checked_in';
case CHECKED_OUT = 'checked_out';
case CANCELLED = 'cancelled';
}What the second version buys you
A few specific things, beyond "it reads better":
The business process is named. "Confirm a reservation" is a thing in the code, with its own name and its own place, instead of a status change you infer from which columns got written.
Invariants live in one place. The rule "a reservation can only be confirmed from the pending state" is enforced inside
Reservation::confirm. There is no other way in the business code to flip the status that bypasses it. In the CRUD version, anyone with aReservation::findOrFail()can write any value into thestatuscolumn, and the rules are someone's responsibility to remember.The code reads as a story. Load the reservation, confirm it, save it, announce it. Four lines.
It has a place to grow. The handler already dispatches
ReservationConfirmed, andconfirmalready draws the line between the act itself and its side effects. When the business adds a step later ("send a confirmation email", "notify the channel manager"), the question "where does this go?" has a mechanism to attach to: a new listener, or a new step insideconfirm. In the CRUD version, the same question doesn't have a default answer, which is why the code drifts.
The same shape carries the rest of the use-case list from earlier in this lesson, and each name tells you what happens before you read the body:
Room::reserve()Reservation::cancel()Reservation::checkIn()Reservation::checkOut()
Same database, different code
The business-process version is not "no database". It still ends up with rows in tables, just like the CRUD version. The schema can even be identical. The difference is in where decisions live and what shape the code takes.
In the CRUD version, the database is the source of truth. The code reflects the schema. The business rules are inferred from how the rows are written.
In the business-process version, the domain is the source of truth. The schema reflects what the domain needs to remember. The business rules are stated as code, in the language of the domain, and the database is a place to persist them.
You don't have to switch tools or reach for fancier patterns to make this shift. You can do it inside the same framework, the same ORM, the same project. What changes is what you write first and what you optimize for.
When CRUD is fine
Remember that this way of thinking and writing code may take a while to get used to, and it depends very much on the context you're in. It's fine to use the CRUD approach for specific situations where there's no specific business process, like data management software, an internal admin tool, a settings page, a tagging system, or a back-office editor where the user really is just editing rows.
A useful test for the reservation example: "edit a reservation" is mostly CRUD if the guest is fixing a typo in their name. "Confirm a reservation" is not CRUD, even though the database write is one row, because the business has a whole process around confirming a reservation that the row only represents.
Where this pays off most
The business-process mindset pays off most in finance, real estate, education, and health software.
In my experience, these businesses don't mostly manage data; they have specific processes that happen on a daily basis, with rules, exceptions, sequences, and stakeholders. Thinking that way makes the code's complexity easier to manage and matches how the business itself understands its own work.
The cost-of-change curve is much steeper for CRUD-shaped code in process-heavy domains, because every business change has to be reverse-engineered from data writes spread across the codebase, instead of made directly to the named process.
What this isn't
ActiveRecord and DataMapper (Doctrine's style, where the object knows nothing about the table it's stored in) can both express the business-process mindset perfectly well; the choice of mapping style is below the level of this lesson. This isn't a case for writing extra layers into every project either: small features, small products, and admin tools are fine in CRUD, and forcing a "confirm a reservation"-style structure onto a tag editor is the same kind of mistake as the opposite, just in the other direction.
The one thing to remember
Code that mirrors the business tells the reader what the business does. That's worth more than code that mirrors the database, even though it takes more thought up front, because it ages much better.
Related Links
- Anemic Domain ModelMartin Fowler on models that hold data but carry no behavior.
- CRUD is an anti-patternMathias Verraes on how CRUD hides user intent behind generic data writes.
- Task-Based UIGreg Young's case for interfaces built around explicit user tasks instead of data forms.
- Unbreakable Domain ModelsMathias Verraes's talk on protecting business rules inside the model.
- What is Domain ModelingShawn McCool on modeling as a design activity, not a folder structure.