Skip to content

Instantly share code, notes, and snippets.

@bennadel
Last active February 9, 2020 15:10
My Personal Best Practices For Using LaunchDarkly Feature Flags
<cfscript>
public void function someControllerMethod( rc ) {
if ( shouldUseProjectLoadTimeExperiment( rc.user.id ) ) {
// .... experimental code.
return;
}
// .... normal code.
}
// ---
// PRIVATE METHODS.
// ---
private boolean function shouldUseProjectLoadTimeExperiment( userID ) {
return( featureFlagService.getFeatureByUserID( userID, "performance-RAIN-1337-project-list-load-time" ) );
}
</cfscript>
app.controller(
"MyController",
function( $scope, config ) {
// ....
$scope.shouldShowHotNewFeature = !! config.featureFlags[ "product-RAIN-007-hot-new-feature" ];
// ....
}
);
<ul class="nav">
<li>
<a href="#/home">Home</a>
</li>
<li>
<a href="#/about">About</a>
</li>
<li>
<a href="#/contact">Contact</a>
</li>
<li ng-if="shouldShowHotNewFeature">
<a href="#/hawtness">Hawtness</a>
</li>
</ul>
render() {
return(
<Badge>
<Avatar
imageUrl={ this.user.imageUrl }
useLazyLoading={ this.featureFlags[ "performance-RAIN-123-lazy-loading-images" ] }
/>
{ this.user.name }
</Badge>
);
}
class Avatar {
private useLazyLoading: boolean;
constructor( featureFlags: FeatureFlags ) {
this.useLazyLoading = !! featureFlags[ "performance-RAIN-123-lazy-loading-images" ];
}
// ....
}
component {
public struct function doSomething() {
// ....
if ( shouldUseSqlExperiment( userID ) ) {
dataAccess.getDataWithOptimization( userID );
} else {
dataAccess.getData( userID );
}
// ....
}
// ---
// PRIVATE METHODS.
// ---
private boolean function shouldUseSqlExperiment( userID ) {
return( featureFlags.getFeatureByUserID( userID, "performance-RAIN-123-sql-optimization" ) );
}
}
component {
public struct function doSomething() {
// ....
if ( shouldUseSqlExperiment( userID ) ) {
dataAccess.getDataWithOptimization( userID );
} else {
dataAccess.getData( userID );
}
// ....
}
// ---
// PRIVATE METHODS.
// ---
private boolean function shouldUseSqlExperiment( userID ) {
return( featureFlags.getFeatureByUserID( userID, "performance-RAIN-123-sql-optimization" ) );
}
}
component {
public struct function doSomething() {
if ( shouldUseMoreRobustExperiment( userID ) ) {
return( doSomethingWithOptimization() );
}
// ....
}
public struct function doSomethingWithOptimization() {
// ....
}
// ---
// PRIVATE METHODS.
// ---
private boolean function shouldUseMoreRobustExperiment( userID ) {
return( featureFlags.getFeatureByUserID( userID, "performance-RAIN-123-more-robust-optimization" ) );
}
}
component {
public struct function doSomething() {
if ( shouldUseMoreRobustExperiment( userID ) ) {
return( doSomethingWithOptimization() );
}
// ....
}
public struct function doSomethingWithOptimization() {
// ....
}
// ---
// PRIVATE METHODS.
// ---
private boolean function shouldUseMoreRobustExperiment( userID ) {
return( featureFlags.getFeatureByUserID( userID, "performance-RAIN-123-more-robust-optimization" ) );
}
}
component {
public void function api_end_point( requestContext ) {
if ( shouldUseMoreRobustExperiment( userID ) ) {
requestContext.data = optimizedService.doSomething();
return;
}
requestContext.data = service.doSomething();
}
// ---
// PRIVATE METHODS.
// ---
private boolean function shouldUseMoreRobustExperiment( userID ) {
return( featureFlags.getFeatureByUserID( userID, "performance-RAIN-123-more-robust-optimization" ) );
}
}
component {
public void function api_end_point( requestContext ) {
if ( shouldUseMoreRobustExperiment( userID ) ) {
requestContext.data = optimizedService.doSomething();
return;
}
requestContext.data = service.doSomething();
}
// ---
// PRIVATE METHODS.
// ---
private boolean function shouldUseMoreRobustExperiment( userID ) {
return( featureFlags.getFeatureByUserID( userID, "performance-RAIN-123-more-robust-optimization" ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment