This question is not on how to actually write such feature flag per-se. But mostly to seek out for recommendations or hints on such system.
The idea is simple:
In order to progressively roll out/in features, we need to fork behaviors.
To do so, we create a
featureDescriptionFlag
object which contains arrays of strings. If it exist returntrue
, otherwisefalse
But, also, has to consider:
- If no flag object exists, returns
false
as in no flip has ben triggered - The ideal condition is to eventually remove all flags
I am refering to feature flag, concepts as described here:
The question
My main concerns on my implementation are the following:
- Using a global object to describe the flag feels wrong
- When a conditional statement exists, should we cache the outcome for subsequent calls (e.g. executes true first, then somewhere edits the
featureDescriptionFlag
, then next calls returns false)
Implementation example
This is the first implementation I came up with (see also on jsfiddle):
// WARNING: Requires underscore.js
function featureFlag(flagContainerName, elementKeyToCheck) {
//console.log('Feature flagging on '+flagContainerName+', with ' + elementKeyToCheck); // DEBUG
if (_.has(window.featureFlagDescriptor||{}, flagContainerName)) {
if(_.contains(window.featureFlagDescriptor[flagContainerName]||[], elementKeyToCheck)) {
//console.log('Disabled by '+flagContainerName+', matching ' + elementKeyToCheck); // DEBUG
return true;
}
}
return false;
}
Then, a flag description object that we create global
window.featureFlagDescriptor = {someFunction: ['example']};
My concern here is that It may be dangerous to allow such flip, I have alarms in my mind on how this could be exploited.
Later, in a function that has to have two behaviors, we do:
function someFunction(){
// Code...
if(featureFlag('someFunction', 'example')) {
// Work only if flag exist
} else {
// Former behavior
}
}
Conclusion
Any recommendations, improvement proposals?
I have created a jsfiddle on the subject and will eventually make a full working sample.