Bv202
Bv202

Reputation: 4044

break in for loop

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

Upvotes: 3

Views: 394

Answers (7)

vbence
vbence

Reputation: 20343

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.

Upvotes: 1

kapa
kapa

Reputation: 78731

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++)

)

Upvotes: 1

Nicktar
Nicktar

Reputation: 5575

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;
}

Upvotes: 1

gen_Eric
gen_Eric

Reputation: 227310

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;
}

Upvotes: 0

Alexander Gessler
Alexander Gessler

Reputation: 46647

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.

Upvotes: 6

mpj
mpj

Reputation: 5367

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.

Upvotes: 0

eisberg
eisberg

Reputation: 3771

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

Upvotes: 0

Related Questions