Switch statement with returns -- code correctness

houbysoft picture houbysoft · Jun 17, 2010 · Viewed 120.1k times · Source

Let's say I have code in C with approximately this structure:

switch (something)
{
    case 0:
      return "blah";
      break;

    case 1:
    case 4:
      return "foo";
      break;

    case 2:
    case 3:
      return "bar";
      break;

    default:
      return "foobar";
      break;
}

Now obviously, the breaks are not necessary for the code to run correctly, but it sort of looks like bad practice if I don't put them there to me.

What do you think? Is it fine to remove them? Or would you keep them for increased "correctness"?

Answer

kgiannakakis picture kgiannakakis · Jun 17, 2010

Remove the break statements. They aren't needed and perhaps some compilers will issue "Unreachable code" warnings.