I’m asked to refactor my component code to support A/B Testing.
What is the better approach:
1) Pass a boolean value to the methods and check for that flag inside the method body?
method(flag abTest):
if A_VERSION_ENABLED:
// Do 'A' logic
else
// Do 'B' logic
2) Create dedicated methods for each version?
codeForATest()
codeForBTest()
1
You should look into the Strategy Pattern. It allows you to swap out the behavior of a function or object on the fly by using interfaces. You specify how a method will be called, then have two or more versions that implement that interface. Then, you don’t care about which one you’re using in the code except upon initialization.