在任何循环块中使用 return 语句是一种不好的做法吗?
Possible Duplicate:
It's considered a bad pratice use return to end a function?
Hi,
In any programming language is it a bad practise to use the return statement in any loop blocks? if so why?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能说它是坏的。有些人不喜欢它 - 原因是你不能只读取方法的一部分,因为你可能永远不会到达该部分,因为在该部分之前循环返回。如果您可以避免它并仅在方法末尾返回,则可以查看该方法的不同部分。一种方法是使用结果变量并将其传递到方法中,另一种在函数式编程中更好的方法是使用递归而不是循环。
You cannot say it is bad. Some people don't like it - the reason being that you cannot read just a part of a method, because it might happen you never come to that part, because of return in loop before that part. If you can avoid it and return only at the end of the method, you can look at different parts of the method. One way to do that is to use result variable and carry it through the method, other way, better in functional programming, is to use recursion instead of loops.
我想不出你不应该这样做的任何理由。但是,我确信它们存在。与开发应用程序的大多数事情一样,这取决于您所做的假设/决定。
I can not think of any reason why you shouldn't. However, I am sure they exist. Like most things with developing applications, it depends on the assumptions/decisions that you make.