开发者

Is it good practice to use break and continue in PHP?

Is it a good practice to use break and continue as 开发者_Go百科sentinel for loops in PHP?

e.g.

if (!empty($var))
    break;


do {
 if (condition1)
   break;
 some code;
 some code;
 if (condition2)
   break;
 some code;
 some code;
 if (condition3)
   break;
 some code;
 some code;
} while (false);

vs.

if (!condition1) {
   some code; 
   some code;
   if (!condition2) {
      some code;
      some code;
      if (!condition3) {
         some code;
         some code;
      }
}

Some find the first version an abhomination and difficult to read and love the second version. Some find the first version cleaner and easier to read. As the number of conditions multiply, I tend to find the first version easier to follow, as the second one tends to get more and more difficult to follow the level of nesting. Also if the if (condition) break; gets into something only slightly more complex like if (condition) {some code; break}, the do {if .. break; if .. break..;} while(false) pattern gets even more clear compared with equivalend nested ifs.


In light usage it is ok, but in heavy usage it makes your code spaghetti. break and continue is basically just a restricted goto and as such, use sparingly.


It absolutely is, they're both valid programming constructs.

What is not a good idea is the newly introduced GOTO. (Please tell me this was an April fool's joke I didn't see the note about!)


It is perfectly correct to use break or continue as long as it helps to make the code easier to read and understand. I personally use them very rarely, and only when I cannot easily use another structure.

In the case of most while statements, it's easier to achieve the same result as a break or continue by using a boolean variable as the condition for the loop in the first place, and then modifying its value inside the loop.

On the other hand, the best use case for the break, in my opinion, is to save resources if you are iterating through an array or something similar with a for or foreach block and are only interested in processing elements until some item is reached. By using a break after reaching this element, it is possible to save on processing power by breaking out of the loop without going over the remaining elements. This makes the code more efficient without making it less legible.

And of course, it is practically impossible to use switch statements without break.

Another exception is if you need to control nested structures, in which case it is sometimes simpler to use break n or continue n than manipulate multiple variables concurrently. (Even though this use case is probably the most controversial...)


In Python, we use infinite loop and break to improve readability. So yes, you can use it, provided you do it correctly, and for a good reason.


Let me answer with a couple of questions:

  • Why shouldn't it be considered good practice?

  • Does it reduce readability of your code?

  • Does it slow down your for cycles?


For a discussion of break and continue in PHP (and looping in general) have a look at Advanced loops - you get the impression that the author can just about manage to swallow break and continue but not break n and continue n. :-)


In 5+ years programming PHP I never had to use break outside of switch statements.

Continue is sometimes used to skip first or last items in iterations, but I don't like it very much.

Why did they reintroduce GOTO ? that's a shame

To answer the question, in

if (!empty($var))
    break;

Why not use return (if in a method context)

if (!empty($var))
    return false;

I think this way much clear and makes the caller aware of what's happened inside. A better use for argument errors is using Excecptions, which in facte will break execution a the point they are raised.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜