{"id":312675,"date":"2020-06-25T07:59:46","date_gmt":"2020-06-25T14:59:46","guid":{"rendered":"https:\/\/css-tricks.com\/?p=312675"},"modified":"2020-06-25T09:49:57","modified_gmt":"2020-06-25T16:49:57","slug":"how-to-disable-code-the-developers-production-kill-switch","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/how-to-disable-code-the-developers-production-kill-switch\/","title":{"rendered":"How to Disable Code: The Developer\u2019s Production Kill Switch"},"content":{"rendered":"\n

The following is a guest post written by Carlos Schults.<\/p>\n\n\n\n

Being able to disable code in production is a power that many developers aren\u2019t aware of. And that\u2019s a shame. The ability to switch off some portions\u2014or even complete features\u2014of the codebase can dramatically improve the software development process by allowing best practices that can shorten feedback cycles and increase the overall quality.<\/p>\n\n\n\n

So, that\u2019s what this post will cover: the mechanisms you can use to perform this switching off, why they\u2019re useful and how to get started. Let\u2019s dig in.<\/p>\n\n\n\n\n\n\n

Why Would You Want to Disable Code?<\/h3>\n\n\n

Before we take a deep dive into feature flags, explaining what they are and how they\u2019re implemented, you might be asking: Why would people want to switch off some parts of their codebase? What\u2019s the benefit of doing that?<\/p>\n\n\n\n

To answer these questions, we need to go back in time to take a look at how software was developed a couple of decades ago. Time for a history lesson!<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n

The Dark Ages: Integration Hell<\/h4>\n\n\n

Historically, integration has been one of the toughest challenges for teams trying to develop software together. <\/p>\n\n\n\n

Picture several teams inside an organization, working separately for several months, each one developing its own feature. While the teams were working in complete isolation, their versions of the application were evolving in different directions. Now they need to converge again into a single, non conflicting version. This is a Herculean task. <\/p>\n\n\n\n

That\u2019s what \u201cintegration hell<\/a>\u201d means: the struggle to merge versions of the same application that have been allowed to diverge for too long. <\/p>\n\n\n

Enter the Solution: Continuous Integration<\/h4>\n\n\n

\u201cIf it hurts, do it more often.\u201d What this saying means is that there are problems we postpone solving because doing so is hard. What you often find with these kinds of problems is that solving them more frequently, before they accumulate, is way less painful\u2014or even trivial.<\/p>\n\n\n\n

So, how can you make integrations less painful? Integrate more often.<\/p>\n\n\n\n

That\u2019s continuous integration (CI) in a nutshell: Have your developers integrate their work with a public shared repository, at the very least<\/em> once a day. Have a server trigger a build and run the automated test suite every time someone integrates their work. That way, if there are problems, they\u2019re exposed sooner rather than later.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n

How to Handle Partially Completed Features<\/h4>\n\n\n

One challenge that many teams struggle with in CI is how to deal with features that aren\u2019t complete. If developers are merging their code to the mainline, that means that any developments that take more than one day to complete will have to be split into several parts. <\/p>\n\n\n\n

How can you avoid the customer accessing unfinished functionality? There are some trivial scenarios with similarly trivial solutions, but harder scenarios call for a different approach: the ability to switch off a part of the code completely.<\/p>\n\n\n

Feature Flags to the Rescue<\/h3>\n\n

Defining Feature Flags<\/h4>\n\n\n

There are many names for the mechanisms that allow developers to switch a portion of their code off and on. Some call them \u201cfeature toggles\u201d or \u201ckill switches.\u201d But \u201cfeature flags\u201d is the most popular name, so that\u2019s what we\u2019ll use for the remainder of this post. So, what are feature flags?<\/p>\n\n\n\n

Put simply, feature flags are techniques that allow teams to change the behavior of an application without modifying the code. In general, flags are used to prevent users from accessing and using the changes introduced by some piece of code, because they\u2019re not adequate for production yet for a number of reasons.<\/p>\n\n\n

Disable Code: What Are the Use Cases?<\/h4>\n\n\n

We\u2019ll now cover some of the most common use cases for disabling code in production.<\/p>\n\n\n

Switching Off Unfinished Features<\/h5>\n\n\n

As you\u2019ve seen, one of the main use cases for feature flags is preventing users from accessing features that aren\u2019t ready for use yet.<\/p>\n\n\n\n

That way, programmers developing features that are more complex and take a longer time to complete aren\u2019t prevented from integrating their work often and benefiting from it.<\/p>\n\n\n

Enabling A\/B Testing<\/h5>\n\n\n

The adoption of feature flags enables the use of several valuable practices in the software development process, one of which is A\/B testing<\/u><\/a>. <\/p>\n\n\n\n

A\/B testing is a user experience research technique that consists of comparing two versions of a website or application to decide which one to keep. It entails randomly splitting users into two groups, A and B, and then delivering a different version of the application to each group. One group might receive the current production version, which we call the \u201ccontrol,\u201d whereas the second group would receive the candidate for the new version, called the \u201ctreatment.\u201d <\/p>\n\n\n\n

The testers then monitor the behavior of both groups and determine which of the versions achieved better results. <\/p>\n\n\n\n

Feature flags are a practical way to enable A\/B testing because they allow you to quickly and conveniently change between the control and treatment versions of your application.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n
Enabling Canary Releases<\/h5>\n\n\n

If you deliver the new version of your app to your entire userbase at once, 100 percent of your users will be impacted if the release is bad in some way. What if you could gradually roll out the new version instead? You\u2019d first deploy to a small subset of users, monitoring that group to detect issues. If something went wrong, you could roll it back. If everything looked fine, you could then gradually release the version for larger groups. That\u2019s a canary release<\/u><\/a> in a nutshell. It\u2019s another powerful technique that feature flags might help with.<\/p>\n\n\n

Customizing Features According to Users\u2019 Preferences<\/h5>\n\n\n

It\u2019s not uncommon to have to customize your application according to the needs of specific users, and there are several ways in which software teams can accomplish that\u2014some more efficient, and others less so (companies that create separate branches or entire repositories for each client come to mind).

This is another area where feature flags could help, allowing teams to dynamically switch between different versions of the same functionality.<\/p>\n\n\n

Disable Code in Production 101<\/h3>\n\n\n

How do you go about disabling code? That\u2019s what we\u2019re going to see now, in three increasingly sophisticated phases.<\/p>\n\n\n

First Stage: The Most Basic Approach<\/h4>\n\n\n

We start with an approach that\u2019s so primitive, it maybe shouldn\u2019t be considered a feature flag at all. Consider the pseudocode below:<\/p>\n\n\n\n

calculateAdditionalWorkHours(Employee employee, Date start, Date end) {\n  \/\/ return calculateAdditionalWorkHoursSameOldWay(employee, start, end);\n  return calculateAdditionalWorkHoursImproved(employee, start, end);\n}<\/code><\/pre>\n\n\n\n

In the code above, we\u2019re just commenting out the old version of some method and replacing it with a new version. When we want the older version to be used, we just do the opposite. (Well, I said it was primitive.) This approach lacks one of the most fundamental properties of a feature flag\u2014the ability to change how the application behaves without changing its code.<\/p>\n\n\n\n

However, it plants the seed for more sophisticated approaches.<\/p>\n\n\n

Second Stage: Taking the Decision Out of the Code<\/h4>\n\n\n

The previous approach didn\u2019t allow developers to select the desired version of the feature without changing the code. Fortunately, that\u2019s not so hard to do. First, we introduce a logical variable to determine which version we\u2019re going to use:<\/p>\n\n\n\n

calculateAdditionalWorkHours(Employee employee, Date start, Date end) {\n\n  var result = useNewCalculation\n    ? calculateAdditionalWorkHoursImproved(employee, start, end)\n    : calculateAdditionalWorkHoursSameOldWay(employee, start, end);\n\n  return result;\n}<\/code><\/pre>\n\n\n\n

Then, we use some mechanism to be able to assign the value to the variable from an external source. We could use a configuration file:<\/p>\n\n\n\n

var useNewCalculation = config[newCalculation];<\/code><\/pre>\n\n\n\n

Passing arguments to the application might be another option. What matters is that we now have the ability to modify how the app behaves from the outside, which is a great step toward \u201ctrue\u201d feature flagging.<\/p>\n\n\n\n

Keep in mind that the code examples you see are all pseudocode. Using your favorite programming language, there\u2019s nothing stopping you from starting with this approach and taking it up a notch. You could, for instance, use classes to represent the features and design patterns (e.g., factories) to avoid if statements.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n

Stage 3: Full-Fledged Feature Flag Management<\/h4>\n\n\n

The previous approach might be enough when your application has only a small number of flags. But as that number grows, things start to become messy.<\/p>\n\n\n\n

First, you have the issue of technical debt. Manually implemented feature flags can create terribly confusing conditional flows in your codebase. That only grows worse with new flags being introduced each day. Additionally, they might make the code harder to understand and navigate, especially for more junior developers, which is an invitation for bugs.<\/p>\n\n\n\n

Another problem is that as the number of flags grows, it becomes more and more common to forget to delete old, obsolete ones.<\/p>\n\n\n\n

The main problem of a homegrown approach is that it doesn\u2019t give you an easy way to see and manage all of your flags at once. That\u2019s why our third and final stage is a single piece of advice: Instead of rolling out your own feature flags approach, adopt a third-party feature flag management system<\/a>.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n

Feature Flags Are a CI\/CD Enabler<\/h3>\n\n\n

We\u2019ve covered the mechanisms developers can use to disable portions of their codebase in production without having to touch the code. This capability is powerful and enables techniques such as A\/B testing and canary releases, which are all hallmarks of a modern, agile-based software development process.<\/p>\n\n\n\n

The names for the techniques might vary\u2014feature flags, feature toggles, feature flipper, and so on. The way in which the techniques are implemented can also vary\u2014from a humble if statement to sophisticated cloud-based solutions.<\/p>\n\n\n\n

But no matter what you call them, you can\u2019t overstate the benefit these mechanisms offer. They\u2019re an enabler of Continuous Integration, which is essential for any modern software organization that wants to stay afloat.<\/p>\n","protected":false},"excerpt":{"rendered":"

The following is a guest post written by Carlos Schults. Being able to disable code in production is a power that many developers aren\u2019t aware of. And that\u2019s a shame. The ability to switch off some portions\u2014or even complete features\u2014of the codebase can dramatically improve the software development process by allowing best practices that can […]<\/p>\n","protected":false},"author":2508,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[508],"tags":[],"jetpack_publicize_connections":[],"acf":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":262483,"url":"https:\/\/css-tricks.com\/how-to-disable-links\/","url_meta":{"origin":312675,"position":0},"title":"How to Disable Links","date":"November 17, 2017","format":false,"excerpt":"The topic of disabling links popped up at my work the other day. Somehow, a \"disabled\" anchor style was added to our typography styles last year when I wasn't looking. There is a problem though: there is no real way to disable an link (with a valid href attribute) in\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":340343,"url":"https:\/\/css-tricks.com\/creating-stylesheet-feature-flags-with-sass-default\/","url_meta":{"origin":312675,"position":1},"title":"Creating Stylesheet Feature Flags With Sass !default","date":"May 14, 2021","format":false,"excerpt":"!default is a Sass flag that indicates conditional assignment to a variable\u2009\u2014\u2009it assigns a value only if the variable was previously undefined or null. Consider this code snippet: $variable: 'test' !default; To the Sass compiler, this line says: Assign $variable to value 'test', but only if $variable is not already\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/05\/sass-default.jpg?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":275719,"url":"https:\/\/css-tricks.com\/new-mobile-chrome-feature-would-disable-scripts-on-slow-connections\/","url_meta":{"origin":312675,"position":2},"title":"New mobile Chrome feature would disable scripts on slow connections","date":"August 31, 2018","format":false,"excerpt":"This is a possible upcoming feature for mobile Chrome: If a Data Saver user is on a 2G-speed or slower network according to the NetInfo API, Chrome disables scripts and sends an intervention header on every resource request. Users are shown a UI at the bottom of the screen indicating\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2018\/08\/chrome-brackets.jpg?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":373100,"url":"https:\/\/css-tricks.com\/when-is-it-ok-to-disable-text-selection\/","url_meta":{"origin":312675,"position":3},"title":"When is it OK to Disable Text Selection?","date":"September 14, 2022","format":false,"excerpt":"Using CSS, it\u2019s possible to prevent users from selecting text within an element using user-select: none. Now, it\u2019s understandable why doing so might be considered \u201ccontroversial\u201d. I mean, should we be disabling standard user behaviors? Generally speaking, no, we shouldn\u2019t be doing that. But does disabling text selection have some\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/10\/browser-text-selection.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":286449,"url":"https:\/\/css-tricks.com\/using-parcel-as-a-bundler-for-react-applications\/","url_meta":{"origin":312675,"position":4},"title":"Using Parcel as a Bundler for React Applications","date":"April 25, 2019","format":false,"excerpt":"You may already be familiar with webpack for asset management on projects. However, there\u2019s another cool tool out there called Parcel, which is comparable to webpack in that it helps with hassle-free asset bundling. Where Parcel really shines is that it requires zero configuration to get up and running, where\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/04\/parcel-react.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":296585,"url":"https:\/\/css-tricks.com\/stop-animations-during-window-resizing\/","url_meta":{"origin":312675,"position":5},"title":"Stop Animations During Window Resizing","date":"October 14, 2019","format":false,"excerpt":"Say you have page that has a bunch of transitions and animations on all sorts of elements. Some of them get triggered when the window is resized because they have to do with size of the page or position or padding or something. It doesn't really matter what it is,\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/09\/browser-resize.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]}],"featured_media_src_url":null,"_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/312675"}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/2508"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=312675"}],"version-history":[{"count":4,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/312675\/revisions"}],"predecessor-version":[{"id":312758,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/312675\/revisions\/312758"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=312675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/categories?post=312675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=312675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}