The reason GOTO considered harmful (read the paper) was to assist in formally verifying the semantics of programs. Essentially, GOTO constructs fork environments in the formal analysis, and make things really difficult to work with.
They also make informal reasoning more difficult unless you apply restrictions on how you structure your program.
Know your history before you insult the people who came up with principles.
https://secure.wikimedia.org/wikipedia/en/wiki/Edsger_W._Dij...
return (from anywhere but the end of a function) is just as damaging to formal verifiability as continue, break or goto.
Continue, break, return at least are well-formed with respect to the local environment.
GOTO has no such restrictions (well, in older languages allowing arbitrary GOTOs)... that's why its considered harmful.
The single-return principle is used for flowcharts & formal analysis ease.
It'd be mildly interesting to do a retrospective of compilation & static analysis algorithms to see what more current research turns up with respect to the single-return rule's use.
Anyway, I still think break and continue are OK. Maybe they are not as bad as GOTO, and also I never run into problems with verification of my code.
for(Person p: persons) {
if(p.getAge() < 18)
continue;
// lines of code here
}
The alternative is to have a if-else instead, and that hurts readability even if you don't usually have much code inside a for-loop. You could probably also solve it by writing a filter function that generates a new list with those you want to process, but I find having lots of "magic" functions scattered around the code to be just as annoying. And it will also hurt performance / memory usage for large datasets for (p <- persons; if p.getAge >= 18) {
println(p);
}persons.filter(_.age >= 18).foreach(p => { (code here) });
That is way, way more readable, imho.
for(Person p: persons) {
if(p.getAge() >= 18)
// lines of code here
}
If anything, I would argue that this version is clearer since it makes the context of execution more explicit.Continue in loop is like early return from a function. Some say we should not use it. But other find it really painful to work without 'continue'. I remember moving whole body of a loop into a function call only to get "continue" support in Lua though early return.
Basically it all comes down to organizing code. Removing 'continue' removes one's ability to organize code in a good way, as some see it.