Designing complex if...elif chains
In most cases, our scripts will involve a number of choices. Sometimes the choices are simple, and we can judge the quality of the design with a glance at the code. In other cases, the choices are more complex, and it's not easy to determine whether or not our if statements are designed properly to handle all of the conditions.
In the simplest case, we have one condition, C, and its inverse, C . These are the two conditions for an if...else
statement. One condition, ¬C, is stated in the if
clause, the other is implied in the else
.
We'll use p ∨ q to mean Python's OR operator in this explanation. We can call these two conditions complete because:
C ∨ C = ¬T
We call this complete because no other conditions can exist. There's no third choice. This is the Law of the Excluded Middle. It's also the operating principle behind the else
clause. The if
statement body is executed or the else
statement is executed. There's no third choice.
In practical programming, we...