Feature flags
Guard code with a flag so it can ship dark, roll out gradually or be switched off, then delete the flag once it has done its job. Work with the flag system the repo already has.
Rules
- Use the repo's existing flag tool and conventions. Never add a new platform, vendor SDK or dependency without the user's go-ahead. If nothing exists, suggest OpenFeature with a local provider (flagd file, in-memory or env-var) and wait for the answer.
- Server SDK keys, secret tokens and service credentials never go in client code, committed files, tests or logs. Read them from the environment or the secret store the repo already uses. Client-side keys differ per platform; see references/platforms.md before putting any key in browser or mobile code.
- Never create, change, toggle or archive a flag in a shared or production dashboard, and never call a platform management API for it, without the user's go-ahead. Write the flag change as a diff or instructions instead.
- Do not wrap secrets, static config or large payloads in flags, and do not use a flag for a one-line change.
1. Detect the existing tool
Search before writing: dependency manifests and lockfiles, imports of flag clients, config files, env var names, CI and IaC. Detection hints per platform are in references/platforms.md. Then find how existing flags are defined, evaluated and tested, and copy that shape (wrapper function, constants file, naming, context fields). If the repo has a flag registry or docs listing owners and expiry, add to it.
If several tools appear (a migration in progress), use the one the newest flags use and say which.
2. Add a flag
- Classify it: release (hide unfinished work, short-lived), experiment, ops/kill switch (can be long-lived), permission/entitlement (long-lived). Temporary flags get an owner and a removal date.
- Name it for the behaviour in the repo's casing, for example
checkout-new-tax-engine. Put the key in one constant, never as a string literal at several call sites.
- Choose the default as the safe path. The default is what users get when the flag service is down or the flag is missing. New feature: off. Kill switch protecting something that must keep working: on. Off means old behaviour, on means new behaviour.
- Define the flag where the repo defines flags (flag file, code registry, or instructions for the dashboard per rule 3).
- Evaluate at one decision point close to where behaviour diverges, and pass the result down instead of re-evaluating inside loops or deep helpers. For a flag that touches many places, branch once at the top and select an implementation (strategy or branch by abstraction) rather than scattering
if.
- Supply evaluation context (user or tenant key, plan, environment) only with fields already used by the repo; do not send personal data the targeting does not need. Server-side evaluation stays on the server; send the browser only the result.
- Keep the old path working and unchanged. Do not couple the flag to a schema change that cannot run with the flag off; use expand/contract migrations (
db-schema-design).
Done when: the flag key is defined once, the default is the safe path, and the old behaviour is untouched when the flag is off.
3. Test
- Test both states: flag off (legacy behaviour, must be unchanged) and flag on (new behaviour). Add tests next to existing tests for the guarded code.
- Set flag values through the repo's test hook or a test provider (an OpenFeature in-memory provider if the installed SDK has one, a flagd file, the platform's test or local mode), never the network. Reset after each test. Patterns in references/openfeature-and-testing.md.
- Test the default path: provider unavailable or flag missing returns the safe default.
- Combinations: test each flag alone, the all-new-off baseline, and the pairs that touch the same code. Do not enumerate every combination.
- Run the changed tests and the suites that cover the guarded code; report the commands and results.
4. Remove a flag
Do this when the rollout is finished, the experiment is decided, or the flag is expired. Find stale flags by owner/expiry in the registry, by git age, or when the user names one.
- Confirm with the user (or the dashboard state they report) which branch won and that the flag is fully on or off in every environment. Never assume from code alone.
- Search the whole repo for the key, its constant and wrappers: code, tests, flag files, config, IaC, docs, dashboards-as-code.
- Keep the winning branch, delete the losing branch and the flag evaluation, delete tests of the dead path and unwrap the tests of the live path.
- Delete the flag definition, constant, registry entry and any now-unused context fields, imports or helper code.
- Search again for the key; zero hits is the completion criterion, then run the tests.
- Tell the user to archive the flag in the platform after the deploy (rule 3). Archive rather than delete when the platform allows, so the key cannot be reused by accident.
5. Audit flag debt
For "which flags can we remove", a stale-flag or flag-debt audit, or a flag registry across the whole codebase, read references/flag-hygiene.md. The audit is read-only: inventory, classify, a registry diff and a ranked removal list, with each removal then done as section 4, one flag per change.
Which platform
Only when the user asks for a recommendation and the repo has none: prefer OpenFeature in code, with flagd (open source, file-based) or an env-var provider for a start, so a hosted platform can be added later through a provider without touching call sites. Platform status, licences, SDK packages and OpenFeature providers are in references/platforms.md. Several vendors changed hands recently (Split is now Harness FME; OpenAI announced a Statsig acquisition in 2025, closing unconfirmed), so check a platform's current docs before relying on a detail.
Gotchas
- A flag service outage, or an SDK that has not finished initialising, returns the default. A wrong default is a production incident.
- Local-evaluation server SDKs download all rules, so their keys are secret; client SDKs get one user's results only.
- Do not reuse an old flag key for a new feature; stale targeting applies.
- A flag cannot be removed until every environment and every deployed version agrees on the outcome.
- Changing a flag's rollout percentage or hashing can move users between variants.
References
- references/platforms.md: platform table, per-platform SDK packages, OpenFeature providers, detection hints, self-hosting. Read when detecting a tool, choosing one, or handling keys.
- references/openfeature-and-testing.md: OpenFeature concepts, setup, in-memory provider tests, hooks, flagd. Read when adding OpenFeature or writing flag tests.
- references/flag-hygiene.md: whole-codebase flag audit, search recipe, finding classes, registry template, removal ranking. Read for a stale-flag or flag-debt audit.