C语言 带返回的 switch 语句——代码正确性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3065438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:41:23  来源:igfitidea点击:

Switch statement with returns -- code correctness

cswitch-statementcorrectness

提问by houbysoft

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

假设我在 C 中有大约这种结构的代码:

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.

现在很明显,breaks 不是代码正确运行所必需的,但如果我不把它们放在那里,这看起来像是一种不好的做法。

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

你怎么认为?去掉它们好吗?或者你会保留它们以增加“正确性”?

回答by kgiannakakis

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

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

回答by NotMe

I would take a different tack entirely. Don't RETURN in the middle of the method/function. Instead, just put the return value in a local variable and send it at the end.

我会采取完全不同的策略。不要在方法/函数的中间返回。相反,只需将返回值放在局部变量中并在最后发送。

Personally, I find the following to be more readable:

就个人而言,我发现以下内容更具可读性:

String result = "";

switch (something) {
case 0:
  result = "blah";
  break;
case 1:
  result = "foo";
  break;
}

return result;

回答by Amardeep AC9MF

Personally I would remove the returns and keep the breaks. I would use the switch statement to assign a value to a variable. Then return that variable after the switch statement.

我个人会删除退货并保留休息时间。我会使用 switch 语句为变量赋值。然后在 switch 语句之后返回该变量。

Though this is an arguable point I've always felt that good design and encapsulation means one way in and one way out. It is much easier to guarantee the logic and you don't accidentally miss cleanup code based on the cyclomatic complexity of your function.

虽然这是一个有争议的观点,但我一直认为良好的设计和封装意味着一种方式和一种方式。保证逻辑要容易得多,而且您不会意外错过基于函数圈复杂度的清理代码。

One exception: Returning early is okay if a bad parameter is detected at the beginning of a function--before any resources are acquired.

一个例外:如果在函数开始时检测到错误参数——在获取任何资源之前,提前返回是可以的。

回答by Paul R

Keep the breaks - you're less likely to run into trouble if/when you edit the code later if the breaks are already in place.

保留中断 - 如果中断已经到位,如果/当您稍后编辑代码时,您不太可能遇到麻烦。

Having said that, it's considered by many (including me) to be bad practice to return from the middle of a function. Ideally a function should have one entry point and one exit point.

话虽如此,许多人(包括我)认为从函数中间返回是不好的做法。理想情况下,一个函数应该有一个入口点和一个出口点。

回答by Stephen

Remove them. It's idiomatic to return from casestatements, and it's "unreachable code" noise otherwise.

删除它们。从case语句返回是惯用的,否则就是“无法访问的代码”。

回答by Hank Gay

I would remove them. In my book, dead code like that should be considered errors because it makes you do a double-take and ask yourself "How would I ever execute that line?"

我会删除它们。在我的书中,像这样的死代码应该被认为是错误,因为它会让你反复思考并问自己“我将如何执行那一行?”

回答by Jerry Coffin

I'd normally write the code without them. IMO, dead code tends to indicate sloppiness and/or lack of understanding.

我通常会在没有它们的情况下编写代码。IMO,死代码往往表明草率和/或缺乏理解。

Of course, I'd also consider something like:

当然,我也会考虑这样的事情:

char const *rets[] = {"blah", "foo", "bar"};

return rets[something];

Edit: even with the edited post, this general idea can work fine:

编辑:即使使用编辑过的帖子,这个总体思路也可以正常工作:

char const *rets[] = { "blah", "foo", "bar", "bar", "foo"};

if ((unsigned)something < 5)
    return rets[something]
return "foobar";

At some point, especially if the input values are sparse (e.g., 1, 100, 1000 and 10000), you want a sparse array instead. You can implement that as either a tree or a map reasonably well (though, of course, a switch still works in this case as well).

在某些时候,特别是如果输入值是稀疏的(例如,1、100、1000 和 10000),您需要一个稀疏数组。您可以将其实现为树或地图相当好(当然,在这种情况下,开关仍然有效)。

回答by Vivin Paliath

Wouldn't it be better to have an array with

有一个数组不是更好吗

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

and do return arr[something];?

和做return arr[something];

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

如果是关于一般实践,则应将break语句保留在 switch 中。如果您return将来不需要语句,它会减少它落入下一个case.

回答by Bella

I would say remove them and define a default: branch.

我会说删除它们并定义一个默认值:分支。

回答by Steve Sheldon

For "correctness", single entry, single exit blocks are a good idea. At least they were when I did my computer science degree. So I would probably declare a variable, assign to it in the switch and return once at the end of the function

对于“正确性”,单入口、单出口块是一个好主意。至少在我攻读计算机科学学位时是这样。所以我可能会声明一个变量,在 switch 中分配给它并在函数结束时返回一次