带返回的 switch 语句——代码正确性 [英] Switch statement with returns -- code correctness

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

问题描述

假设我的 C 代码具有大致这种结构:

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

现在显然,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"?

推荐答案

删除 break 语句.它们不是必需的,也许某些编译器会发出 无法访问的代码" 警告.

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

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

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