How a conditional in code can cause combinatorial scenarios ! And how can unit tests can help !
Lets say we have a little beautiful code like this:
def little_function():
do_something_a()
But then some lovely PO tell us that method do_something_a should be called; if not we return nothing. So we do the change.
def little_function(new_condition):
if new_condition:
do_something_a()
return None
After several weeks we realize that the condition was wrong and we actually need to call another method do_something_b() only if some property contains the prefix '000'.
So we do the change:
def little_function(new_condition):
if new_condition:
if new_condition.prefix != '' and '000' in new_condition.prefix:
do_something_b()
else:
do_something_a()
return None
So we think; we are done but at last minute your UX asked that please you should validate that last digit of prefix in condition is 'X':
def little_function(new_condition):
if new_condition:
prefix = new_condition.prefix
if prefix != '':
if '000' in prefix and prefix[-1]='X':
do_something_b()
else:
do_something_a()
else:
raise Exception('Prefix cannot be Empty')
return None
Everything looks fine and we can go home. But this is a combinatorial bomb time. As we have innocently and un-intentational added several possible scenarios that are far from being detected by manual testing.
Lets say that I enumerate all possible combinations in a table and pass it to QA proactively. But what will happen when i add a new condition or just an "OR" condition that opens more combinations. Can QA still manage all combinations in their head when testing manually ?
I dont think it is possible as our memory slots in our head are limited too.
So what can we do ?
One obvious solution is to review the code and check why do we have so many conditionals and also apply the S in SOLID principles or just refactor the code in several bits so we dont have all conditions in one method.
But the main solution here is to do proper unit testing.
Why ? Because unit tests are the best way to check these combinations through code and also we can prevent that if some conditions are changed. Tests will warn us and let us know that code has been changed and we need to fix code or as a last resource adapt the tests. Also if some new conditions are added we can add/update our tests to check the new combination.
So dont let your code run naked in the street and dress them up with proper unit tests.