Bring back the GOTO ;-)
Linux: Using goto In Kernel Code | KernelTrap
Makes an interesting read. The original poster was chastising Linus Torvolds for using goto in linux kernal code. The thread actually contains replies by Torvolds and a longer comment by a subsequent poster pointing out that goto can make the code simpler & smaller. They point out that even if we don't have to count every cycle and byte into today's computers, compact code prevents cache misses, as well as the point that structured constructs are ultimately implemented with jmp instructions with particular reference to the equivilence of the break instruction.
The thread ends with the OP repeating his plea to Torvolds to remove the goto because it violates every tenet that he had learned in studying CS.
If !goto is strictly enforced by a shop, I can understand. It can be misused by the unwashed, and in general, there are very strong reasons for using the general control methods of structured programming. Shouldn't be a fundamentalist religious issue though.
for sure, sometimes the forest gets lost in the trees. and the right solution is the one that works.. it's just that the mraning of 'right' and 'works' is very subjective, influenced by the constraints from the environment, and changes over time.
In Java, I can't imagine how 'well' a goto would work with the try..catch..finally exception handling constructs.
The good that goto can do is linear.
The evil that goto can do is exponential.
Goto might be fine in the initial implementation, but it makes maintenance a nightmare -- especially when the person making changes didn't write the code initially... which is most of the time.
The points that others have made is that the proper use of goto is:
> exiting a deeply nested control structure. The java labeled break is exactly this usage, as are "break" and "continue" These statements simply prevent alternative uses of goto that give its use a bad name.
> that it would be an alternative to the catch-try, not to be used in the same code.
The link in my first post includes a link to the linux designer guide which provides model code.
A goto should be used sparingly, and my reading has indicated that there are only a couple places that it should be used. The linux kernal authors present error handling code using goto as part of their manuals writing drivers.
Edsger Dijkstra wrote the original "GoTo considered harmful" which was followed by "'goto considered harmful' considered harmful." In the true spirit of recursion or deeply nested statements, there is also '"'GoTo considered harmful' considered harmful" considered harmful'.
I'm in no way suggesting that goto should be used as a matter of course. It should be used only within the concepts of structured programming, but we need to remember that structured program constructs are only a way to allow the compiler to enforce the correct use of jump statements. Perhaps computer architecture has changed enough to allow it, but the only structure at the level is the conditional and absolute jump.
Let me suggest, and I realize this is not strictly true, goto shouldn't make code any harder to maintain than break, continue, and labeled break do. Let me also suggest for the sake of argument, that following correctly written code with goto is no harder than following nested brackets and parens. Not a trivial job in LISP which has no goto. Different languages have different controls structures, all built with jump statements under the hood. Perhaps one should have the freedom to roll your own.
I can be a bit of an iconoclast, and dislike fundamentalist absolutism in all forms, whether it is the Taliban or Rush Limbaugh. As mentioned earlier, I can only shake my head when someone multiply requests Torvolds to stop using goto jumps because it is evil coding incarnate. This is all I am arguing against - the absolutist labeling of the goto as the tool of the devil.
The underlying byte implementation is, frankly, irrelevant to a language like Java, at the level we code at.
Originally Posted by rdtindsm
Nothing to do with "unwashed" (poisoning the well much?). As pointed out by quad64bit, it's about maintainability, especially in a higher level language like Java. Breaks, continues, multiple returns are all legitimate in their own right as it's pretty obvious where they are going to. I don't use labelled jumps for the simple reason that it is not obvious where things are going to.
Originally Posted by rdtindsm
Add to that that Java code that get's so complex you feel you need a goto is probably written badly in the first place, gotos are entirely unecessary.