Add to the technical principles that a function keeps a copy of its input and a limit counts every entry

This commit is contained in:
2026-11-27 20:27:16 +01:00
parent 72a1c921bb
commit 912e8e1191
2 changed files with 12 additions and 0 deletions
+10
View File
@@ -3805,3 +3805,13 @@ Read most (opens and searches):
- `bin/run-tests` 1
- `ci.sh` 1
- `todo.md` 1
## Reading cost, chunk b54e19271a0f, finished 2026-11-27T19:26:50Z
40918 new input tokens before the first commit.
Read most (opens and searches):
- `skills/larv-review/SKILL.md` 5
- `agents/stability-reviewer.md` 4
- `README.md` 1
+2
View File
@@ -46,6 +46,8 @@
- A cast (`x as Foo`) asserts what the compiler could not check and stops it checking — so when the type later changes, nothing tells you. Derive the real type instead: infer it, name the return type, or parse the input once into a typed shape. Where a value genuinely arrives untyped (HTTP body, `JSON.parse`, DB row), the boundary owes a **type guard validating the fields it claims**; past that boundary everything is typed. `as unknown as T` and non-null `!` are the same defect wearing a hat. BANNED: `const user = body as User` — one rename and it is a silent lie. GOOD: `function isUser(v: unknown): v is User` checking each field, then `if (!isUser(body)) return { error: … }`.
- DTOs, enums, and value objects are pure data — no logic. They stand in for the structs/records a language lacks.
- Don't loosen a type when refactoring. A function that returned a concrete type keeps returning it when its implementation changes, never `object`, `array`, `mixed` or `any`; introduce a DTO or value object instead. BANNED: `totals(): Totals` becomes `totals(): array` once it reads from two sources. GOOD: `totals(): Totals` stays, and a new `SourceTotals` value object holds what each source returns.
- A function that keeps an argument keeps a copy the caller's later change cannot reach. BANNED: `this.bytes = bytes.slice()`, since on a Node `Buffer` `slice()` shares the storage, and the same for `subarray`, or `this.rows = rows`, which keeps the caller's array. GOOD: `new Uint8Array(bytes)` and `[...rows]`.
- A limit counts what the code allocates, every entry and every cell, not only the bytes it prints. BANNED: a table limit that sums the bytes of the cells, so a million records of a thousand empty columns add a billion entries that count zero, and the program runs out of memory before it reports the limit. GOOD: the count includes each entry, empty or not, and the check runs before the allocation.
* Always use explicit state machines.
* Most, if not all, code should have automated tests, and each test earns its keep.
- A valuable test checks behaviour through the unit's interface, fails when that behaviour breaks, and still passes after a refactor that keeps that behaviour.