Answer stability round 1: 0 🔴, 1 🟡 (0 misread); copy each row and say which sizes copy
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user