Skip to content

26. Separation of Concerns (SoC) ​

A game does three different jobs: it decides what is happening, it shows it, and it keeps time. MVT assigns each job to one owner (the model, the view and the ticker) and keeps them apart, so each can be understood, tested and changed without the others. Within each job, the same idea applies at a smaller scale: keep each component cohesive, and the components loosely coupled.

Related: Why MVT · Rules · Single Source of Truth (SSOT)


26.1 The Principle ​

Any program of real size does several different jobs at once. A concern is one of those jobs: an area of work you can think about on its own, such as reading the player's input, drawing the screen, or saving progress.

Separation of concerns means arranging code so that each concern lives in one place, and can be read, tested and changed without thinking about the others. Edsger Dijkstra coined the phrase in 1974, calling it "the only available technique for effective ordering of one's thoughts". It is less a rule than a habit of mind: before writing code, ask which concern it belongs to.

26.2 Symptoms ​

Mixing concerns is the obvious way to write code, and at first it feels fine. Here is a frame of a tiny game written that way. A ship drifts right, wraps around at the edge of the arena, and is drawn as a sprite.

ts
let lastTime = performance.now();

function onFrame(now: number) {
    const deltaMs = now - lastTime;
    lastTime = now;

    shipX += SPEED * deltaMs;
    if (shipX > ARENA_WIDTH) shipX -= ARENA_WIDTH;

    shipSprite.x = shipX * PIXELS_PER_UNIT;

    requestAnimationFrame(onFrame);
}

It is short and it works. But each of its lines answers one of three quite different questions:

LinesThe question they answerThe concern
shipX += ..., the wrapWhat is happening?Domain: the game's state and logic
shipSprite.x = ...How is it shown?Presentation: sprites, pixels, sound
lastTime, deltaMs, requestAnimationFrameWhen, and how fast?Time: the ticker

Now try a few ordinary requests against it:

  • Test the wrap. You can't run onFrame without a sprite and a browser, and you can't make time pass on demand.
  • Pause the game. The clock is read in the middle of the domain logic, so pausing means editing the domain logic.
  • Show the ship twice, say on a minimap. The domain logic and the main sprite are in one function, so the minimap either copies the domain logic or hooks into the middle of it.

Each request is about one concern, and each forces you to untangle the other two first. In a ten-line function that is easy. In a real game, with the same tangle spread across hundreds of files, it shows up as three familiar symptoms:

  • Every change touches code it shouldn't. A designer tunes the ship's speed, an artist redraws it, someone adds slow motion to the debug menu. Three changes, for three reasons, by three people, and each edits code that the other two depend on. Each change risks breaking something that its author never meant to touch.
  • Testing needs everything running. Domain logic could be tested by asserting values (after two seconds, the ship has wrapped), but tangled with drawing and a clock it can only be tested the hardest way: run the whole game and watch.
  • Each change is harder than the last. Every new line has to fit around all three concerns at once, so the tangle grows, and so does the effort of the next change.

26.3 Applied to MVT: Three Concerns, Three Owners ​

MVT separates the three concerns and assigns each one to an owner:

ConcernOwnerHandlesNever depends on
DomainModelThe game's state and logic, in domain units (tiles, metres)Pixels, renderers, views, the clock
PresentationViewHow to show state: sprites, layout, sound, easingDomain logic, what happens next, the clock
TimeTickerWhen each frame happens, how much time passed, the order of each frameDomain logic, how anything is shown

The same tiny game, separated:

ts
// Domain: the model, without pixels or a clock.
const ship = {
    x: 0, // arena units
    update(deltaMs: number) {
        ship.x = (ship.x + SPEED * deltaMs) % ARENA_WIDTH;
    },
};

// Presentation: the view, without domain logic or a clock.
function refreshShipView() {
    shipSprite.x = ship.x * PIXELS_PER_UNIT;
}

// Time: the ticker, without domain logic or drawing.
let lastTime = performance.now();
function onFrame(now: number) {
    const deltaMs = Math.min(now - lastTime, MAX_DELTA_MS);
    lastTime = now;
    ship.update(deltaMs);
    refreshShipView();
    requestAnimationFrame(onFrame);
}

It is a few lines longer. In return, each of the three requests now touches one concern:

  • Test the wrap: call ship.update(...) with any times you like and check ship.x. The test doesn't need a sprite or a browser, and doesn't wait.
  • Pause: the ticker stops calling ship.update. The model doesn't change.
  • A minimap: write a second view that reads ship.x. The model and the first view don't change.

Notice too that time now enters the model as a number, deltaMs, rather than being read from a clock. That one change makes the model testable and pausable, and the Dependency Inversion chapter follows it further.

26.4 Applied to MVT: Cohesion and Coupling ​

Separating the three concerns is the large-scale step. The same idea applies again inside each one: a game's domain is many smaller concerns (the ship, the asteroids, the score), and so is its presentation.

At this scale, two older ideas do the work. A component (a model, a view, or a smaller piece of either) is cohesive when its contents belong together: they share state and change for the same reasons. Components are loosely coupled when they depend on each other only through small, declared interfaces. The Single Responsibility Principle (SRP), from Robert C. Martin, is cohesion taken to its limit, and Martin traces it to cohesion himself. In his later wording, "a module should be responsible to one, and only one, actor": one group of people whose requests change it. That reading fits MVT closely, since game designers change models and artists change views.

Splitting a component makes each piece more cohesive, but the pieces now have to call each other, so coupling grows, and following one idea means jumping between files. Split while the pieces would change for different reasons and depend on each other only a little. Stop when splitting further would only move the tangle into the wiring between them.

In practice:

  • A model covers one cohesive part of the domain. Burrow Bust's game model combines a digger model, a field model, and a model for each rock and each enemy, and handles what happens between them, such as a rock landing on an enemy. The rock model has one behaviour, its fall: it wobbles, drops and shatters. The enemy model has several (patrol, chase, ghosting, fire, inflation), but they all share the enemy's position, direction and phase, so splitting them would add more coupling than cohesion. If it grew, the fire sequence, which already runs on its own timeline, would be the first piece to split off.
  • A view covers one visual layer or effect. The dug-out field, the digger, the rocks, the HUD. A parent view arranges them.
  • A function's steps can be read apart. Putting steps in order is a job in itself, so a function that only calls a short list of named steps has enough to do. That works when each step can be understood from its name, without reading its code or the code around it. Steps that only make sense together belong in one function.

Two quick tests:

  • Describe the component in one sentence. Where it needs an "and", check that the two halves share state and change together. "Falls once the dirt beneath it is dug away" doesn't need one, since the wobble before the fall is part of falling. "Moves the digger and keeps the score" joins two halves that don't share any state, so it is two components.
  • Look at a view's bindings. A long list of loosely related bindings usually means several views in one.

26.5 Practical Benefits ​

  • Domain logic runs without a screen. A model can be tested, replayed and profiled on its own, in milliseconds, without a renderer.
  • One model, any number of views. The fruit machine demo is one model shown through four views at once: Pixi, three.js, an HTML panel and a text terminal. The views don't reference each other.
  • Time control is built in. Pause, slow motion, fast forward, single-step and replay all live in the ticker, because only the ticker reads a clock.
  • Bugs have an address. "The score is wrong" is a model bug; "the score is drawn in the wrong place" is a view bug. You know which file to open.
  • Work in parallel. Game logic and art can be built at the same time, against the same model interface.

26.6 Practical Drawbacks ​

Separation has drawbacks, and it is worth knowing them up front.

  • More components, and wiring between them. A model, a view, bindings and a ticker are more pieces than one function. In a tiny game, the extra code is noticeable. As the game grows, the separation becomes worth it, and Chapter 3, A First Game, shows how little ceremony it needs in practice.
  • Some things need a decision about which side they are on. A red flash when the player is hit is presentation: the game's outcomes don't depend on it. Two seconds of invulnerability after a hit is domain: it changes what happens next. The test is always the same: would any domain outcome change if this were different, or missing? If so, it belongs to the model. See Presentation State.
  • The domain is described in its own units. A model holds positions in tiles or arena units, not pixels, so every view converts. That conversion is one line, and it lets a second view draw at a different scale.
  • Concerns connect only through declared interfaces. A view gets the model's state only by reading it, and the model never calls the view. Something like "play a sound when a crate breaks" therefore needs the model to hold state that the view can notice. Noticing Changes shows how.

26.7 Common Misreadings ​

  • Not a folder layout. Separation is about what each component depends on, not where its file lives. This repo keeps a game's models and views side by side in one folder per game, and they are fully separated.
  • Not "views are dumb". A view can hold a lot of logic: layout, easing, presentation state, even a view model of its own. It can't hold domain logic, though. The fruit machine's reels land with a bounce that lives entirely in the views (reel-landing.ts): the model settles each reel in a straight line, and each view eases it its own way.
  • Not a game manager. The ticker only keeps time. "If the game is over, show the menu" is domain logic, so it lives in a model's state, and a view shows whatever that state says.
  • Not more layers. MVT has three concerns because a game has three kinds of job. Adding layers for their own sake (a service layer, a controller layer) adds wiring without separating anything new.

26.8 Checklist ​

Questions to ask of a design, or in a review:

  • For each piece of state: would any domain outcome change without it? If so, is it in a model?
  • Does any model import anything about presentation: a renderer, pixels, colours, textures, sounds?
  • Does any model read a clock, or schedule anything with a timer?
  • Does any view decide something that changes what happens next?
  • Does the ticker contain any decision about the game?
  • Could you delete a view and write a new one from the same model state, without changing the model?
  • Can you describe each model, view and function in one sentence, with any "and" joining things that share state?
  • Did any split leave pieces that can only be understood together?

26.9 Rules Built on SoC ​

RuleHow it follows
M-isolationThe domain doesn't depend on presentation or time
M-domainDomain state is described in domain units, leaving pixels to the presentation
M-timeTime is the ticker's concern; it reaches the model only as deltaMs
V-statelessDomain state is the model's concern, not the view's
V-outputPresentation is separate, so any presentation will do
V-treeDomain structure and presentation structure are separate concerns
T-minimalThe ticker only keeps time

26.10 Further Reading ​

  • Edsger W. Dijkstra, "On the role of scientific thought" (EWD 447, 1974). The origin of the phrase.
  • Edward Yourdon and Larry L. Constantine, Structured Design (1979). Coupling and cohesion.
  • Robert C. Martin, Agile Software Development: Principles, Patterns, and Practices (2002). The Single Responsibility Principle.
  • Robert C. Martin, Clean Architecture (2017), chapter 7. SRP restated as "responsible to one actor".
  • John Ousterhout, A Philosophy of Software Design (2nd ed., 2021). Deep modules, and when to split code apart or keep it together.
  • John Ousterhout and Robert C. Martin, "A Philosophy of Software Design vs Clean Code" (2025). A written debate; its Method Length section argues both sides of how far to split, over one worked example.
  • Robert Nystrom, Game Programming Patterns (2014), the Game Loop and Update Method chapters. The separation of time from the rest, as games practise it.