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 @@ -30,6 +30,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.
Expand Down
50 changes: 50 additions & 0 deletions coding/feature-flags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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. 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. 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
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, 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. 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).
1 change: 1 addition & 0 deletions coding/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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.
Expand Down
46 changes: 46 additions & 0 deletions test/feature-flags.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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",
"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}`);
}
});

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");
});