java 返回类型为 void 的方法是否应该使用 return 语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1909779/
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
Should methods with return type void use a return statement?
提问by Pops
I know that there are times when using return;can serve a useful purpose in Java, such as in guarding:
我知道有时 usingreturn;可以在 Java 中起到有用的作用,例如在保护方面:
public void foo(Bar bar) {
if(bar == null)
return;
// bar is not null, go ahead and do stuff with it
}
But what about just reaching the end of a method with return type void? For example,
但是仅仅到达返回类型的方法的末尾void呢?例如,
public void printMenu() {
System.out.println("Print out some boilerplate info here, line 1.");
System.out.println("Print out some boilerplate info here, line 2.");
System.out.println("Print out some boilerplate info here, line 3.");
return;
}
Other than pure style preferences, are there any reasons for or against including that return;? If so, what are they?
除了纯粹的风格偏好之外,是否有任何理由支持或反对包括这一点return;?如果有,它们是什么?
EDIT: Well, that got answered quickly. To summarize the 15 answers posted below: "No."
编辑:嗯,很快就得到了答复。总结下面发布的 15 个答案:“不。”
回答by ChssPly76
Perhaps you're paid by line of code?
也许你是按代码行支付的?
Other then that there's really no reason to put an empty return in the end.
除此之外,真的没有理由在最后放置一个空的回报。
回答by Thomas Owens
回答by ZoogieZork
I see no reason, even from a style perspective, to have a dangling return at the end. After all, you know it's going to return because there's an end brace there...
我认为没有理由,即使从风格的角度来看,最终会有一个悬而未决的回报。毕竟,你知道它会回来,因为那里有一个末端支撑......
回答by Lars Tackmann
I say don't ever do this. Return statements in voidfunctions are only for the purpose of breaking out of the statement's logic. If you start doing this then you send a confusing statement to the readers of your code, one will be tempted to think that perhaps you planed having some ifstatement that you forgot. Always go for readability.
我说永远不要这样做。void函数中的return 语句仅用于打破语句的逻辑。如果您开始这样做,那么您向代码的读者发送了一个令人困惑的语句,人们可能会认为您可能计划使用一些您忘记的if语句。始终追求可读性。
回答by John Scipione
Purely style-based question, makes absolutely no difference (maybe an extra asm instruction, but who cares?). Do whichever you feel more comfortable with or follow the convention previously established in the code.
纯粹基于风格的问题,绝对没有区别(也许是额外的 asm 指令,但谁在乎?)。做任何你觉得更舒服的事情,或者遵循之前在代码中建立的约定。
回答by Chip Uni
One idea of structured programmingwas:
结构化编程的一个想法是:
Every routine should have exactly one entry point and exactly one exit point.
每个例程都应该正好有一个入口点和一个出口点。
If you subscribe to that policy, then the returnstatement indicates the only way to exit the routine.
如果您订阅该策略,则该return语句指示退出例程的唯一方法。
In practice, that policy does not make code clearer, and it has been mostly ignored since the 1970s. If you allow multiple return statements in other routines, then you should allow zero return statements where it makes most sense.
在实践中,该政策并没有使代码更清晰,自 1970 年代以来,它大多被忽略。如果您在其他例程中允许多个 return 语句,那么您应该在最有意义的地方允许零 return 语句。
回答by Ravi Wallau
I think that unnecessary statements are just noise, so I wouldn't add that return in the end. That being said, I would add returns to the begin of the method if something doesn't satisfy my requirements or, even better, I would throw IllegalArgumentException exceptions.
我认为不必要的陈述只是噪音,所以我不会在最后加上那个回报。话虽如此,如果某些东西不满足我的要求,我会在方法的开头添加返回,或者更好的是,我会抛出 IllegalArgumentException 异常。
回答by user12786
In your example the 'return' at the end is a matter of personal, team or organization style. I personally prefer to do an explicit return.
在你的例子中,最后的“回报”是个人、团队或组织风格的问题。我个人更喜欢做一个明确的回报。
回答by ccook
I would avoid it just from a consistency point. Its easier to never put it, than remembering to always add it.
我会从一致性的角度避免它。从不放它比记住总是添加它更容易。
回答by fastcodejava
It doesn't serve any purpose but if you want to do it (because everyone in your team does it or for whatever reason), you can do it.
它没有任何用途,但如果您想这样做(因为您团队中的每个人都这样做或出于任何原因),您可以做到。

