From 47c0ad8dc5a8e8a32794357c4d8e3e975147c69b Mon Sep 17 00:00:00 2001 From: Daniel Kindl Date: Wed, 25 Nov 2026 17:01:57 +0100 Subject: [PATCH 1/2] docs(coding): add the absent values rule Closes #368 --- CHANGELOG.md | 1 + coding/absent-values.md | 43 +++++++++++++++++++++++++++++++++++++ coding/index.md | 1 + test/absent-values.test.mjs | 40 ++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 coding/absent-values.md create mode 100644 test/absent-values.test.mjs diff --git a/CHANGELOG.md b/CHANGELOG.md index 13bf9cc..567f2ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ change that a consumer must act on has a "Consumer action:" line. ### Added - `skills/improve-agents` (#198). The owner starts it after a session, a pull request or an issue. It finds changes to the agents' environment in six categories and grades each finding with the `verify-claim` levels. A finding with no event is a hypothesis. The report goes to the owner. A finding about dkstack becomes a draft issue, not an edit. The idea comes from the `retro` skill by Matt Pocock. +- `coding/absent-values.md` (#368). A value that can be absent says so in its type. The null check of the language is on. No special value stands for "no value". Input is converted once at the boundary. An absent value does not report a failure, and an empty collection means "no items". - `scripts/check-consumer.mjs` checks a consumer: the `AGENTS.md` pointer, `CLAUDE.md`, `.editorconfig`, `.gitattributes`, the lockfile, the CI audit steps and Dependabot. It also checks each copied dkstack script against `scripts/dkstack-hashes.json` for its tag, with no network. `setup-quality-gates` step 4 copies both (#305). - `scripts/check-links.mjs` has an option `--dkstack-root ` (#332). Without it, a run in a consumer checks only the local links and skips each dkstack URL. With it, the run checks each dkstack file and diff --git a/coding/absent-values.md b/coding/absent-values.md new file mode 100644 index 0000000..c6e189e --- /dev/null +++ b/coding/absent-values.md @@ -0,0 +1,43 @@ +# Absent values + +A value that can be absent must say so in its type. A value that is in the type cannot be absent. + +An absent value is `null`, `undefined`, `None`, `nil` or an empty optional. Apply this page to +code that you add or change. + +## Rules + +- Make values present by default. Turn on the null check of the language and run it in CI as an + error. Examples are `strictNullChecks` in TypeScript, which `"strict": true` turns on, and + `enable` in C#. See [types.md](types.md). Reason: the checker then finds + each place that reads a value that can be absent. +- Show in the type when a value can be absent, for example `User | undefined`, `User?`, + `Optional[User]` or `Option`. Reason: the caller sees that it must handle the absent case. +- Do not use a special value for "no value", for example `-1`, `0` or `""`. Use the absent value + of the type. Reason: a caller can use a special value as a real value. See + [error-handling.md](error-handling.md). +- Check an input at the boundary, where it enters the code. Convert it once to a type that + cannot be absent. Do not check the same value again deeper in the code. Reason: one check at + the boundary is easy to test. Many checks inside the code hide which values are possible. +- Do not use an absent value to report a failure. Return or throw an error with the reason. See + [error-handling.md](error-handling.md). Reason: a caller cannot tell "not found" from "the + database is down". +- Return an empty collection for "no items", not an absent value. Reason: the caller can loop + over an empty collection with no check. +- Do not use a non-null assertion, for example `user!.name`, to silence the checker. Use it only + when a check before it proves that the value is present. See [types.md](types.md). + +## Good example + +`findUser(id: UserId): Promise` returns `undefined` when no user has the ID. It +throws an error when the database fails. The handler checks for `undefined` once and returns +"404 Not Found". The rest of the handler uses a `User`. + +## Bad example + +`findUser(id)` returns `null` when no user has the ID, and also when the database fails. The +handler passes the result on. A function three calls deeper reads `user.email` and fails with a +`TypeError`. The log does not show that the database was down. + +Related: [fail fast](../architecture/fail-fast.md), +[design by contract](../architecture/design-by-contract.md). diff --git a/coding/index.md b/coding/index.md index d549bf6..7c94880 100644 --- a/coding/index.md +++ b/coding/index.md @@ -10,6 +10,7 @@ Coding standards and practices. - [naming.md](naming.md): Identifier forms, meaning and enforcement. - [quality-gates.md](quality-gates.md): Never weaken a quality gate. - [error-handling.md](error-handling.md): Handle errors explicitly. +- [absent-values.md](absent-values.md): Show an absent value in its type. - [logging.md](logging.md): Logging events and the observability assessment. - [security.md](security.md): Security basics. - [testing.md](testing.md): A pointer to [tdd/](../tdd/index.md), which has the testing rules. diff --git a/test/absent-values.test.mjs b/test/absent-values.test.mjs new file mode 100644 index 0000000..584322c --- /dev/null +++ b/test/absent-values.test.mjs @@ -0,0 +1,40 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { fileURLToPath } from "node:url"; +import { dirname, join } from "node:path"; +import { readFile } from "./helpers.mjs"; + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); + +async function text(path) { + return readFile(join(root, path), "utf8"); +} + +test("coding/absent-values.md states the absent value rules", async () => { + const page = await text("coding/absent-values.md"); + for (const part of [ + "# Absent values", + "null check", + "strictNullChecks", + "enable", + "special value", + "`-1`", + "at the boundary", + "empty collection", + "error-handling.md", + "non-null assertion", + "types.md", + ]) { + assert.ok(page.includes(part), `missing ${part}`); + } +}); + +test("coding/absent-values.md has a good and a bad example", async () => { + const page = await text("coding/absent-values.md"); + assert.ok(page.includes("## Good example"), "missing good example"); + assert.ok(page.includes("## Bad example"), "missing bad example"); +}); + +test("coding/index.md lists coding/absent-values.md", async () => { + assert.ok((await text("coding/index.md")).includes("(absent-values.md)"), "coding/index.md does not list the page"); +}); From 6ec088d78651de89bd73501f7f8e7f4d6a12feef Mon Sep 17 00:00:00 2001 From: Daniel Kindl Date: Wed, 25 Nov 2026 17:28:19 +0100 Subject: [PATCH 2/2] test(absent-values): pin the full rule sentences --- test/absent-values.test.mjs | 44 ++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/test/absent-values.test.mjs b/test/absent-values.test.mjs index 584322c..1269b58 100644 --- a/test/absent-values.test.mjs +++ b/test/absent-values.test.mjs @@ -2,7 +2,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { fileURLToPath } from "node:url"; import { dirname, join } from "node:path"; -import { readFile } from "./helpers.mjs"; +import { readFile, sectionText } from "./helpers.mjs"; const root = join(dirname(fileURLToPath(import.meta.url)), ".."); @@ -10,29 +10,43 @@ async function text(path) { return readFile(join(root, path), "utf8"); } -test("coding/absent-values.md states the absent value rules", async () => { - const page = await text("coding/absent-values.md"); +// Join wrapped lines so that a phrase can span a line break. +function flat(value) { + return value.replace(/\s+/g, " "); +} + +test("coding/absent-values.md states each absent value rule", async () => { + const page = flat(await text("coding/absent-values.md")); for (const part of [ "# Absent values", - "null check", - "strictNullChecks", - "enable", - "special value", + "Make values present by default.", + "Turn on the null check of the language and run it in CI as an error.", + "`strictNullChecks`", + "`enable`", + "Show in the type when a value can be absent", + "Do not use a special value for \"no value\"", "`-1`", - "at the boundary", - "empty collection", - "error-handling.md", - "non-null assertion", - "types.md", + "Check an input at the boundary, where it enters the code.", + "Convert it once to a type that cannot be absent.", + "Do not use an absent value to report a failure.", + "Return an empty collection for \"no items\", not an absent value.", + "Do not use a non-null assertion", + "only when a check before it proves that the value is present", + "(error-handling.md)", + "(types.md)", ]) { assert.ok(page.includes(part), `missing ${part}`); } }); -test("coding/absent-values.md has a good and a bad example", async () => { +test("coding/absent-values.md has a good and a bad example with content", async () => { const page = await text("coding/absent-values.md"); - assert.ok(page.includes("## Good example"), "missing good example"); - assert.ok(page.includes("## Bad example"), "missing bad example"); + for (const heading of ["## Good example", "## Bad example"]) { + const body = sectionText(page, heading).split("\n").slice(1).join("\n").trim(); + assert.ok(body.length > 80, `${heading} is empty or too short`); + } + assert.ok(flat(sectionText(page, "## Good example")).includes("`findUser(id: UserId): Promise`"), "good example lost its signature"); + assert.ok(flat(sectionText(page, "## Bad example")).includes("`TypeError`"), "bad example lost its failure"); }); test("coding/index.md lists coding/absent-values.md", async () => {