Sub topic 12
Feature flags: merging unfinished work safely
This chapter assumes you can branch, commit, merge, and open a pull request, and that you know why long-lived branches cause painful merges. A feature flag lets you merge unfinished work into the main branch without changing what users see.
The problem flags solve
Trunk-based development asks for small, frequent merges to the main branch. Some features take weeks. If you hold that work on a branch until it is finished, you get a long-lived branch with a large, risky merge at the end.
A feature flag (also called a feature toggle) is a condition in the code that turns a feature on or off. You merge the unfinished code with the flag off. Deployed behavior stays the same. When the feature is ready, you turn the flag on.
This separates two events that branching alone ties together:
- Merging code into main.
- Releasing a feature to users.
A minimal flag
The simplest flag is a constant or a config value that selects between two code paths.
const flags = { newCheckout: false };
function checkout(cart) {
if (flags.newCheckout) {
return newCheckoutFlow(cart);
}
return legacyCheckoutFlow(cart);
}While newCheckout is false, every user gets legacyCheckoutFlow. The team can merge pieces of newCheckoutFlow to main in many small pull requests, and each one is reviewed and tested alongside everything else.
Where flag values come from
A hard-coded constant needs a new deploy to change. Teams usually move the value somewhere they can change at runtime:
| Source | Change requires | Typical use |
|---|---|---|
| Constant in code | Code change and deploy | Short-lived work in progress |
| Environment variable | Restart or redeploy | Per-environment switches |
| Config service or flag platform | No deploy | Gradual rollout, per-user targeting, quick shutoff |
A flag platform also supports percentage rollouts, where you enable the feature for 5% of users first. It also gives you a kill switch: if the feature misbehaves, you turn it off without reverting a commit.
Common errors
- Testing only one path. Both branches of the
ifmust stay tested while the flag exists. A broken path nobody runs fails on the day you flip the flag. - Leaving flags in forever. Each flag doubles the paths through that code. Once a feature is fully released, remove the flag and the legacy path in a cleanup pull request.
- Nesting flags. Flags that depend on other flags create combinations that are hard to reason about. Keep them independent.
- Flagging risky changes poorly. A flag hides behavior, but it does not hide a database migration or a changed shared function. Make schema and shared changes backward compatible so both paths still work.
Flags and branching strategy
Flags reduce the need for long-lived branches, so they pair naturally with trunk-based development and GitHub Flow. They add work of their own: flag management, extra tests, and cleanup. For a short change that finishes in a day or two, a flag is usually more overhead than it is worth.
Exercise
Your team merges a half-built search feature to main behind a flag set to false. Name two things you must still do before and after releasing it.
Show answer
Before release, test both the flag-on and flag-off paths so neither is broken. After the feature is fully released, remove the flag and the old code path so the code does not keep two versions.