Skip to content

25. The Eight Principles ​

MVT is built on eight well-known design principles, and all eight serve one goal: you can understand, test and change any part of a game without touching the rest. MVT's rules are those principles applied to the situations that every game meets. This chapter introduces the eight, and shows how each builds on the ones before it. The chapters after it take one principle each, and the last of them uses all eight on a request that no rule covers. Learn the principles, and you can decide such cases yourself.

Related: MVT on One Page · Single Source of Truth (SSOT) · Using the Principles

A designer plays your game and asks for one small thing: the screen should shake when the player is hit. You check the rules in Appendix A. They tell you that a model advances only through update(deltaMs), and that a view doesn't hold domain state. The rules don't mention screen shake.

So where does the shake go? Does the model need to track it? How does the view detect that the player was hit, when the hit is over before the view ever runs? The rules don't say. They're short checks for the situations that every game meets, and no list of checks can name every request a designer will make. The principles that the rules come from can settle it. By the end of this part, you'll be able to answer each question, and say why.

25.1 Why Principles, Not Just Rules ​

Rules say what an MVT program must do. They're quick to check, and most of the time they're all you need.

The rules aren't where MVT starts, though. Each one is a principle applied to a situation that every game meets: time reaching a model, a view reading the state, the player pressing a key. Appendix A names the principles that each rule serves. When you meet a situation that no rule covers, such as a view that plays sounds instead of drawing, or two games that share a model, the principles still apply, and they give the kind of answer that a rule would.

A principle is a general statement about how to keep a program easy to understand and change. The eight in this chapter are decades-old ideas, each with a name you can search for. MVT applies all of them, together, to a program that runs in frames.

25.2 The Goal ​

A component, here and in the chapters that follow, is any separable unit of an MVT program: a model, a view, the ticker. Where that could be unclear, the text says MVT component, or names the kind. Every principle here serves one goal:

Any component of a game can be understood and tested alone, and changed or replaced without changing anything else.

This is easiest to see from its opposite. In a tangled program, a change ripples. Renaming a field breaks a view. The fix to the view changes what its parent passes in, and that changes what a test expects. Each step is small, but you can't tell in advance where the ripple stops. So every change takes more work than it should, and is riskier than it looks.

The goal is for the ripple to stop at the edge of the component you changed. The rest of the game isn't affected. You don't need to edit anything else, or check anything else again. Each principle below is one way of stopping the ripple.

25.3 The Eight Principles ​

They're best read in this order, since each builds on the ones before it. The chapters after this one cite them by their short names, such as SSOT, so the table gives each one's full name and its acronym together.

#PrincipleIn one lineIts question
1Separation of Concerns (SoC)Domain state, presentation and time are separate concerns, with separate owners: the model, the view and the tickerWhich concern does this belong to?
2Single Source of Truth (SSOT)Each piece of domain state lives in one place; everything else reads it or derives itWhere does this value really live?
3Command-Query Separation (CQS)Reading state never changes it: each tick runs every command, then every queryDoes this read change anything?
4Dependency Inversion Principle (DIP)A component declares what it needs, as a small interface, and receives it, time includedIs this component reaching for something it should receive?
5Composite Reuse Principle (CRP)Build bigger things by combining smaller ones, never by extending themCan this be built from components that already exist?
6Design by Contract (DbC)Every component states what it requires and what it guarantees, and fails loudly when a requirement isn't met or a guarantee can't be keptWhat does this need, and what exactly does it guarantee?
7Make Illegal States Unrepresentable (MISU)Design state and types so that illegal states can't even be writtenCan this be set to an illegal state?
8Design for Testability (DfT)Every component can be tested on its own, through its contract, with the same result every timeHow would I test this, with nothing real around it?

Each of the next eight chapters takes one principle. In each, you'll find where the idea came from, the symptoms of breaking it, what it means for MVT, its benefits and drawbacks, the ways people misread it, a checklist, and the rules that follow from it.

25.4 How the Principles Build on One Another ​

The principles aren't a checklist of separate ideas. Each one makes the next possible, or makes it worth using.

Separating the concerns puts domain state in models, with one home for each piece. Reading it without changing it means every view reads the same state, in any order, sixty times a second. Passing each component what it depends on lets components combine, a stated contract makes combining them safe, and the types enforce the contract where they can. Testing each component alone, with nothing real around it, proves the rest.

25.5 Summary ​

MVT rests on eight well-known principles, and each serves the same goal: a change to one component stops at its edge. The rules are those principles applied to the situations that every game meets, so when you meet one that no rule covers, the principles still decide it.

  • SoC gives domain state, presentation and time separate owners.
  • SSOT gives each piece of domain state one home, and CQS makes reading it safe from anywhere, in any order.
  • DIP passes each component what it depends on, which lets CRP build big components from small ones.
  • DbC states what each component requires and guarantees, and MISU lets the types enforce those guarantees.
  • DfT follows from all of them: each component can be tested alone.

The next eight chapters take one principle each, in this order. Then Chapter 34 comes back to the screen shake.

25.6 Further Reading ​

Each principle's chapter ends with its original sources.