Prompt
How do I add feature flags to an existing app?
Latest observation
A good way to add feature flags to an existing app is to treat them like a small, centralized configuration system rather than sprinkling if statements everywhere.
1) Start with the goal
Feature flags are useful for:
- gradual rollout
- A/B testing
- hiding unfinished features
- quick kill-switches for risky code
- environment-specific behavior
Decide whether you need:
- simple on/off flags
- targeted flags by user, org, region, etc.
- percentage rollouts
- remote management from a dashboard
2) Pick an implementation approach
Option A: Build a simple internal flag system
Good if your needs are small.
Typical structure:
- a
FeatureFlagsservice/module - a config file or database table for flag values
- a helper like
isEnabled("new_checkout")
Example:
if (featureFlags.isEnabled("new_checkout")) {
renderNewCheckout();
} else {
renderOldCheckout();
}
Option B: Use a feature flag platform
Good if you need:
- remote updates without deployments
- targeting rules
- analytics
- gradual rollout
- audit logs
Popular tools include LaunchDarkly, Unleash, ConfigCat, Split, Firebase Remote Config, etc.
3) Centralize flag access
Don’t read flags directly from random places in code. Put all access behind one abstraction.
Example:
class FeatureFlags {
isEnabled(flagName: string, user?: User): boolean {
// read from config, DB, or remote provider
return false;
}
}
This makes it easy to:
- change providers later
- mock in tests
- enforce consistent rules
4) Define a flag naming convention
Use names that are:
- descriptive
- stable
- easy to search
Examples:
checkout_new_flowbilling_retry_v2enable_dark_mode
Avoid vague names like flag1 or newStuff.
5) Decide how flags are evaluated
Common evaluation modes:
- global: on/off for everyone
- environment-based: dev/staging/prod
- user-targeted: specific accounts or segments
- percentage rollout: 10% → 25% → 100%
A common rollout pattern:
- enable in dev
- enable internally
- enable for a small % of users
- ramp up gradually
- remove old code when stable
6) Keep flag logic close to the boundary
Use flags at decision points, not deep inside business logic.
Better:
if (flags.isEnabled("new_pricing")) {
return NewPricingPage();
}
return OldPricingPage();
Less ideal:
calculatePrice() {
if (flags.isEnabled("new_pricing")) {
// many nested branches
}
}
7) Add tests for both paths
When a flag changes behavior, test:
- flag ON
- flag OFF
Also test edge cases if flags are user- or segment-based.
8) Make flags temporary when possible
Many feature flags become technical debt if never removed.
Best practice:
- add an owner
- add an expiry date
- remove the old path once the feature is fully rolled out
A useful rule: if a flag is permanent, treat it more like a config setting than a feature flag.
9) Add observability
Track:
- who has the flag enabled
- errors when a flag is on
- rollout progress
- performance changes
This helps catch issues early during staged rollouts.
10) Roll out safely
A practical rollout process:
- implement the new code behind a flag
- default flag off
- deploy
- enable for internal/test users
- enable for a small production cohort
- monitor metrics/errors
- ramp to full release
- remove old code and flag
Simple example architecture
feature_flagstable or config fileFeatureFlagService- middleware or request context that determines user/tenant
- UI/backend checks via
isEnabled()
Common pitfalls
- adding flags without a cleanup plan
- using too many flags
- mixing business rules and flag logic everywhere
- forgetting cache invalidation if flags are remote
- not testing both states
- shipping a flag and never removing dead code
If you want, I can show you:
- a simple DIY feature flag implementation,
- how to do it in a specific framework like React, Node, Django, or Rails, or
- a database-backed design for multi-user apps.