DEV Community

Cover image for Best Practices to Follow with ConfigCat's Feature Flags
Alex G. Mircean
Alex G. Mircean

Posted on • Originally published at configcat.com

Best Practices to Follow with ConfigCat's Feature Flags

Do you want to learn how to work with ConfigCat's feature toggles better and more efficiently? You've come to the right place! Let's take a look at the benefits of using some good practices when managing feature flags with ConfigCat.

A little bit about feature flags

You've probably heard about the concept of feature flags or feature toggles before, but let's summarize it really quick. A feature flag acts as a switch for enabling and disabling certain parts of an application.

For example, a company can use a feature flag to limit the use of an experimental feature to certain demographics or user groups.

Best practices

ConfigCat's feature flag management system is versatile and widely available. It can be set up and used with a variety of technologies (see ConfigCat's Docs). Let's take a look at what the best approaches are when working with this service. My examples will be in JavaScript, but they should still be relevant in most other languages.

Naming Conventions and Sorting

Case styles

Depending on preferences, naming conventions may vary from company to company. A development team would benefit from keeping the flags' name case consistent. This makes it easier to find the flag that you're looking for or to add a new flag.

For example, the first feature flag that appears when you open the ConfigCat dashboard is in camelCase, but that's not the only option. You could also use snake_case, PascalCase, or kebab-case ("could" doesn't mean that you "should" though, so it's advised to use camelCase like everybody else). Also, give your flags a suggestive name to avoid any misunderstandings or time wasted searching for a specific flag.

watch a flag

Space sorting

In larger apps, you'll probably have a large number of feature flags. Let's say you have a flag that's more important than the others and you always want to have that right at the top of your dashboard feature flag list. To achieve that, you can sneakily start the flag name with a space character and it will be automatically brought to the top because of the ASCII character order.

start with space

Feature flag tags

To group a selection of flags, simply click the +tag button next to each flag you want to group. You will now be able to see all the flags associated with that specific tag when you search for it. You can do it in a jiffy.

Watching a flag

Users can toggle the option to watch a flag in ConfigCat. When you watch a flag, you'll receive an email notification if the flag is not active for a certain time. Clicking on the little zombie icon next to the watch/unwatch button also allows you to set the time interval and a couple of other handy settings. Braaaains!

Keeping your SDK key safe

You've probably seen something similar in the official documentation. Creating the configCatClient in the manner presented below is great for getting to know the API and it works perfectly well.

var configCatClient = configcat.createClient("#YOUR-SDK-KEY#");
Enter fullscreen mode Exit fullscreen mode

For safety reasons though, it's better to save the SDK key string in a constant (yourSdkKey) that won't be available in the production source code. One suitable option is storing it in a file that's included in .gitignore. Storing it in the backend rather than on the client-side to make it less accessible is also a good solution.

var configCatClient = configcat.createClient(yourSdkKey);
Enter fullscreen mode Exit fullscreen mode

Flag configuration

In addition to the SDK Key, you can pass a few option parameters when creating the client. One of them is pollIntervalSeconds. It tells your app how often it should check the feature flag value updates. If you are developing an app where time is of the essence like a trading or betting app, you'd then want to check for your feature flag values quite often. This checking interval default is 60 seconds, which is enough for most applications, but you can always overwrite it if you need to.

User targeting

Targeting users comes in handy when you want to make your feature available only to a specific audience. Rather than making complicated checks in your code, you can get the flag's value along with the users that it targets. Here's an example of a user object which could be passed as a parameter to the getValue function:

var userObject = {
  identifier: "1234",
  email: "alex@f1.com",
  country: "Indonesia",
  custom: {
    subscriptionType: "Pro",
    userRole: "Administrator",
  },
};
Enter fullscreen mode Exit fullscreen mode

Clean code

As you can see in the code example above, I've chosen to store the user options in an object. Passing a function parameter instead of a long object will improve readability.

Similarly, you can apply this concept when fetching your flag's value. Let's say you want to do something when the feature flag value changes. Rather than writing all of the code that needs to be executed inside getValue, you can move it into another function.

configCatClient.getValue("isMyFirstFeatureEnabled", false, (flagValue) => {
  if (flagValue) {
    doSomething();
  } else {
    doSomethingElse();
  }
});
Enter fullscreen mode Exit fullscreen mode

Conclusion

Writing clean code, flags that follow a naming scheme, and proper configuration can go a long way to make your work more effective. The tips provided here are just suggestions and they are by no means mandatory. They aim to make life a bit easier for everyone working with feature flags in their project.

You can check out ConfigCat on LinkedIn, Facebook, and Twitter pages.

Top comments (0)