DEV Community

loading...

Discussion on: A Case Against Switches

Collapse
codevault profile image
Sergiu Mureşan

Switches, as you said, are an ugly and poorly implemented concept. This object to switch trick is fairly useful in cases you need lots of cases to check for.

Call me crazy but, most of the time, I simply use a long else if chain when needing a switch. It feels natural, it's much more readable than the object one and it works in any language.

All in all, very useful post!

Collapse
darkain profile image
Vincent Milum Jr

Some language implementations have a limit to the number of "else if" statements you're allowed to use though.

Collapse
codevault profile image
Sergiu Mureşan

Interesting. I never knew that. What languages have this limitation?

Thread Thread
darkain profile image
Vincent Milum Jr

It isn't language specific, it is implemention specific. I know some C/C++ compilers have this issue, but not all.

Thread Thread
codevault profile image
Sergiu Mureşan

For embedded I can see that limitation and that switch could look much more readable since you're mostly working with numbers (integers, addresses etc.) instead of more complex structures (objects, strings etc.)