From 9a0c8037df64543435b6e5d9453eafff3d4f7ee8 Mon Sep 17 00:00:00 2001 From: Daniel Kindl Date: Wed, 25 Nov 2026 17:14:57 +0100 Subject: [PATCH 1/2] docs(coding): add the feature flags rule Closes #377 --- CHANGELOG.md | 1 + coding/feature-flags.md | 46 +++++++++++++++++++++++++++++++++++++ coding/index.md | 1 + test/feature-flags.test.mjs | 39 +++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 coding/feature-flags.md create mode 100644 test/feature-flags.test.mjs diff --git a/CHANGELOG.md b/CHANGELOG.md index 13bf9cc..07ea4b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ change that a consumer must act on has a "Consumer action:" line. block with no paragraph directly above it fails. A tracked `.png` or `.svg` file in `docs/` fails. A diagram with more than 12 nodes gives a warning only. `npm run check` and `validate` run it. The file has no dependencies, so a consumer can copy it. +- `coding/feature-flags.md` (#377). Each temporary flag has an owner and a linked issue that removes it. Tests cover each state that can reach production. A flag has a safe default, is not nested, and is not configuration or a permission. - `setup-quality-gates` copies `check-title`, `check-dashes` and `check-writing` to a consumer, with `markdown-files.mjs` and `pr-event.mjs` that they import (#304). Step 4 gives the title steps of `validate` as the CI example. A test pins the list and runs each script alone. diff --git a/coding/feature-flags.md b/coding/feature-flags.md new file mode 100644 index 0000000..c8bbbfc --- /dev/null +++ b/coding/feature-flags.md @@ -0,0 +1,46 @@ +# Feature flags + +A feature flag turns a code path on or off at run time. A flag is temporary. Remove it when its +job is done. + +Use a flag to merge work that is not finished, or to roll out a change to a part of the users +first. Apply this page to each flag that you add or change. + +## Rules + +- Give each flag an owner and a linked issue that removes it. Write the issue number beside the + flag definition. Reason: a flag with no owner stays in the code after nobody remembers why. +- Name the flag for the behavior that it turns on, for example `newCheckoutFlow`. See + [naming.md](naming.md). +- Test each state of the flag that can reach production. Reason: the path that is off today is + on tomorrow, and the change of state must not break the code. +- Give each flag a safe default. When the source of the flag is not available, the code uses the + default and logs the failure once. Reason: an outage of the flag service must not turn on code + that is not finished. +- Remove the flag after the full rollout. Remove the dead path and its tests in the same change. + Reason: each flag doubles the number of paths that a reader must understand. +- Do not nest a flag in the path of another flag. Reason: two nested flags give four paths, and + some of them are never tested. +- A flag is not configuration and not a permission. Put a value that differs between + environments in configuration. See + [architecture/configuration.md](../architecture/configuration.md). Check what a user may do + with the permission model of the product. Reason: a permission that is a flag has no audit and + is removed with the flag. +- A long-lived operations switch, for example a switch that turns off an expensive feature during + an incident, is configuration. Document it as configuration, not as a flag. + +## Good example + +The flag `newCheckoutFlow` has the owner `payments` and the comment `Remove: #412`. Its default +is off. Tests run the checkout with the flag on and off. After the rollout to all users, one pull +request removes the flag, the old flow and the old tests, and closes #412. + +## Bad example + +The flag `beta2` has no owner and no issue. It was turned on for all users a year ago. The old +path is still in the code, and nobody tests it. A second flag inside the new path turns on a +discount for staff. When the flag service is down, the code reads `beta2` as off. All users see +the old checkout, which no longer works with the new prices. + +Related: [reversibility](../architecture/reversibility.md), +[small steps](../architecture/small-steps.md). diff --git a/coding/index.md b/coding/index.md index d549bf6..963da43 100644 --- a/coding/index.md +++ b/coding/index.md @@ -12,6 +12,7 @@ Coding standards and practices. - [error-handling.md](error-handling.md): Handle errors explicitly. - [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. - [testing.md](testing.md): A pointer to [tdd/](../tdd/index.md), which has the testing rules. - [dependencies.md](dependencies.md): No unjustified dependencies. - [debugging.md](debugging.md): Reproduce the problem and fix the cause. diff --git a/test/feature-flags.test.mjs b/test/feature-flags.test.mjs new file mode 100644 index 0000000..36adaa0 --- /dev/null +++ b/test/feature-flags.test.mjs @@ -0,0 +1,39 @@ +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/feature-flags.md states the feature flag rules", async () => { + const page = await text("coding/feature-flags.md"); + for (const part of [ + "# Feature flags", + "owner", + "linked issue", + "each state", + "Remove the flag", + "dead path", + "architecture/configuration.md", + "permission", + "safe default", + "Do not nest", + ]) { + assert.ok(page.includes(part), `missing ${part}`); + } +}); + +test("coding/feature-flags.md has a good and a bad example", async () => { + const page = await text("coding/feature-flags.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/feature-flags.md", async () => { + assert.ok((await text("coding/index.md")).includes("(feature-flags.md)"), "coding/index.md does not list the page"); +}); From ea7dee4059298a374bf35f2ec68d46d789b9b67e Mon Sep 17 00:00:00 2001 From: Daniel Kindl Date: Wed, 25 Nov 2026 17:21:03 +0100 Subject: [PATCH 2/2] docs(coding): fix feature flags review findings --- coding/feature-flags.md | 20 ++++++++++++-------- test/feature-flags.test.mjs | 7 +++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/coding/feature-flags.md b/coding/feature-flags.md index c8bbbfc..a138fe2 100644 --- a/coding/feature-flags.md +++ b/coding/feature-flags.md @@ -14,14 +14,17 @@ first. Apply this page to each flag that you add or change. [naming.md](naming.md). - Test each state of the flag that can reach production. Reason: the path that is off today is on tomorrow, and the change of state must not break the code. -- Give each flag a safe default. When the source of the flag is not available, the code uses the - default and logs the failure once. Reason: an outage of the flag service must not turn on code - that is not finished. +- Give each flag a safe default. The safe default is the state that is correct for users now. For + a flag in rollout it is off. When the full rollout is done, change the default to on, and then + remove the flag. When the source of the flag is not available, the code uses the default. It + logs the failure once. Reason: an outage of the flag service must not turn on code that is not + finished. It must not turn off code that all users already use. - Remove the flag after the full rollout. Remove the dead path and its tests in the same change. Reason: each flag doubles the number of paths that a reader must understand. - Do not nest a flag in the path of another flag. Reason: two nested flags give four paths, and some of them are never tested. -- A flag is not configuration and not a permission. Put a value that differs between +- A flag is not configuration and not a permission. A flag is temporary and has a removal issue. + Configuration stays for the life of the product. Put a value that differs between environments in configuration. See [architecture/configuration.md](../architecture/configuration.md). Check what a user may do with the permission model of the product. Reason: a permission that is a flag has no audit and @@ -32,15 +35,16 @@ first. Apply this page to each flag that you add or change. ## Good example The flag `newCheckoutFlow` has the owner `payments` and the comment `Remove: #412`. Its default -is off. Tests run the checkout with the flag on and off. After the rollout to all users, one pull -request removes the flag, the old flow and the old tests, and closes #412. +is off. Tests run the checkout with the flag on and off. After the rollout to all users, the +default changes to on. Then one pull request removes the flag, the old flow and the old tests, and +closes #412. ## Bad example The flag `beta2` has no owner and no issue. It was turned on for all users a year ago. The old path is still in the code, and nobody tests it. A second flag inside the new path turns on a -discount for staff. When the flag service is down, the code reads `beta2` as off. All users see -the old checkout, which no longer works with the new prices. +discount for staff. The default of `beta2` is still off. When the flag service is down, all users +see the old checkout. It no longer works with the new prices. Related: [reversibility](../architecture/reversibility.md), [small steps](../architecture/small-steps.md). diff --git a/test/feature-flags.test.mjs b/test/feature-flags.test.mjs index 36adaa0..2ab8c9f 100644 --- a/test/feature-flags.test.mjs +++ b/test/feature-flags.test.mjs @@ -23,6 +23,13 @@ test("coding/feature-flags.md states the feature flag rules", async () => { "permission", "safe default", "Do not nest", + "The safe default is the state that is correct for users now.", + "change the default to on", + "A flag is not configuration and not a permission.", + "A flag is temporary", + "closes #412", + "Tests run the checkout with the flag on and off.", + "The default of `beta2` is still off.", ]) { assert.ok(page.includes(part), `missing ${part}`); }