为什么在 OOP(例如 Java、C#)中使用中断/继续标签是一种不好的做法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11133127/
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
Why it is a bad practice to use break/continue labels in OOP (e.g. Java, C#)?
提问by Andrii Muzychuk
I was told that using break and continue labels in an OOP language is not OOP programming style. Can you explain in detail why and what is the problem?
有人告诉我,在 OOP 语言中使用 break 和 continue 标签不是 OOP 编程风格。你能详细解释原因和问题是什么吗?
The trick was with this label word. I meant labeled break/continue.
诀窍在于这个标签词。我的意思是标记为中断/继续。
class BreakWithLabelDemo {
public static void main(String[] args) {
int[][] arrayOfInts = {
{ 32, 87, 3, 589 },
{ 12, 1076, 2000, 8 },
{ 622, 127, 77, 955 }
};
int searchfor = 12;
int i;
int j = 0;
boolean foundIt = false;
search:
for (i = 0; i < arrayOfInts.length; i++) {
for (j = 0; j < arrayOfInts[i].length;
j++) {
if (arrayOfInts[i][j] == searchfor) {
foundIt = true;
break search;
}
}
}
if (foundIt) {
System.out.println("Found " + searchfor +
" at " + i + ", " + j);
} else {
System.out.println(searchfor +
" not in the array");
}
}
}
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
回答by tibo
The person who told you that would probably means that break and continue are branching statements like goto which are one mechanism of imperative programming.
告诉你这件事的人可能意味着 break 和 continue 是分支语句,比如 goto,这是命令式编程的一种机制。
A break/continue only allow you to jump to an outer statement, which means that you cannot go everywhere in the code. So you stay in the same method object, so it's not incompatible with OOP.
break/continue 只允许你跳转到外部语句,这意味着你不能在代码中到处走。所以你留在同一个方法对象中,所以它与 OOP 不兼容。
Anyway, saying that break and continue are not OOP is a non-sense. We can discuss about their impact on the readibility maybe but that's all.
无论如何,说break和continue不是OOP是无稽之谈。我们可以讨论它们对可读性的影响,但仅此而已。
回答by Peter Lawrey
break and continue are not functionalstyle programming. There is nothing about OOP which suggestsbreak
, continue
or even goto
within a method is a bad idea.
break 和 continue 不是函数式编程。没有任何关于 OOP 的暗示break
,continue
甚至goto
在一个方法中也是一个坏主意。
IMHO using break and continue are discouraged in OOP languages as they can lead to complexity and confusion. As Labels are used rarely they can confuse even further. I would say you should still use them when you feel its the simplest solution to the problem.
恕我直言,在 OOP 语言中不鼓励使用 break 和 continue,因为它们会导致复杂性和混乱。由于标签很少使用,它们可能会进一步混淆。我会说,当您觉得这是解决问题的最简单方法时,您仍然应该使用它们。
// confusing use of LABEL
http://www.google.com/
do {
if (condition) continue http;
} while(condition2)
another confusing use
另一个令人困惑的用法
GOTO: {
// code
if (condition)
break GOTO; // without a loop
// code
}
Good use of a label
善用标签
OUTER:
for(outer loop) {
for(inner loop)
if (condition)
continue or break OUTER;
}
Odd use of a label
标签的奇怪使用
FOUND: {
for(loop)
if(found)
break FOUND;
// not found
handle not found
}
回答by Heiko Schmitz
The advice not to use break/continue is probably not really related to OOP. It is based on the fact that these statements are similar to the infamous GOTO, which can make code completely unreadable. However, dogmas are bad counsels. The main paradigm should be readability of the code. Jumping out of a loop in the first line using break or continue can be much clearer than putting the whole rest into an if condition.
不使用 break/continue 的建议可能与 OOP 没有真正的关系。它基于这样一个事实,即这些语句类似于臭名昭著的 GOTO,它可以使代码完全不可读。然而,教条是不好的忠告。主要范式应该是代码的可读性。使用 break 或 continue 跳出第一行的循环比将整个其余部分放入 if 条件要清楚得多。
回答by Vladislav Bauer
Bruce Eckel wrote in "Thinking in Java" following idea: "It's important to remember that the only reason to use labels in Java is when you have nested loops and you want to break or continue through more than one nested level."
Bruce Eckel 在“Thinking in Java”中写道:“重要的是要记住,在 Java 中使用标签的唯一原因是当您有嵌套循环并且想要中断或继续通过多个嵌套级别时。”
Actually when you don't use lables the workflow of code is more clear in many cases.
实际上,当您不使用标签时,代码的工作流程在许多情况下会更加清晰。
回答by alexey28
I think main reason is that code is not so clear with break and continue.
我认为主要原因是中断和继续的代码不太清楚。
But also may be some performance issues (not related to OOP): CPU use predictors to load instruction in queue before this instruction will be processed. It easy for predictor to detect what instructions to load next for conditional jump and will be harder for unconditional.
但也可能是一些性能问题(与 OOP 无关):CPU 使用预测器在处理此指令之前将指令加载到队列中。预测器很容易检测到接下来要加载哪些指令以进行条件跳转,而对于无条件跳转则更难。