We all work on awesome beautiful apps and we do it by constantly working on improvements, features and fixes. Everything goes smoothly most of the time until it does not!
How many times have you found yourself in a situation where you are working on a very beautiful and useful feature, and suddenly customer reports an issue or you noticed a crash in console.
Now you need to fix and ship it immediately. Most of the time, fix is easy but shipping is not!
The reason is, shipping the fix will also ship the feature you are working on and it’s not complete yet!
It’s easy you will think, I will just take last shipped commit and will create a fix on top of that. As easy as it sounds, that approach will create a bunch of issues like —
hotfix
branch into your current branch.Now, things get messy when you have weird merge conflicts because your current branch is too much ahead. Also, it’s hard to manage when you need to make multiple hot fixes during a feature development.
So what’s the answer to all these issues?
Let me introduce Feature Flags!
We are what we repeatedly do. Excellence, then, is not an act, but a habit. Try out Justly and start building your habits today!
Feature Flag is a coding practice where while working on a new feature, everything related to that feature will be developed in a such a way that the feature can be enabled/disabled from a boolean flag.
That means we don’t need to keep track of multiple branches in case of hot-fix. The app can easily be shipped with feature flag OFF and it will behave exactly like it was before the feature was introduced.
Feature flag are even more handy when there are multiple team members contributing to a repository. In that case, sometimes the team will have multiple features in progress and Feature Flag will be the ultimate solution.
Alright, enough with the intro, let’s move on to how you can start using them now!
Feature Flag is just a boolean! That means you don’t need any SDK or Framework to start using it.
Though there are some frameworks that allow turning feature flag on/off remotely, means you can turn on feature progressively after the app is rolled out on stores. Ever wondered why your friend has a new cool Whatsapp feature but you don’t?
Let’s see an example!
Consider a scenario where you are working on a TODO app. The list screen is in production and now you are working on Add TODO item feature.
I will use swift
and SwiftUI
in this example but you can use similar technique in any language.
We will start by adding a class that will keep track of all feature flags. This class will be one per app, you can keep adding/remove features from there as needed.
class FeatureFlags {
static let isAddTodoEnabled = true
}
Remember — one flag per feature. Don’t use 1 flag for multiple features as then you will lose power of controlling them individually.
Now in list view, where we want to control visibility of Add
button based on the feature flag, we can take care of those
struct TaskListView: View {
@StateObject var viewModel = TaskListVM()
var body: some View {
List(viewModel.tasks) { (task: Task) in
TaskItemView(task: task)
}
.background(Color.yellow)
.navigationTitle("Tasks")
.onAppear {
self.viewModel.refreshTasks()
}
.navigationBarItems(trailing: FeatureFlags.isAddTodoEnabled
? Button("Add", action: { self.viewModel.onAdd() })
: nil
)
}
}
Here, we have added Add
button to navigation bar based on the condition FeatureFlags.isAddTodoEnabled
.
Now we can easily enable/disable add feature throughout our app with a single flag.
You will have basic understanding of how feature flags work and where it can be applicable. Now you can easily start using it in your current or next project.
The example I added above uses local feature flag. However, you can go one step ahead and use something like Firebase A/B testing tool to control feature flag from Firebase console. There are so many other popular tools are available as well like Launch Darkly.
That’s it for today, hope you learned something new!