What is a Toggle?
A toggle is a hardware or software switch that has two positions, on and off. It’s used to allow users to choose which functions they want to use. It is commonly found on keyboards with caps lock and numlock switches, for example. The word toggle is also a verb, meaning “to move or alternate between two things.” For example, you might use the term to describe switching between two screens when video calling with two friends at once.
In the context of software development, a toggle is a way to dynamically manage feature flag configuration at runtime. Toggles are typically used for experimental features that will be exposed to a subset of the user population. By using toggles we can test the effectiveness of different codepaths without having to modify a large percentage of our production codebase.
There are a few ways to implement a toggle, from the very simple through to more complex. The simplest approach is to hardcode the toggle into static files. This can work well at smaller scales but becomes cumbersome when a project grows larger. Having to edit these files on every server adds complexity and may impact CI/CD pipelines and cycle times.
An alternative to hardcoding toggles is to store them in some type of centralized database. This can provide advantages such as consistency across a fleet of servers and the ability to easily update configuration across the whole application. However, this is typically accompanied by an additional requirement of building out an admin UI that system operators, product managers and testers can use to view and modify Toggle Configuration.
One of the most common uses of Toggles in modern development practices is to perform multivariate or A/B testing. By using a toggle and a custom Router we can consistently send a user down one or the other codepath at runtime, allowing us to measure the effectiveness of different variations on things such as an ecommerce purchase flow or Call to Action wording.
A toggle can be anything from a single “if” statement to a more complex decision tree which acts upon many variables. The conditions triggering a toggle can come from many places in a codebase, including fitness test results, settings in a feature management tool or even a variable provided by a config file. The most important factor is that a toggle can be changed at runtime and that the change can be seen immediately by the user. Otherwise, it is not really a toggle. It would be more accurately described as a “Flag”.