Feature Debugging flags

I chuckled today when I received an add from launchdarkly.com. They’re a “feature flag” SaaS provider. I explained the concept of feature flags to a non-software-developer just a few weeks ago, so maybe my phone was eavesdropping. I was amused by the bright-and-shiny presentation. Although it’s new to some people, this stuff ain’t new to me.

The concept of feature flags is an old friend. The old school EMS ERP system used a configuration-driven system to control application development. It allowed extension into customer-specific modifications. We also used this to control standard but customer-specific operating modes- for example, fiscal year vs calendar year or cash basis vs accrual basis.

I’ve used this more recently to add a level of granularity to debugging messages. Sprinkling Debug.Print messages during app dev and initial troubleshooting is fairly normal, and the compiler Debug/Release setting means you can leave them in your code forever without impacting released software. However, this requires extra work to remove them, and in the future you might want to restore them for diagnostics — and you already did the significant work of deciding which messages were valuable. So, how??

Add an enum that lists all features, another to list the features in active debugging, and a third to contain the other other features that are not being debugged. Enclose your Debug.Print statements with something like this:

        bool debugging = FraMES.IsFeatureDebuggingEnabled(Feature_Debugging_Flags.FraMESActivities);
        if (debugging) Debug.Print("frmMain.OpenActivityDialog is updating the font size to: {0}.", subdialog.Font.Size);
    /// <summary>
    /// Given the an application feature,
    /// return the status of it's debugging flag.
    /// </summary>
    /// <param name="enumName"></param>
    /// <returns></returns>
    public static bool IsFeatureDebuggingEnabled(Feature_Debugging_Flags feature)
    {
        // Extract the name of the feature into a string and search for the string name,
        // because the Enum doesn't translate directly to another Enum.
        string featureName = feature.ToString();
        bool result;
        if (Enum.IsDefined(typeof(Feature_Debugging_Flags_Enabled), featureName))
        {
            result = true;
        }
        else if (Enum.IsDefined(typeof(Feature_Debugging_Flags_NotEnabled), featureName))
        {
            result = false;
        }
        else
        {
            throw new NotSupportedException("MES Utilities has no application feature named: " + featureName
                + ". The MES Utils developer must add it to one of the enums FraMES.Feature_Debugging_Flags_Enabled or _NotEnabled."); 
        }
        return result;
    }
This entry was posted in C#, Musings, Programming. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.