DEV Community

Cover image for Fun with Feature Flags

Fun with Feature Flags

Matt Eland on September 20, 2019

Feature flags are a simple technique that let you toggle whether a new piece of functionality is available in your software or some aspect of how i...
Collapse
 
seangwright profile image
Sean G. Wright • Edited

I think this is an important topic that is easy to ... well, just not know about!

Thanks for adding this topic to your collection of posts!

Launching new features "darkly" (code is in the app, but a feature flag has it turned off) will allow teams to continue to keep code merging into master, deploying, running tests on new code without requiring it to be fully completed.

One thing I would add is (and I realize you aren't recommending this practice - you just wrote some example code) is to move the Feature Flag logic out of a static global class.

Those static helper classes aren't testable and are often directly tied to volatile dependencies (ex: database, file system, app-settings.json/AppSettings.config).

Instead either supply a feature service behind an interface:

public interface IFeatureService
{
   bool IsFeatureEnabled(string featureName);
}

// or ...

public interface IFeatureService
{
   bool IsFeatureEnabled<TContext>(TContext featureContext);
}

Where you can abstract away logic to determine if the feature is enabled by checking an API, database, or environment configuration.

This approach is helpful when features need to be dynamic based on the execution context (ex: not the same for every user).

The other option is to create very specific feature 'configuration' classes that are populated in your composition root when using Inversion of Control and a Dependency Injection container.

public class UserDiscountFeature
{
    public bool IsEnabled { get; }

    public UserDiscountFeature(bool isEnabled)
    {
        IsEnabled = isEnabled;
    }
}

These can be populated per-request or as a singleton for the lifetime of the app, but they tend to not change often.

For example, the feature is off today, but we update the configuration value in the database tomorrow, and then it's on for everyone.


ASP.NET Core has feature management baked in, so the patterns are established - we just have to choose our implementation!

Collapse
 
integerman profile image
Matt Eland

I had not worked with Microsoft.FeatureManagement before. My go-to with .NET has been FeatureToggle despite some of the odd syntax, because it makes spelling mistakes in configurations a non-issue by erroring early and by giving you strongly-typed solutions. I'll have to look into feature management more.

Collapse
 
seangwright profile image
Sean G. Wright

The error-ing early part is something I always lean towards when possible.

IoC containers like SimpleInjector verify your container by building everything in it 1 time at startup.

If you include your Feature configuration types in your container then the verification step will throw an exception if values can't be found in app settings or the database (assuming your classes guard against invalid parameters).

I haven't used FeatureToggle - I'll have to check it out.

Thread Thread
 
integerman profile image
Matt Eland
Thread Thread
 
seangwright profile image
Sean G. Wright

I appreciate you writing that up!

Collapse
 
baileyjs78 profile image
baileyjs78 • Edited

I agree that feature flags should be treated as a class, not a static or magic string. It makes deleting (cleaning up) them so much easier. In my opinion, the technical debt of deleting old flags is the biggest draw back for their use, however I still think it is worth it.

I've been part of a team working on an open source feature flag management project with a UI (think of a simple launch darkly, for free). We are in the process of simplifying the code base (remove sql, use core 3) right now. This has allowed our non developers (management, qas) to manage feature flags without having to have access to code.

github.com/NSIAppDev/Moggles
shameless plug

Collapse
 
seangwright profile image
Sean G. Wright

Thanks for the link - I'm gonna check this out.

Btw I think you typod the URL, it's missing an 's'

Collapse
 
baileyjs78 profile image
baileyjs78

Thanks, I fixed it

Collapse
 
ivarconr profile image
Ivar Conradi Østhus • Edited

Hi, I am the author of Unleash, the open source feature flags service with client SDKs for most popular languages. Could be a great option when you want to change your toggles remotely,dto gradual rollout etc.

unleash.github.io/

Collapse
 
thatzacdavis profile image
Zachary Davis

Launch Darkly is the best!

Collapse
 
integerman profile image
Matt Eland

I reached out to them for a bit more info after they contacted me on something else but they never replied.

Collapse
 
thatzacdavis profile image
Zachary Davis

Oh bummer, what were you wondering about it?

Thread Thread
 
integerman profile image
Matt Eland

It was a longer response to a sales inquiry from downloading an eBook they advertised on LinkedIn. The key paragraph I sent was:

I have to admit that I have looked at LaunchDarkly in the past for some personal game development hobbyist projects and found your prices prohibitive so I stopped evaluating you for my own needs, but as a community representative, I'd love to see a blurb on the specific value you offer over the multitude of configuration-based feature flag libraries available. Feel free to send me a sales spiel for me to take in as I'd love to be able to present more perspectives in the matter.

I think they just wrote me off, since I never got a reply.