Java 编码标准/最佳实践 - 中断/继续标签的命名约定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15481/
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
Java Coding standard / best practices - naming convention for break/continue labels
提问by Mo.
Sometimes a labeled break or continue can make code a lot more readable.
有时标记的中断或继续可以使代码更具可读性。
OUTERLOOP: for ( ;/*stuff*/; ) {
//...lots of code
if ( isEnough() ) break OUTERLOOP;
//...more code
}
I was wondering what the common convention for the labels was. All caps? first cap?
我想知道标签的通用约定是什么。全部大写?第一个帽子?
采纳答案by Craig
If you have to use them use capitals, this draws attention to them and singles them out from being mistakenly interpreted as "Class" names. Drawing attention to them has the additional benefit of catching someone's eye that will come along and refactor your code and remove them. ;)
如果您必须使用它们,请使用大写字母,这会引起对它们的注意,并避免将它们错误地解释为“类”名称。引起对它们的注意还有一个额外的好处,那就是吸引将要出现并重构您的代码并删除它们的人的眼球。;)
回答by RodeoClown
The convention is to avoid labels altogether.
惯例是完全避免标签。
There are very, very few valid reasons to use a label for breaking out of a loop. Breaking out is ok, but you can remove the need to break at all by modifying your design a little. In the example you have given, you would extract the 'Lots of code' sections and put them in individual methods with meaningful names.
使用标签来跳出循环的正当理由非常非常少。断开是可以的,但是您可以通过稍微修改设计来完全消除断开的需要。在您给出的示例中,您将提取“大量代码”部分并将它们放入具有有意义名称的单独方法中。
for ( ;/*stuff*/; )
{
lotsOfCode();
if ( !isEnough() )
{
moreCode();
}
}
Edit:having seen the actual code in question (over here), I think the use of labels is probably the best way to make the code readable. In most cases using labels is the wrong approach, in this instance, I think it is fine.
编辑:看过有问题的实际代码(在这里),我认为使用标签可能是使代码可读的最佳方式。在大多数情况下使用标签是错误的方法,在这种情况下,我认为这很好。
回答by JProgrammer
The convetion/best practise would still be not to use them at all and to refactor the code so that is more readable using extract as method.
传统/最佳实践仍然是根本不使用它们并重构代码,以便使用提取作为方法更具可读性。
回答by Michael Neale
They are kind of the goto of Java - not sure if C# has them. I have never used them in practice, I can't think of a case where avoiding them wouldn't result in much more readable code.
它们有点像 Java 的 goto - 不确定 C# 是否有它们。我从未在实践中使用过它们,我想不出避免它们不会导致代码更具可读性的情况。
But if you have to- I think all caps is ok. Most people won't use labelled breaks, so when they see the code, the caps will jump out at them and will force them to realise what is going on.
但如果你必须 - 我认为所有的大写都可以。大多数人不会使用带标签的中断,所以当他们看到代码时,大写字母会跳到他们身上,迫使他们意识到发生了什么。
回答by RodeoClown
I know, I should not use labels.
But just assume, I have some code, that could gain a lot in readability from labeled breaks, how do I format them.
我知道,我不应该使用标签。
但是假设,我有一些代码,可以从标记的中断中获得很多可读性,我该如何格式化它们。
Mo, your premise is wrong. The question shouldn't be 'how do I format them?'
莫,你的前提是错误的。问题不应该是“我如何格式化它们?”
Your question should be 'I have code that has a large amount of logic inside loops - how do I make it more readable?'
您的问题应该是“我的代码在循环中包含大量逻辑 - 如何使其更具可读性?”
The answer to that question is to move the code into individual, well named functions. Then you don't need to label the breaks at all.
这个问题的答案是将代码移动到单独的、命名良好的函数中。那么你根本不需要标记休息时间。
回答by izb
The convention I've most seen is simply camel case, like a method name...
我最常看到的约定是简单的驼峰式大小写,就像方法名一样......
myLabel:
but I've also seen labels prefixed with an underscore
但我也看到了带有下划线前缀的标签
_myLabel:
or with lab...
或与实验室...
labSomething:
You can probably sense though from the other answers that you'll be hard-pushed to find a coding standard that says anything other than 'Don't use labels'. The answer then I guess is that you should use whatever style makes sense to you, as long as it's consistent.
您可能会从其他答案中感觉到,您将很难找到一个除了“不要使用标签”之外的任何其他内容的编码标准。然后我想答案是你应该使用任何对你有意义的风格,只要它是一致的。
回答by Marcus Downing
I don't understand where this "don't use labels" rule comes from. When doing non-trivial looping logic, the test to break or continue isn't always neatly at the end of the surrounding block.
我不明白这个“不使用标签”规则的来源。在进行非平凡的循环逻辑时,中断或继续的测试并不总是整齐地出现在周围块的末尾。
outer_loop:
for (...) {
// some code
for (...) {
// some code
if (...)
continue outer_loop;
// more code
}
// more code
}
Yes, cases like this do happen all the time. What are people suggesting I use instead? A boolean condition like this?
是的,这样的情况确实时常发生。人们建议我改用什么?像这样的布尔条件?
for (...) {
// some code
boolean continueOuterLoop = false;
for (...) {
// some code
if (...) {
continueOuterLoop = true;
break;
}
// more code
}
if (continueOuterLoop)
continue;
// more code
}
Yuck!Refactoring it as a method doesn't alleviate that either:
糟糕!将其重构为一种方法也不能缓解:
boolean innerLoop (...) {
for (...) {
// some code
if (...) {
return true;
}
// more code
}
return false;
}
for (...) {
// some code
if (innerLoop(...))
continue;
// more code
}
Sure it's a little prettier, but it's still passing around a superfluous boolean. And if the inner loop modified local variables, refactoring it into a method isn't always the correct solution.
当然它更漂亮一点,但它仍然传递了一个多余的布尔值。如果内部循环修改了局部变量,将其重构为方法并不总是正确的解决方案。
So why are you all against labels? Give me some solid reasons, and practical alternatives for the above case.
那么为什么你们都反对标签呢?给我一些可靠的理由,以及上述案例的实用替代方案。
回答by SCdF
wrt sadie's code example:
WRT萨迪的代码示例:
You gave
你给了
outerloop:
for (...) {
// some code
for (...) {
// some code
if (...)
continue outerloop;
// more code
}
// more code
}
As an example. You make a good point. My best guess would be:
举个例子。你说的对。我最好的猜测是:
public void lookMumNoLabels() {
for (...) {
// some code
doMoreInnerCodeLogic(...);
}
}
private void doMoreInnerCodeLogic(...) {
for (...) {
// some code
if (...) return;
}
}
But there would be examples where that kind of refactoring doesn't sit correctly with whatever logic you're doing.
但是,在某些示例中,这种重构与您正在执行的任何逻辑都不正确。
回答by Mo.
As labels are so rarely useful, it appears, that there is no clear convention. The Java language specification has one example with labels and they are in non_cap.
由于标签很少有用,似乎没有明确的约定。Java 语言规范有一个带有标签的示例,它们在 non_cap 中。
But since they are so rare, in my opinion it is best, to think twice whether they are really the right tool.
但由于它们非常罕见,在我看来,最好再三考虑它们是否真的是正确的工具。
And if they are the right tool, make them all caps so that other developers (or yourself later on) realize them as something unusual right away. (as Craig already pointed out)
如果它们是正确的工具,请将它们全部大写,以便其他开发人员(或稍后您自己)立即意识到它们是不寻常的。(正如克雷格已经指出的那样)
回答by Anders Sandvig
Sun's Java code style seem to prefer naming labels in the same way as variables, meaning camel case with the first letter in lower case.
Sun 的 Java 代码风格似乎更喜欢以与变量相同的方式命名标签,即首字母小写的驼峰式大小写。