I have two boolean expressions, A
and B
. I have four distinct paths which I must handle:
A and B
A and !B
!A and B
!A and !B
What would be the best way to check and define all four paths associated with the booleans? There are a few options, but each one seems to have dis/advantages. For example, this one is more readable (IMO; code is fairly self-explana), but evaluates A
and B
multiple times, which can be detrimental to performance if they are expensive operations.
if A and B
# A and B
path1()
else if A and !B
# A and !B
path2()
else if !A and B
# !A and B
path3()
else
# !A and !B
path4()
The next one evaluates A
and B
once each, but it isn't as immediately apparent which path is being evaluated. It also doesn't have the advantages of short-circuit evaluation.
if A
if B
# A and B
path1()
else
# A and !B
path2()
else
if B
# !A and B
path3()
else
# !A and !B
path4()
Is there a best practice to implementing this? Perhaps something I hand't considered?
Aucun commentaire:
Enregistrer un commentaire