code正确性 - 与回报switch语句 [英] Switch statement with returns -- code correctness

查看:145
本文介绍了code正确性 - 与回报switch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有在C code。与此近似的结构:

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;
}

现在很明显,破发S是没有必要为code才能正常运行,但它那种貌似不好的做法,如果我不把它们放在那里给我。

Now obviously, the "break"s 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"?

推荐答案

删除break语句。他们不需要,也许有些编译器会发出不可达code的警告。

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

这篇关于code正确性 - 与回报switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆