In our team we started with the smallest possible shareable system - a flags namespace in our front-end code. We later adopted an existing open source library in the back end. This got me thinking - what is the smallest possible feature flag system?
Here's a Hello World example...
# greet.py print("Hello, world!")
Let's test it:
python -m greet Hello, world!
I have a great idea for a new flagship feature - the script will ask the user's name and greet them personally!
What's the easiest way to add feature flags to a python script? For demonstration purposes we will just add a command line argument but this could be done with an environment variable or by looking at the user id or configuration. We put the flag-specific code inside an if-statement and keep common code outside the flagged block:
# greet1.py import sys personal_flag = "personal" in sys.argv if personal_flag: greeted = input("What's your name? ") else: greeted = "World" print(f"Hello, {greeted}!")
Let's run this new feature up the flagpole and see who salutes:
> python -m greet1 Hello, world! > python -m greet1 personal What's your name? Graham Hello, Graham!
I love this feature, the team loves it - we have tested it in production by activating the flag and our users love it too. Now it's time to remove the flag and tidy up the code:
# greet2.py greeted = input("What's your name? ") print(f"Hello, {greeted}!")
That's much cleaner - glad I removed the feature flag! Testing it...
> python -m greet2 What's your name? Graham Hello, Graham!
Perfect!