Domain-Driven Design (DDD) Basics

Organize code around the real business concepts it models, with entities, value objects, and a vocabulary shared with domain experts, not around technical convenience.

What is it?

A common failure: a team builds a shipping feature. With the warehouse manager they talk about "packages", "shipments", and "carriers". In the code, it all becomes generic Record objects with string fields and type flags, shaped however was easiest to store.

Six months later, a new engineer can't tell which fields matter, what a "shipment" may do, or how it differs from a "package". The business concepts got lost in translation.

Domain-Driven Design works the other way. Model the code around the real business concepts (the "domain"), using the exact words the business experts use, everywhere: in conversations, code, and docs. That shared vocabulary is the ubiquitous language. Using it consistently closes the gap between what the business means and what the code says.

Two building blocks show up constantly:

- An entity has a distinct identity that lasts even as its attributes change. An Order with an ID is still "the same order" after its status goes from "pending" to "shipped". - A value object has no identity. It's defined entirely by its values, and two with the same values are equal and interchangeable. A Money of "$20.00 USD" is simply the same value as any other Money with that amount and currency.

DDD is a large topic. This is a starting foothold, not the whole picture.

Explain like I'm 10

A doctor and patient need the same words for the same things: 'the left knee', not 'that joint thing'. DDD insists the code uses the same precise vocabulary as the people who understand the business, not its own generic terms.

Examples

Before: generic, technically-convenient shapes

// Everything is a loosely-typed record with string flags
const shipment = {
  type: "shipment",
  status: "pending",
  amount: 1999, // cents? dollars? unclear
  currency: "USD",
};

function process(record) {
  if (record.type === "shipment" && record.status === "pending") {
    // ...
  }
}

Nothing here reflects the domain's real concepts. A bare 'amount' number invites bugs (cents vs. dollars), and no code says what a Shipment is allowed to do.

After: entities and value objects modeling the domain

class Money {
  constructor(amountCents, currency) {
    this.amountCents = amountCents;
    this.currency = currency;
  }
  equals(other) {
    return this.amountCents === other.amountCents && this.currency === other.currency;
  }
}

class Shipment {
  constructor(id, cost) {
    this.id = id; // gives this object identity — two Shipments are only "the same" if the ID matches
    this.cost = cost; // a Money value object, not a bare number
    this.status = "pending";
  }
  markDispatched() {
    if (this.status !== "pending") throw new Error("Only a pending shipment can be dispatched");
    this.status = "dispatched";
  }
}

Money is a value object: two with the same amount and currency are interchangeable. Shipment is an entity: its id makes it 'the same shipment' as its status changes, and its own method enforces the rule about when it can be dispatched.

How it works

DDD starts with conversations, not code. Engineers and domain experts pin down the exact meaning of terms ("what counts as a 'dispatched' shipment?") and agree to use them everywhere.

Those terms become the names of classes, methods, and fields:

- An entity for each concept with a real identity. - A value object for each concept defined purely by its data. - Methods on those objects that enforce the business rules, instead of loose if-checks scattered across service functions.

Why does it exist?

It closes the gap between what the business means and what the code says. Left open, that gap causes:

- Miscommunication: an engineer builds the wrong thing because "order" meant something subtly different to them. - Code that's hard to trust: business rules are scattered as ad-hoc checks instead of living in one clearly named place that matches how the business describes them.

When to use it

DDD pays off in parts of a system with complex, valuable business logic: the "core domain" the business competes on. It's most worth it when you have real access to domain experts and the logic is complicated enough that getting concepts exactly right matters.

When not to use it

For a simple CRUD screen where the "domain" is "store this form and show it back", entities, value objects, and a curated ubiquitous language are overhead with nothing to justify them. Save full DDD for parts of the system where the domain really is complex.

Common mistakes

  • Letting the code's vocabulary drift from the business's (e.g., the business now says 'return' but the code still says 'refund') instead of actively maintaining the ubiquitous language.

  • Making every object an entity with an ID, even natural value objects (like an amount of money or an address), which adds needless identity tracking.

  • Putting business rules in generic service functions instead of on the entities and value objects, losing much of DDD's benefit while still using its vocabulary.

Practice exercises

  1. Easy:

    For an online bookstore, decide whether 'Book' (a specific physical copy tracked in inventory) and 'ISBN' should be entities or value objects. Justify each choice.

  2. Medium:

    Using the loosely typed 'shipment' example above, write a small ubiquitous-language glossary (5-6 terms) that the warehouse and engineering teams would agree on.

  3. Hard:

    Model a 'hotel room booking' domain with at least one entity and one value object. Include one business rule enforced as a method on the entity (e.g., a booking can't be modified within 24 hours of check-in).

Interview questions

What is the 'ubiquitous language' in Domain-Driven Design?

A shared vocabulary that engineers and domain experts build together and use consistently in conversations, docs, and code. It removes the translation gap between how the business talks and how the code is written.

What's the difference between an entity and a value object?

An entity has a distinct identity that lasts as its attributes change; two entities are 'the same' only if their identity matches. A value object has no identity and is defined entirely by its data; two with the same data are equal.

Why does DDD emphasize putting business rules on the domain objects themselves rather than in separate service functions?

So each rule lives in one place, named in the business's vocabulary, and is enforced wherever the object is used, instead of being reimplemented or forgotten in scattered if-checks.