logo
podcast Podcast
get help Get Unstuck

Limit the number of feature flag checks

You'll thank yourself later

Joel Clermont
Joel Clermont
2024-11-25

Last week, I answered the question of when I would use a feature flag. Today, I want to build on that topic and talk about where in your code you should actually check if the feature flag is on or off.

The initial thought might be: I'll check the feature flag in all the files that make up the feature, or where the feature is presented: controllers, views, models, and so on.

It is worth questioning if that is really necessary, though. You probably don't want to spread out the feature flag check into dozens of places in your code.

So what is the alternative? Let's use a tangible example to discuss in more detail.

Say your app is going to launch into a new country. Before, all your users were in country X, but now they could also be coming from country Y.

First, you'll need to ask them what country they are in. And then based on what they pick, there will be different data collected in the user onboarding flow.

While you're building this, you don't even want the new country or its corresponding onboarding flow to show up to new users.

A great use for a feature flag! But back to our question: where will we check if the feature flag is on or off?

Clearly, we'll need to check if the feature is enabled before we ask them what country they are in. If the feature is on, we'll show the new country selection question. If the feature is off, we'll continue to hard-code them to country X, as we always have before.

But what about the rest of the onboarding flow? What about the dozens of other places in the app where we customize behavior based on the user's country?

None of those additional places require checking the feature flag. At each of those places, we only need to check what the user's country is set to. It's irrelevant if the feature is on or not. They couldn't set the new country if the feature was off.

With this approach, we end up with one place where we check the feature flag instead of dozens.

This keeps our code much more tidy and makes the feature flag logic easier to understand.

Here to help,

Joel

P.S. What if they mess with our form and set a different country we didn't put in the list? That's what validation rules are for.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.