From 101d88e808bda4efc3bc4e6eac73937edf9dcf0e Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Fri, 27 Nov 2026 20:32:08 +0100 Subject: [PATCH] =?UTF-8?q?Answer=20stability=20round=201:=200=20?= =?UTF-8?q?=F0=9F=94=B4,=201=20=F0=9F=9F=A1=20(0=20misread);=20copy=20each?= =?UTF-8?q?=20row=20and=20say=20which=20sizes=20copy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- principles/technical-principles.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/principles/technical-principles.md b/principles/technical-principles.md index da2e4d2e..6053f99e 100644 --- a/principles/technical-principles.md +++ b/principles/technical-principles.md @@ -46,8 +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. + - A function that keeps an argument after it returns keeps its own copy, so a change the caller makes later doesn't reach it. BANNED: `this.rows = rows`, which keeps the caller's array. BANNED: `this.bytes = bytes.slice()`, since on a Node `Buffer` `slice()` shares the storage, as `subarray()` does on every typed array. GOOD: `this.rows = [...rows]`, copying each row too where a row is an object the caller can change, and `this.bytes = new Uint8Array(bytes)` for a typed array, which copies it. + - A size limit counts every cell the code allocates, empty ones too, not only the bytes of its output, and the check runs before the allocation. BANNED: a limit on a returned table that sums the bytes of its cells, so a million rows of a thousand empty cells make a billion cells that add zero bytes, and the program runs out of memory before it reports the limit. GOOD: the limit counts each cell, empty or not, before the code allocates it. * 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.