ALL-BRAN0001 · value_dispatch_candidate
Count condition chains that only select behavior by one value.
This is a deterministic rule for all languages. Read its implementation.
Definition
Section titled “Definition”Report a chain of minimum_arms or more arms where every arm compares the same subject against
a distinct literal and reads nothing else. Such a chain is a lookup written as control flow.
Every new case edits the same function, which is exactly the change a dispatch table, a match
over a closed type, or a registry avoids. The rule reports the chain, not the individual arms,
because the chain is what gets replaced.
Evidence
Section titled “Evidence”Each finding records the chain range, the subject, every literal it tests, and whether a fallback arm exists. The value is the number of chains.
Exceptions
Section titled “Exceptions”A chain whose arms test different subjects, compare with anything other than equality, or read additional state is real branching logic and is not counted. A chain of two arms is left alone because a table costs more than it saves at that size.
Examples
Section titled “Examples”Three arms testing kind == "pbs", kind == "slurm", and kind == "ssh", with or without a
fallback beneath them, return 1 and should become a registry or a mapping. A chain testing
kind == "pbs" and then queue.is_full returns 0, because its second arm reads something
else. A chain testing kind == "pbs" twice returns 0 as well, since two arms share one
literal.
References
Section titled “References”- Cites “Refactoring”, replace conditional with polymorphism
- Cites Clippy match_like_matches_macro
- Cites Clippy comparison_chain. Open reference
- Cites “The Python Standard Library”,
functools.singledispatchand structural pattern matching. Open reference