3 ms·
My Opinion: Banning break and continue from your loops will improve code quality
- elviejo 12y agoDijkstra said it before: "Go To Statement Considered Harmful"[1] And Break and Continue are just forms of GoTo. [1] http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html http://www.u.arizona.edu/~rubinson/copyright_violations/Go_T...
- PaulHoule 12y agoThis is an old fight which was settled (in the other direction) around 1980 by looking at the example of linear string search. Without break the logic is clearly contorted and hard to understand. Now I don't argue at all that dropping out of a loop with return out of a small method is preferable though.
- chrisbennet 12y agoI think "break" and "continue" have their place. The code he used is a bit of a straw man IMO: while (someCondition) { if (someOtherCondition) { doSomeLogic(); continue; //jump to the top of the loop } doSomeOtherLogic(); break; //exit the loop } Compare this to how I use "break" and "continue" for (int j = 0; j < nSomethings; j++) { int nDroidsLeftInQ = GetNDroidsInQueue(); if (nDroidsLeftInQ == 0) break; // blow this clambake, we're done Droid droid = GetDroidFromQueue(); // Skip droids that aren't even close bool theseArentTheDroidsWereLookingFor = (DroidFitnessTest(droid) == -1); if (theseArentTheDroidsWereLookingFor ) continue; // 30 more lines code where the work actually gets done doSomeLogic1(); doSomeLogic2(); ... }