中断for循环
假设您有以下代码:
function doSomething($array)
{
for($i = 0; $i < sizeof($array); $i++)
{
if ($array[$i] == "ok")
return true;
}
return false;
}
请注意,我不是在谈论 PHP 特定的(这适用于所有语言)或这个特定的示例。这是关于中断 for 循环(在本例中,返回 true;停止循环)。
据我的一位老师说,这是非常非常糟糕的做法,而且还没有完成。
打破循环真的是一种未完成的练习吗?
谢谢
Assume you have this code:
function doSomething($array)
{
for($i = 0; $i < sizeof($array); $i++)
{
if ($array[$i] == "ok")
return true;
}
return false;
}
Note that I'm not talking about PHP specific (this applies to all languages) or this particular example. It's about breaking in the for loop (in this case, return true; stops the loop).
According to a teacher of me, this is very, very bad and a not-done practice.
Is breaking from a loop really a not-done practice?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
从循环中中断或返回是完全可以的。
你的老师可能指的是经典的函数应该只有一个返回点扩展到循环。其背后的基本原理是,您的控制流程应始终尽可能简单且易于理解。这并不是一个你必须不假思索地遵守的严格规则。
要在不使用
break
和return
的情况下重写示例:这很难阅读和维护。你的更简洁。
It is perfectly fine to break or return from a loop.
What your teacher possibly refers to is the classic a function should have only one return point extended to loops. The rationale behind this is, that your control flow should always be as easy and understandable as possible. It's not a strict rule that you have to obey without thinking.
To rewrite your sample without using
break
andreturn
:That's painful to read and maintain. Yours is mich more concise.
如果可能的话,我会远离循环中断。如果你的循环变得越来越大,它就会变得越来越难以阅读。任何不熟悉您的代码或特定函数的人都会认为您的循环只需查看第一行即可迭代整个数组。做任何其他事情都是“令人惊讶的”,因此打破了 CleanCode 哲学中的“最小惊讶原则”。如果您有多个退出循环的条件,那么 for 循环不是您应该寻找的。这就是 while 循环的用途。
I'd stay away from breaks in loops if possible. If your loop gets bigger it becomes increasingly hard to read. Anyone not familiar with your code or that specific function will assume that your loop iterates over the whole array just by looking at the first line. To do anything else is "surprising" and thus breaks the "Principle of Least Astonishment" from the CleanCode philosophy. If you've got multiple conditions to exit your loop, then a for-loop isn't the one you should be looking for. That's what while-loops are for.
我认为这不是一个坏习惯。这些类型的事情,比如打破循环,或者在循环中使用 return 都是不应该粗心地做的事情。
当我还是个初学者时,我总是听到这些话。不要使用这个,也不要使用那个。但后来我意识到他们这么说只是为了防止新人不小心使用这些东西而犯下严重错误。那些没有意识到这一点,后来自己成为老师的人,会让你远离这些,因为它们是邪恶的东西。
所以是的,使用它,有时它非常方便。正如其他人提到的那样,请注意使用这些时需要注意的所有事项。了解何时不应使用它们。
(还有一件事:在循环条件中获取数组的维度确实被认为是不好的做法。您不想在每次迭代中获取大小。
而不是:
使用
:)
I don't think it would be a bad practice. These types of things, like breaking a loop, or using a return in the loop are just things that should not be done carelessly.
When I was a beginner, I always heard these things. Don't use this, or don't use that. But later I realized they only say this to keep newcomers from committing bad mistakes by using these things carelessly. And people who don't realize this, and later become teachers themselves, will keep you away from these as they were something evil.
So yes, use it, it is very very handy sometimes. Just note all the things you have to care about when using these, as others here have mentioned. Learn when you should not use them.
(One more thing: getting the array's dimension in the loop's condition is considered bad practice indeed. You don't want to get the size on every iteration.
Instead of:
Use:
)
一些程序设计方法,例如 Jackson 并不是真正擅长打破循环。这是一个相当学术性的论点,在现实生活中并没有得到真正遵循。
Some program design approaces like Jackson are not really friends with breaking loops. This is a rather academical argument and not really followed in real life.
打破循环并没有什么不好。它就像一个非常有限的转到。不要关心你的老师:-)
There is nothing bad about breaking in loops. It is like a very limited goto. Do not care about your teacher :-)
我不认为打破循环是一个坏习惯。不过,我通常根据情况使用
break
或continue
来完成此操作。I don't think it's a bad practice to break a loop. However, I usually do it by using either
break
orcontinue
, depending on the situation.人们通常说使用
break
或continue
(或它们的等效项)是不好的。无论如何我都会使用它们:-P另一种方法是将所有代码包装在
if
或else
中。在从循环
返回
的情况下。我建议您在循环外设置一个变量,在需要时中断循环,然后返回该变量。People usually say that using
break
orcontinue
(or their equivalents) is bad. I use them anyway :-PAnother way to do it is to wrap all code in an
if
or anelse
.In the case of
return
ing from a loop. I suggest you set a variable outside the loop, break the loop when needed, then return the variable.