# Design Heuristics
> **In one sentence:** "it depends" is usually true, so this chapter spells out *what* it depends
> on, chiefly the subdomain's type and complexity, and turns that into a decision tree.

## What a heuristic is

A **heuristic** is a rule of thumb. It isn't guaranteed to be right every time, but it's good enough
to make a sound decision quickly, because it focuses on the few signals that matter most and ignores
the noise. Everything in this chapter is a heuristic, not a law.

## Sizing bounded contexts

- **Size is a poor target.** Don't aim for "small" (or "micro") for its own sake. Let the *model*
  decide the size of the context, not the other way round.
- **Changes that cut across contexts are expensive**, especially when different teams own them.
  Frequent cross-context changes mean the boundaries are wrong, and moving physical boundaries
  later is costly, so bad ones tend to stay and turn into technical debt.
- **Boundaries break most when the domain is uncertain or keeps changing**, which describes core
  subdomains, especially early on.
- **The heuristic: start wide for core subdomains.** Put a new core subdomain in a context together
  with the subdomains it talks to most (core, supporting or generic). Mistakes inside one context
  are cheap to fix, because moving *logical* boundaries is far cheaper than moving *physical* ones.
  Split the context once your understanding settles.
- Generic and supporting subdomains are more predictable, so they're less risky to separate early.

## Picking a business logic pattern

Ask these questions in order:

1. **Does it handle money, need a reliable audit trail, or need deep analysis of its behavior?**
   If yes, use an **event-sourced domain model**.
2. **Is the business logic complex?** If yes, use a **domain model**.
3. **Are the data structures complex?** If yes, use **active record**.
4. Otherwise, use a **transaction script**.

**What counts as "complex"?** Complex logic has real rules, invariants and algorithms. Simple
logic mostly validates input. Another clue is the ubiquitous language itself: does it describe
CRUD operations, or processes and rules?

**Use the result to check your assumptions.** If what you called a core subdomain only needs a
transaction script, or a "supporting" one clearly needs a full domain model, look again at how you
classified it. And remember that a core subdomain's edge isn't always technical.

## Picking an architecture

Once the logic pattern is chosen, the architecture mostly follows from it:

| Business logic | Architecture | Why |
|---|---|---|
| Event-sourced domain model | **CQRS** | Without projections you can only load single aggregates by ID |
| Domain model | **Ports & adapters** | Keeps aggregates and value objects free of persistence concerns |
| Active record | **Layered + service layer** | The service layer orchestrates the active records |
| Transaction script | **Minimal layered** (three layers) | Nothing more is needed |

*Exception:* CQRS is useful with **any** of these whenever the subdomain needs its data in
several persistent models.

## Picking a testing strategy

The same choices also suggest where to put most of your testing effort:

| Shape | Emphasis | Fits |
|---|---|---|
| **Testing pyramid** | Mostly unit tests | Domain model and event-sourced domain model, because aggregates and value objects make ideal units |
| **Testing diamond** | Mostly integration tests | Active record, because the logic is spread across the service and business layers |
| **Reversed pyramid** | Mostly end-to-end tests | Transaction script, because the logic is simple and there are few layers, so test the whole flow |

## The whole decision tree

Put it all together and the subdomain's type and complexity guide the logic pattern, which guides
the architecture, which guides the testing strategy:

```
money / audit / deep analysis? ──yes──▶ event-sourced domain model ▶ CQRS              ▶ pyramid
        │no
complex logic?                 ──yes──▶ domain model               ▶ ports & adapters  ▶ pyramid
        │no
complex data?                  ──yes──▶ active record              ▶ layered + service ▶ diamond
        │no
                                        transaction script         ▶ layered           ▶ reversed pyramid
```

The tree reflects a deliberate bias: **use the simplest tool that works**, and reach for the
heavier patterns only when you need them. Some teams are so comfortable with event sourcing that
they use it everywhere, and that can work *for them*. Treat the tree as a starting point, adjust it
to your context, or build your own. It doesn't replace thinking.

## Check yourself

For each, pick a logic pattern, an architecture and a testing approach:

1. A core claims-assessment engine at an insurer, where every decision must be explainable afterwards.
2. A supporting staff-holiday request screen.
3. A nightly job that pulls exchange rates from a third-party API and stores them.

<details><summary>Answers</summary>

1. Event-sourced domain model, CQRS, mostly unit tests. It's core and needs a full history.
2. Active record (or transaction script if the data is flat), layered architecture, integration-focused tests.
3. Transaction script, layered architecture, end-to-end tests.

</details>
