Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continue Considered Harmful? [closed]

Tags:

Should developers avoid using continue in C# or its equivalent in other languages to force the next iteration of a loop? Would arguments for or against overlap with arguments about Goto?

like image 637
Brian Avatar asked Sep 11 '08 20:09

Brian


People also ask

Is it good practice to use continue?

If you use continue then it means your loop elements are not restricted enough, so there is the potential you are looping through unnecessary elements. It also means that at any point inside a loop, you break the 'rules' of the loop. So any change at a later date may break things if you do not notice a continue.

Is it okay to use continue in Python?

Using continue in Python, personally, is fine. If you're putting it in a if/else statement type condition in python, continue will work.

Is goto useless?

Every control structure can be implemented in terms of goto but this observation is utterly trivial and useless. goto isn't considered harmful because of its positive effects but because of its negative consequences and these have been eliminated by structured programming.

What does continue do in java?

The continue keyword is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration.


2 Answers

I think there should be more use of continue!

Too often I come across code like:

for (...) {    if (!cond1)    {       if (!cond2)       {           ... highly indented lines ...       }    } } 

instead of

for (...) {    if (cond1 || cond2)    {       continue;    }     ... } 

Use it to make the code more readable!

like image 90
Rob Walker Avatar answered Oct 16 '22 03:10

Rob Walker


Is continue any more harmful than, say, break?

If anything, in the majority of cases where I encounter/use it, I find it makes code clearer and less spaghetti-like.

like image 27
dF. Avatar answered Oct 16 '22 03:10

dF.