Switch statement with returns — code correctness

前端 未结 17 2213
梦如初夏
梦如初夏 2020-12-12 20:05

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

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

    case 1:
    case 4:
         


        
17条回答
  •  佛祖请我去吃肉
    2020-12-12 20:44

    Wouldn't it be better to have an array with

    arr[0] = "blah"
    arr[1] = "foo"
    arr[2] = "bar"
    

    and do return arr[something];?

    If it's about the practice in general, you should keep the break statements in the switch. In the event that you don't need return statements in the future, it lessens the chance it will fall through to the next case.

提交回复
热议问题