Welcome back!
In the previous chapter, Chapter 1: Feature Definition Stub, we created a "Stub"โa safety placeholder object that represents a feature that isn't ready yet.
Now, we are going to focus on the most critical part of that object: The Switch.
Imagine you are an electrician wiring a new house. You install a master switch for the lights. Even if you haven't screwed in the lightbulbs yet, you want to make sure the switch is set to OFF.
Why? Because if there is a loose wire or a problem, you don't want electricity flowing. You want a guarantee of safety.
The Use Case:
You are deploying a new feature, NewPaymentSystem, to your live application.
You need a Feature Toggling Interface that acts as a hard "Kill Switch." It guarantees that no matter what code attempts to run this feature, the answer is always "No".
The interface is simple. We use a standard method called isEnabled.
When you are writing code in your application, you should never run feature logic blindly. You must always ask for permission first.
Here is how you use the interface in your daily coding:
import feature from './index.js';
// 1. Ask the interface for permission
const canRun = feature.isEnabled();
// 2. Decide what to do
if (canRun) {
// This line will NEVER run in our current setup
runComplexLogic();
} else {
console.log("Safety mode: Logic skipped.");
}
Output:
Safety mode: Logic skipped.
By wrapping your code in this if statement, you have created a safety barrier. Because isEnabled() returns false, the dangerous or unfinished code inside the if block is completely ignored by the computer.
How does this safety mechanism work internally?
Think of a Security Guard standing in front of a VIP room.
Here is the flow of that conversation:
Let's look at index.js again to see how this specific "Kill Switch" is implemented.
// --- File: index.js ---
export default {
// This is the Feature Toggling Interface
// It is an Arrow Function that takes no arguments
// and immediately returns false.
isEnabled: () => false,
isHidden: true,
name: 'stub'
};
Explanation:
isEnabled: This is the name of our interface. Every feature in our system must have this method so the app knows how to check it.() => false: This is a JavaScript Arrow Function.() means it accepts no inputs.=> false means "return false immediately."It is hardcoded. There is no complex logic, no database lookup, and no API call. It is a permanent red light. This makes it incredibly fast and completely failsafe for features that should essentially be dead.
You have learned about the Feature Toggling Interface.
isEnabled) used to gatekeep logic.false return value.Now we have a feature that is safely turned off. But what about the user interface? Even if the logic doesn't run, we don't want a broken button appearing on the screen.
In the next chapter, we will learn how to control whether the feature is seen by the user.
Next Chapter: Visibility Configuration
Generated by Code IQ