Thank you for stopping by! I am a full-stack developer that combines the power of entrepreneurship and programming to make the lives of programmers easier.
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.
I've been coding for over 20 years now! (WOAH, do I feel old)
I've touched just about every resource imaginable under the Sun (too bad they were bought out by Oracle)
Thank you for stopping by! I am a full-stack developer that combines the power of entrepreneurship and programming to make the lives of programmers easier.
I've been coding for over 20 years now! (WOAH, do I feel old)
I've touched just about every resource imaginable under the Sun (too bad they were bought out by Oracle)
Thank you for stopping by! I am a full-stack developer that combines the power of entrepreneurship and programming to make the lives of programmers easier.
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.)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Switches, as you said, are an ugly and poorly implemented concept. This
object
toswitch
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 aswitch
. It feels natural, it's much more readable than theobject
one and it works in any language.All in all, very useful post!
Some language implementations have a limit to the number of "else if" statements you're allowed to use though.
Interesting. I never knew that. What languages have this limitation?
It isn't language specific, it is implemention specific. I know some C/C++ compilers have this issue, but not all.
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.)