why the state is one plain object
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 answerthis page says why the code has this shape and what a reader must be careful of.
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.
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:
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
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.
try first, then show answer
3. One boundary for reads and writes
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.
try first, then show answer
3. Every caller that reads it
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.
try first, then show answer
1. By constructing a Game value directly
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.
try first, then show answer
3. So changes survive the call
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.
try first, then show answer
2. Nothing update wrote survives the call, so the ball never moves
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.
try first, then show answer
1. The global keeps whatever the last case wrote to it
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.