Creating a boolean flag

4 minute read

Boolean flags, which can only be configured to true or false, are central to CloudBees Feature Management. The default flag value, which is the flag value when configuration is disabled, is the value specified in the code. If no value is specified in your code, the default boolean flag value is false. If configuration is enabled, the boolean flag value can be set in the UI. The boolean flag name is derived from the flag variable name.

Creating a boolean flag in the UI

To create a boolean flag in the UI, select the Boolean flag type when you create your flag. Refer to Creating feature flags for more information.

Creating a container class and defining a feature flag

You can create boolean flags in your code using the static API. A container class is required, and you define your flag inside the container class.

In the following examples, a container class and a boolean flag are defined. The flag name, videoChat, is derived from the flag variable name. This flag is false by default.

Swift
Objective-c
Android
React Native
JavaScript
Node.js
JavaScript SSR
JVM
Kotlin
.NET
Python
Go
Ruby
PHP
C
C Client Side
C++
C++ Client Side
import Foundation
import ROX
public class Flags: RoxContainer{
  public let videoChat = RoxFlag();
}
#import <ROXCore/ROXCore.h>

@interface Flags : ROXBaseContainer

@property (nonatomic) ROXFlag* videoChat;

@end


#import "Flags.h"

@implementation Flags

- (instancetype)init {
  self = [super init];
  if (self) {
    self.videoChat = [[ROXFlag alloc] init];
  }
  return self;
}

@end
public class Flags implements RoxContainer{
  public RoxFlag videoChat = new RoxFlag();
}
const flags = {
  videoChat: new Rox.Flag()
};
const flags = {
  videoChat: new Rox.Flag()
};
const flags = {
  videoChat: new Rox.Flag()
};
import {Flag} from 'rox-ssr';

const flags = {
  videoChat: new Flag()
};
public class Flags implements RoxContainer{
  public RoxFlag videoChat = new RoxFlag();
}
import kotlin.jvm.JvmField

class Flags: RoxContainer {
    @JvmField
  val videoChat = RoxFlag()
}
public class Flags : IRoxContainer
{
  public RoxFlag videoChat = new RoxFlag();
}
from rox.server.flags.rox_flag import RoxFlag

class MyContainer:
     def __init__(self):
        self.video_chat = RoxFlag()
import (
     "github.com/rollout/rox-go/v5/server"
)

type Container struct {
     EnableTutorial server.RoxFlag
}

var flags = &Container {
     // Default value must be specified with go
   EnableTutorial: server.NewRoxFlag(false),
}
require 'rox/server/rox_server'

class Container
    attr_accessor :video_chat

  def initialize
    @video_chat = Rox::Server::RoxFlag.new
  end
end
use Rox\Server\Rox;
use Rox\Server\Flags\RoxFlag;
require __DIR__ . '/vendor/autoload.php';

class Container
{
    public $videoChat;

    public function __construct()
    {
        $this->videoChat = new RoxFlag(false);
    }
}
RoxStringBase *videoChat = rox_add_flag("demo.video_chat", false);
RoxStringBase *videoChat = rox_add_flag_with_freeze("demo.video_chat", false, RoxFreezeUntilLaunch);
auto videoChat = Rox::Flag::Create("demo.video_chat", false);
auto videoChat = Rox::Client::Flag::Create("demo.video_chat", false, RoxFreezeUntilLaunch);

Registering the container class

To use the container class, register its instance to the CloudBees Feature Management SDK with the Rox.register() SDK function.

The namespace parameter is a logical separation between containers, and can be used to find flags on the dashboard.

A namespace can only be registered once.

In the following examples, the Rox.register() SDK function is called to register the class instance:

Swift
Objective-C
Android
React Native
JavaScript
Node.js
JavaScript SSR
JVM
.NET
Python
Go
Ruby
PHP
C
C Client Side
C++
C++ Client Side
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

  ROX.register("<namespace>", container: Flags())
  ROX.setup(withKey: YOUR_APP_KEY)
  return true
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [ROX register:[[Flags alloc] init]];
    [ROX setupWithKey:@<YOUR_APP_KEY>];

    return YES;
}
public class App extends Application {

  @Override
  public void onCreate() {
    super.onCreate();

    Flags flags = new Flags();
    Rox.register("<namespace>", flags);

    Rox.setup(this);

    // This can also be set up in MainActivity if your app doesn't extend the Application class
  }
}
const flags = {
  // define Rox entities here
}
Rox.register('<namespace>', flags);
await Rox.setup('<YOUR_APP_KEY>');
const flags = {
  // define Rox entities here
}
Rox.register('<namespace>', flags);
Rox.setup('<YOUR_APP_KEY>');
const flags = {
  // define Rox entities here
}
Rox.register('<namespace>', flags);
await Rox.setup('<YOUR_APP_KEY>');
import {Rox} from 'rox-ssr';

const flags = {
  // define Rox entities here
}
Rox.register('<namespace>', flags);
Rox.setup('<YOUR_APP_KEY>');
Flags flags = new Flags();
Rox.register("<namespace>", flags);
Rox.setup(YOUR_APP_KEY);
Container flags = new Container();
Rox.Register("<namespace>", flags);
await Rox.Setup(<YOUR_APP_KEY>);
from rox.server.rox_server import Rox

flags = MyContainer()
Rox.register(<namespace>, flags)
cancel_event = Rox.setup('<YOUR_APP_KEY>').result()
package main
import "github.com/rollout/rox-go/v5/server"

type Container struct {
     EnableTutorial server.RoxFlag
}

var flags = &Container {
     // Default value must be specified with go
   EnableTutorial: server.NewRoxFlag(false),
}
var rox *server.Rox

func main() {
  options := server.NewRoxOptions(server.RoxOptionsBuilder {})

  rox = server.NewRox()
  rox.Register("<namespace>", flags)
    <-rox.Setup("<YOUR_APP_KEY>", options)
}
require 'rox/server/rox_server'

class Container
end

flags = Container.new

Rox::Server::RoxServer.register(<namespace>, flags)
Rox::Server::RoxServer.setup(<YOUR_APP_KEY>).join
use Rox\Server\Flags\RoxFlag;
use Rox\Server\Rox;

class Container
{
    public $videoChat;

    public function __construct()
    {
        $this->videoChat = new RoxFlag();
    }
}

$container = new Container();
Rox::register(<namespace>, $container);
Rox::setup(<YOUR_APP_KEY>);
#include <rox/server.h>

#define DEFAULT_API_KEY "YOUR_APP_KEY"

int main(int argc, char **argv) {
    RoxStringBase *videoChat = rox_add_flag("demo.video_chat", false);
    rox_setup(DEFAULT_API_KEY, NULL);
    rox_shutdown();
}
#include <rox/client.h>

#define DEFAULT_API_KEY "YOUR_APP_KEY"

int main(int argc, char **argv) {
    RoxStringBase *videoChat = rox_add_flag_with_freeze("demo.video_chat", false, RoxFreezeUntilLaunch);
    rox_setup(DEFAULT_API_KEY, NULL);
    rox_shutdown();
}
#include <roxx/server.h>

#define DEFAULT_API_KEY "YOUR_APP_KEY"

using namespace Rox;

int main(int argc, char **argv) {
    auto videoChat = Rox::Flag::Create("demo.video_chat", false);
    Rox::Setup(DEFAULT_API_KEY, NULL);
    Rox::Shutdown();
}
#include <roxx/client.h>

#define DEFAULT_API_KEY "YOUR_APP_KEY"

using namespace Rox::Client;

int main(int argc, char **argv) {
    auto videoChat = Rox::Client::Flag::Create("demo.video_chat", false, RoxFreezeUntilLaunch);
    Rox::Setup(DEFAULT_API_KEY, NULL);
    Rox::Shutdown();
}

Using a boolean flag

The following examples check the flag value:

Swift
Objective-C
Android
React Native
JavaScript
Node.js
JavaScript SSR
JVM
.NET
Python
Go
Ruby
PHP
C
C++
flags.videoChat.enabled {
  print("VideoChat Feature is Enabled")
}
[myContainer.videoChat enabled:^{
  NSLog(@"VideoChat Feature is Enabled");
} disabled:^{
  // ...
}];
if (flags.videoChat.isEnabled()) {
  Log.i(tag, "VideoChat Feature is Enabled");
}
if (flags.videoChat.isEnabled()) {
  console.log('videoChat is enabled');
}
if (flags.videoChat.isEnabled()) {
  console.log('videoChat is enabled');
}
if (flags.videoChat.isEnabled()) {
  console.log('videoChat is enabled');
}
if (flags.videoChat.isEnabled()) {
  console.log('videoChat is enabled');
}
if (flags.videoChat.isEnabled()) {
  Logger.info(tag, "VideoChat Feature is Enabled");
}
if (flags.videoChat.isEnabled() == true)
{
  // videoChat is enabled
}
if my_container.video_chat.is_enabled():
    print('video_chat is enabled')
if (flags.VideoChat.IsEnabled(nil)) {
  // ...
}
if container.video_chat.enabled?
  puts "video_chat is enabled"
end
if ($container->videoChat->isEnabled()) {
    echo 'video chat is enabled';
}
if (rox_is_enabled(videoChat)) {
    // ...
}
if (videoChat->IsEnabled()) {
    // ...
}

Default flag values

A boolean flag is false by default. To override the value, pass true to the flag.

Flag freeze is available on client-side SDKs by default.

  • For SDK version 4.x, the default is set to untilForeground.

  • For SDK version 5.0 or later, the default is set to none.

Refer to Understanding a flag freeze for more information.