who owns what

staff grug (level 4 of 6)explanationteaches: adr-boundary, adr-testabilityneeds: game-aggregate, state-fields, key-snapshot, draw-only, adr-boundary, adr-testability

alreadyYou can trace a field from input through update to draw and have read the three decision records.

hereHow to name the owner of each value and each write, and what a change touches when one module takes a new job.

Page: who owns what

Job: explanation

Level: staff grug (level 4 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.

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
Two benches, one shared Game slab, one writer.

This page teaches the ownership map: which module produces a value, which module writes a field, and which module only reads.

why

main.ts:8#read_input samples the keyboard once and hands the snapshot to advance. Input owns the snapshot and nothing else. main.ts:9#clock shows the frame supplying elapsed time; the frame orders the work and owns no round state. main.ts:10#draw_game then paints what the steps produced. The round itself lives in state.ts:6#aggregate, and draw.ts:9#draw_game reads that value while changing no field.

Four owners follow from those lines. input.ts produces Input. update.ts writes the fields of Game. draw.ts produces DrawCommand values. main.ts owns the order of the three calls. A reader who asks who owns the ball's x position gets one answer: update writes it, state holds it, draw reads it. Each write has one home.

The type checker is the enforcement. A signature names the value a function receives, so a module that starts writing a field shows up in the caller list the compiler prints. adr-testability at tier 3 used that property to build a Game value on the spot. The same property lets a change at this rung land with the write site named before the edit.

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
One crate, one write arrow in, one dashed read arrow out: the fields have one owner each.

what breaks

A draw function that writes the ball gives the game two writers. The frame rate then changes the simulation, and a bug in physics has two files to hide in. A module-level Game removes the argument that names the writer, so a test must reset state between cases.

A frame function that keeps its own copy of the score spreads the round across two places. The score shown on screen and the score used by the goal check then come from different values.

where it fits

why the state is one plain object and why the loop pays for a fixed step chose this boundary at tier 3. This page makes the ownership explicit before a change touches it. change one rule, follow the result changes one owner's rule and traces the result. when this shape stops fitting asks when the boundary itself stops fitting.

try it

Name the owner of the accumulator before you change it. The proof prints the three calls that fix the order:

source anchor: this line is in the file

run grep -n -E 'read_input|clock|draw_game' main.ts

It prints:

8:  const input: io.Input = io.read_input(keys); // read_input: one snapshot per frame
9:  sim.advance(game, input, frame_time); // clock: the caller supplies elapsed time
10:  return screen.draw_game(game); // draw_game: paint what the steps produced

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

reference: the write sites named in one place

advance at update.ts:11#advance writes the accumulator and the scores. move_paddles at update.ts:43#keypair writes the paddle positions. The two passes at update.ts:62#hface and update.ts:73#vface write the ball. score_if_out at scoring.ts:4#goalcheck writes a score and calls serve. draw_game writes none of them.

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, one keyboard snapshot per frame, draw reads, update writes, why the state is one plain object

flowchart LR
  A0["one value holds the round"]
  A1["one keyboard snapshot per frame"]
  A2["draw reads, update writes"]
  A3["why the state is one plain object"]
  H["you are here: who owns what"]
  A0 --> H
  A1 --> H
  A2 --> H
  A3 --> H

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 4 of 6Q1 · Which module creates the Input value that every step reads?

try first, then show answer

2. input.ts, which samples the keys once and returns it

why

read_input at input.ts:13#read_input builds the snapshot from three key reads and returns it. main.ts:8#read_input calls that reader once before the simulation starts. State holds the round; it does not produce the input value.

level 4 of 6Q2 · Which line names the module that writes a score after a goal?

try first, then show answer

3. scoring.ts:4#goalcheck, after both collision passes have run

why

score_if_out at scoring.ts:4#goalcheck adds a point and calls serve after both collision passes. draw.ts:9#draw_game turns the score number into text and changes no field. The frame in main.ts:7#DrawCommand only orders the calls.

level 4 of 6Q3 · Why can a score rule be judged without opening a window?

try first, then show answer

2. Build a Game value directly with known field values

why

state.ts:6#aggregate names the fields a Game value carries, so a rule can be handed a value with a chosen ball position. advance at update.ts:11#advance takes that value as an argument. A window and a keyboard stay outside the signature.

level 5 of 6Q4 · draw_game starts writing the ball position each frame. What follows?

try first, then show answer

1. A second writer of the ball makes the frame rate change the simulation

why

draw_game at draw.ts:9#draw_game reads the value and returns commands. A write to ball.x there makes the number of paint calls per second change the number of writes. The simulation then follows the display rate.

level 3 of 6Q5 · Who owns the order of input, advance and draw?

try first, then show answer

1. main.ts:7#DrawCommand, the frame function

why

frame in main.ts:7#DrawCommand calls the reader, the simulation step and the painter in that order. The line at main.ts:10#draw_game shows the paint after advance at main.ts:9#clock. The order lives in one function.

level 5 of 6Q6 · Game moves into a module global. What does a reader lose?

try first, then show answer

1. The signature that names every caller allowed to write a field

why

A parameter names the value a function receives, and the type checker lists its callers. A module global removes that line from every signature. A test then carries over the last case's writes.

level 4 of 6Q7 · One module samples the keys, another writes the fields and a third returns drawing commands. Name the module that orders the three calls.

try first, then show answer

main.ts

why

frame at main.ts:7#DrawCommand holds the three calls: read_input, advance and draw_game. It keeps no round state of its own. The file that orders them is main.ts.