Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ change that a consumer must act on has a "Consumer action:" line.

- `coding/types.md` (#362). Do not turn off the type check with `any`, `dynamic`, `Any` or `@ts-ignore`. Use the safe top type and check the value at the boundary. Write explicit types for public signatures and local variables. The page names the exceptions where a language cannot name a type, and the strict settings for TypeScript, JavaScript, C# and Python. `review-standards` has a smell row and `setup-quality-gates` links the page. `CONTRIBUTING.md` records the dkstack exception: the scripts are plain JavaScript, and a test fails on `var`.
- `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 <dir>` (#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
Expand Down
43 changes: 43 additions & 0 deletions coding/absent-values.md
Original file line number Diff line number Diff line change
@@ -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
`<Nullable>enable</Nullable>` 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<User>`. 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<User | undefined>` 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).
1 change: 1 addition & 0 deletions coding/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Coding standards and practices.
- [types.md](types.md): No escape from the type system, and explicit types.
- [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.
- [feature-flags.md](feature-flags.md): Each flag has an owner and an issue that removes it.
Expand Down
54 changes: 54 additions & 0 deletions test/absent-values.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
import { readFile, sectionText } from "./helpers.mjs";

const root = join(dirname(fileURLToPath(import.meta.url)), "..");

async function text(path) {
return readFile(join(root, path), "utf8");
}

// 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",
"Make values present by default.",
"Turn on the null check of the language and run it in CI as an error.",
"`strictNullChecks`",
"`<Nullable>enable</Nullable>`",
"Show in the type when a value can be absent",
"Do not use a special value for \"no value\"",
"`-1`",
"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 with content", async () => {
const page = await text("coding/absent-values.md");
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<User | undefined>`"), "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 () => {
assert.ok((await text("coding/index.md")).includes("(absent-values.md)"), "coding/index.md does not list the page");
});