Reading Nested Branches and Guards
Nested conditions are useful when a later decision assumes that an earlier requirement was met.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace the dependency
A cancellation policy can first check that a booking exists and then compare its date against a deadline. The deadline comparison relies on a retrieved booking. The same dependency can be expressed with an earlier guard returning a missing-booking result.
Try two failing examples
An absent booking should identify the missing record. A booking found past its deadline should identify the deadline. If both give the same message, inspect whether the tests are combined into a single result. Use input and output observations to decide which difference the user needs to see.[1][2]
Draw the facts available at each inner decision
A cancellation flow can first locate a reservation, then compare its scheduled date with a cutoff, then decide how to refund it. The date comparison assumes a real reservation. The refund calculation additionally relies on the cancellation being on time. Put those facts alongside each branch in a trace. A path beginning with a missing reservation should never attempt a date comparison. See guard clauses for moving that prerequisite to an early return.
Check outcome balance when rearranging branches
A nested structure can group logic requiring one shared prerequisite, such as calculations that all depend on the same loaded record. A guard sequence can make the successful path easier to read when several failures simply return. Both structures should produce the same decisions and effects under the intended behavior.
Test an absent record, an expired record, and one permitting a refund. For the failure cases, assert that no refund-related state changed. Include old timestamps on the exact cutoff. Postconditions help keep the unchanged records and returned reasons explicit when reviewing a refactor.