3 ms·
Kinda late but I want to point out these snippets have different behaviors: if A and B: .. elif B: .. vs if A and B: ..
by wting 11y ago
Kinda late but I want to point out these snippets have different behaviors:
if A and B:
..
elif B:
..
vs
if A and B:
..
elif A and not B:
..
I prefer flattening because enumeration makes it obvious what's different between the branches despite the code duplication.
For example, three boolean conditions has 2^3 = 8 possible combinations. When nested this complexity is hidden but obviously apparent as a code smell when flattened.
It also echoes Python's ethos that flat is better than nested.