Skip to main content

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.

auto s = 0;

switch(s)
{
   case 0:
      doSomething();
      break;
   case 1:
     doSomething1();
     break;

}

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 doSomething1())

This often bites me in particular, becase I forget to add the break;

Switching on strings

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.

Tagged In:
c++ things i avoid