If Chains and Independent Conditions
An if-elif chain chooses the first matching branch. Two independent checks can both run for one input.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace the overlapping case
Suppose a parcel over ten kilograms gets a heavy label and a fragile parcel gets a fragile label. A heavy fragile parcel needs both labels, so use two independent tests. The same input triggers at most one label under an if-elif chain. Reading branch structure reveals which outcomes are possible.
Choose deliberate precedence
A calculator may award one delivery discount, prioritizing a staff discount over a student one. Place staff first and test a person eligible for both. The expected single result documents overlapping predicates and their intended precedence.[1][2]
Check the case where two conditions are true
A parcel can be both heavy and fragile. If both labels are required, two independent conditions can emit both. In a chain using an exclusive alternative, the first true branch can emit only one of those labels. Construct the four input combinations and verify each emitted set. Testing only heavy or only fragile would make both implementations look compatible. A truth table makes the overlapping row explicit.
Use precedence when the outcome really is exclusive
Suppose a passenger can qualify for a staff discount or a youth discount, and the policy permits just one discount. If staff takes priority, a chain can test staff first. Check somebody eligible for both and confirm the staff result appears only once. An unrelated required fee can be applied separately regardless of which discount was chosen.
Keep the returned decision and the associated state change distinguishable in tests. A branch may calculate a correct amount and still emit a duplicate discount record if separate code also runs. Define which effects constitute one result in the function contract.