why the state is one plain object

senior grug (level 3 of 6)explanationteaches: adr-boundary, adr-testabilityneeds: game-aggregate

alreadyYou have read the state lesson and know which fields the Game type holds.

hereThe decision to keep state as one plain object, the alternative that was rejected, and what the choice buys for testing.

Page: why the state is one plain object

Job: explanation

Level: senior grug (level 3 of 6)

Source lines:

why grug give these 3 tool notes why grug give the proof why grug give the grill why grug give the why under an answer
explanation

this page says why the code has this shape and what a reader must be careful of.

A timber crate named Game with its lid lifted; two paddle slabs, a ball, a clock and a small flag float above it, each tied to the crate mouth by a dashed lift line
The shape this decision chose: one crate, every field of the round inside it.

This page teaches the tradeoff behind the one value: what a global or a split state would have bought, and why the one-value boundary won.

why

The decision is written at docs/002-plain-state-struct.md:3#accepted. A single Game value at state.ts:6#aggregate gives the four files one boundary. update.ts takes the object and writes it. draw.ts takes the same object and reads it. Input returns a separate snapshot. A type checker shows every caller when a field is renamed.

The rejected alternatives lose in different ways. A global Game would have made the value reachable from every file with no parameter, and no reader would see the writers named in a signature: draw could change a score while update reads it, and a test would have to reset the global between cases. Splitting the state into per-file types would have localised writes but spread the truth: a serve would touch a score value, a ball value and a flag value, and no single place would hold “what the round is right now”. Passing the whole state as a fresh copy into update was also considered, since it is the most local shape of the three, but the shared object handed to advance at update.ts:11#advance is the part that makes changes stick, and a copy would leave the ball unmoved.

The boundary also makes a test cheap. A Game value can be built on the spot with a known ball position, and a score rule can be called without a window or a keyboard. No service has to start before the rule can be judged.

Two timber benches face a shared paper slab named Game: at the left bench a grug writes on the slab along a heavy amber arrow fed by the Input slip; at the right bench a grug reads through a magnifier along a thin dashed arrow, and a fire arrow from his bench is crossed out and labelled no write
The crossed-out arrow from the reading bench is the write a global Game would leave unguarded.

what breaks

A global Game would let draw change a score while update is reading it. A reader would trace writes across files to work out what a frame sees, and a test would have to reset the global between cases.

Passing the state as a fresh copy into update would leave the original untouched. The ball would never move.

where it fits

one value holds the round shows the fields. This page explains the shape choice. draw reads, update writes lives on one side of the boundary and time arrives in equal slices lives on the other.

try it

Build a Game value in a scratch TypeScript file, call score_if_out with the ball outside the right edge, and print the left score. The proof shows the object type in state.ts and the value returned by init_game:

source anchor: this line is in the file

run grep -n -E 'export type Game|return game' state.ts

It prints:

6:export type Game = { // aggregate: one value holds the whole round
32:  return game;

The cited file state.ts ships with this site at the same path. Run the command from the school folder, or open state.ts.

reference: every connection to this page
where this fits

This is reference. The next page in the reading order is at the bottom of the page.

learn first: one value holds the round

goes on to: who owns what, when this shape stops fitting

flowchart LR
  A0["one value holds the round"]
  H["you are here: why the state is one plain object"]
  B0["who owns what"]
  B1["when this shape stops fitting"]
  A0 --> H
  H --> B0
  H --> B1

this badge records quiz answers with a difficulty from 1 to 6. it does not measure independence or responsibility.

grill: check what you know

Grug asks. Say the answer out loud, then tap. Grug remembers what you miss and asks again in the pop quiz.

the number is a question's difficulty from 1 to 6. a coloured chip marks a question that asks for a change, an effect or a judgment, declared in the quiz row. the rung badge records quiz answers: two right at a level and the levels below it, with a declared transfer question at level 5 or 6. it does not certify independence or responsibility. the questions are level 1 of 6 recall a fact; level 2 of 6 recall a detail; level 3 of 6 trace a path; level 4 of 6 trace a longer path; level 5 of 6 predict or judge; level 6 of 6 judge a transfer.

level 3 of 6Q1 · What does one Game value give the four files?

try first, then show answer

3. One boundary for reads and writes

why

state.ts:6#aggregate puts the round in one value. update.ts writes that value and draw.ts reads it, so the boundary is visible in function arguments.

level 5 of 6Q2 · What does the compiler show when a field changes?

try first, then show answer

3. Every caller that reads it

why

The note at docs/002-plain-state-struct.md:15#compiler says a new field shows every caller. That is the cost of passing the state explicitly.

level 3 of 6Q3 · How can a score rule be tested here?

try first, then show answer

1. By constructing a Game value directly

why

The note names serving_to_right at docs/002-plain-state-struct.md:15#serving_to_right as a field a test can set up in isolation. No window is needed.

level 3 of 6Q4 · Why does advance take the same Game object?

try first, then show answer

3. So changes survive the call

why

advance at update.ts:11#advance takes the same Game object. The caller's writes reach that object; a fresh copy would leave it unchanged.

level 4 of 6Q5 · advance takes a fresh copy of Game after a refactor. What does the reader see?

try first, then show answer

2. Nothing update wrote survives the call, so the ball never moves

why

advance at update.ts:11#advance takes the same Game object. Its writes reach the object held by the frame loop; a copy would leave that object unchanged, so the ball would never move no matter how many steps ran.

level 4 of 6Q6 · Why would a test with a global Game need a reset between cases?

try first, then show answer

1. The global keeps whatever the last case wrote to it

why

A global Game keeps whatever the previous test wrote into it, so each case starts from the last case's leftovers and a reader cannot trust any verdict. The lesson's point: a Game value built on the spot starts from made-up numbers every time.