Why I avoid switch statements in c++
So one thing that kills me a lot in c++ is the switch statement. As you all know switch statements look like the following.
Now the first thing that bothers me about C++ switch statements is that, you can fall through a case. What I mean by that is that if
case 0 did not have a
break; statement, you will go directly into the next case (and execute
This often bites me in particular, becase I forget to add the
In c++ strings are not supported as a type. Strings in c++ are actually char Arrays, which means that the switch statement cannot infer switching on strings like Java or C#.
So ultimately switching on strings cannot be done, and if/else if is what has to be used for strings.
So I can’t use
switch except for the other common types, and I can shoot myself in the foot with the behavior of the switch. So I avoid it completely.