中断for循环

发布于 2024-10-28 19:52:11 字数 345 浏览 8 评论 0原文

假设您有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

刘备忘录 2024-11-04 19:52:11

从循环中中断或返回是完全可以的。

你的老师可能指的是经典的函数应该只有一个返回点扩展到循环。其背后的基本原理是,您的控制流程应始终尽可能简单且易于理解。这并不是一个你必须不假思索地遵守的严格规则。

要在不使用 breakreturn 的情况下重写示例:

function doSomething($array)
{
  $ret = false;
  for($i = 0; $i < sizeof($array) && !$ret; $i++)
  {
    if ($array[$i] == "ok")
      $ret = true;
  }

  return $ret;
}

这很难阅读和维护。你的更简洁。

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 and return:

function doSomething($array)
{
  $ret = false;
  for($i = 0; $i < sizeof($array) && !$ret; $i++)
  {
    if ($array[$i] == "ok")
      $ret = true;
  }

  return $ret;
}

That's painful to read and maintain. Yours is mich more concise.

还给你自由 2024-11-04 19:52:11

如果可能的话,我会远离循环中断。如果你的循环变得越来越大,它就会变得越来越难以阅读。任何不熟悉您的代码或特定函数的人都会认为您的循环只需查看第一行即可迭代整个数组。做任何其他事情都是“令人惊讶的”,因此打破了 CleanCode 哲学中的“最小惊讶原则”。如果您有多个退出循环的条件,那么 for 循环不是您应该寻找的。这就是 while 循环的用途。

function doSomething($array) {
    $found = false;
    $i = 0;

    while ($i < sizeof($array) && !$found) {
        if ($array[$i] == "ok") {
            $found = true;
        }
        $i++;
    }

    return $found;
}

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.

function doSomething($array) {
    $found = false;
    $i = 0;

    while ($i < sizeof($array) && !$found) {
        if ($array[$i] == "ok") {
            $found = true;
        }
        $i++;
    }

    return $found;
}
你与清晨阳光 2024-11-04 19:52:11

我认为这不是一个坏习惯。这些类型的事情,比如打破循环,或者在循环中使用 return 都是不应该粗心地做的事情。

当我还是个初学者时,我总是听到这些话。不要使用这个,也不要使用那个。但后来我意识到他们这么说只是为了防止新人不小心使用这些东西而犯下严重错误。那些没有意识到这一点,后来自己成为老师的人,会让你远离这些,因为它们是邪恶的东西。

所以是的,使用它,有时它非常方便。正如其他人提到的那样,请注意使用这些时需要注意的所有事项。了解何时不应使用它们。

还有一件事:在循环条件中获取数组的维度确实被认为是不好的做法。您不想在每次迭代中获取大小。

而不是:

for($i = 0; $i < sizeof($array); $i++)

使用

$size=sizeof($array);
for($i = 0; $i < $size; $i++)

:)

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:

for($i = 0; $i < sizeof($array); $i++)

Use:

$size=sizeof($array);
for($i = 0; $i < $size; $i++)

)

月光色 2024-11-04 19:52:11

一些程序设计方法,例如 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.

在风中等你 2024-11-04 19:52:11

打破循环并没有什么不好。它就像一个非常有限的转到。不要关心你的老师:-)

There is nothing bad about breaking in loops. It is like a very limited goto. Do not care about your teacher :-)

陌伤ぢ 2024-11-04 19:52:11

我不认为打破循环是一个坏习惯。不过,我通常根据情况使用 breakcontinue 来完成此操作。

I don't think it's a bad practice to break a loop. However, I usually do it by using either break or continue, depending on the situation.

悲念泪 2024-11-04 19:52:11

人们通常说使用 breakcontinue (或它们的等效项)是不好的。无论如何我都会使用它们:-P

另一种方法是将所有代码包装在 ifelse 中。

function doSomething($array)
{
  for($i = 0; $i < sizeof($array); $i++)
  {
    if ($array[$i] != "ok"){
       // do stuff...
    }
    else{
      // do something else
      // or omit this else block to do nothing
    }
  }
}

在从循环返回的情况下。我建议您在循环外设置一个变量,在需要时中断循环,然后返回该变量。

$ret = false;
function doSomething($array)
{
  for($i = 0; $i < sizeof($array); $i++)
  {
    if ($array[$i] == "ok"){
      $ret = true;
      break;
    }
  }

  return $ret;
}

People usually say that using break or continue (or their equivalents) is bad. I use them anyway :-P

Another way to do it is to wrap all code in an if or an else.

function doSomething($array)
{
  for($i = 0; $i < sizeof($array); $i++)
  {
    if ($array[$i] != "ok"){
       // do stuff...
    }
    else{
      // do something else
      // or omit this else block to do nothing
    }
  }
}

In the case of returning from a loop. I suggest you set a variable outside the loop, break the loop when needed, then return the variable.

$ret = false;
function doSomething($array)
{
  for($i = 0; $i < sizeof($array); $i++)
  {
    if ($array[$i] == "ok"){
      $ret = true;
      break;
    }
  }

  return $ret;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文