Implementing basic Feature Flags

Welcome on this Friday the 13th 👻.

Today I'm going to show you how to lock down specific features in your app by using the most simple version of Feature Flags imaginable.

Recently I've been working on a new feature for Locative that I wanted to lock down, so I can thoroughly test it. Once ready to release I just wanted to flip a toggle. I've achieved this by declaring a simple enum that'll be queried in places where the feature might be required.

Let's take a look at an example implementation.

enum Feature {
    case audioEditor, customIcons, debugMode
    var isEnabled: Bool {
        switch self {
        case .audioEditor:
            return false
        case .customIcons:
            return true
        case .debugMode:
        #if DEBUG
            return true
            return false

Now once where at the place where the code path for accessing the specific feature might be hit, we can easily query for its availability:

guard Feature.debugMode.isEnabled else { return }

That's all what's required for super simple Feature Flags. Obviously there are more sophisticated and complex solutions, but for my very simple needs, and in most cases when working on an App as an Indie, this is probably enough.

What's your take on Feature Flags, are you using them, and how? If you'd like to jump into a conversation feel free to ping me @Kidmar.