DEV Community

Discussion on: Avoid multiple ElIf situation

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

I still don't really understand why elif or else if smells, when dictionary with functions just make the code more complicated.

Danger 1 - Dictionary with non-function gets compiled
Danger 2 - not in Python. Fall through in switch case statement

Collapse
 
jhermann profile image
Jürgen Hermann • Edited

Because the number of code paths is reduced, and the intent of the code is way more clearer (get me a parser for this format). The code is actually simpler, because less chance for the unexpected.

Complexity is not just what is, but what could be.

Collapse
 
c_v_ya profile image
Constantine • Edited

I have nothing against elifs, it's a powerful and useful tool. And I use it a lot in my projects. But when I see 4-5 or more of them - Idk, it makes me cringe. Dictionary can help with that giving you a neat structure of what to do when you have this condition.