3 ms·
Goto is good to have. With enough other flow controls you will not use goto so much, although sometimes it is best way to do, by goto. Just because it is uncomm
by zzo38computer 8y ago
Goto is good to have. With enough other flow controls you will not use goto so much, although sometimes it is best way to do, by goto. Just because it is uncommon that it will be helpful does not mean that it should be never used; sometimes it is much more clearly and cleanly than the other way which might be messy in some case. Unfortunately, JavaScript does not have a goto command (although I wrote a way in which a preprocessor could convert programs using goto, to the one without, with the restriction that you cannot jump into a block) (what else I don't like about JavaScript is automatic semicolon insertion, although that is a different problem). Of course there is the consideration of implementation, since you will have to keep track of the labels, which may be forward or backward, rather than only the nesting level stack like implementing break/continue and if/else.
- quietbritishjim 8y agoAny time I have felt the need for something like goto, I have found that need served well by a function with multiple return statements. You're right that it can't simulate jumping into a block though, but that seems like a feature to me. Also, refactoring the necessary portion of code into a function is usually an improvement in its own right. It's funny that the GP comment mentioned this but the other comments have all focused on break/continue. I suppose the backward jumps you mention are usually only served by something like break/continue. But I find that when beginners are starting to learn to program, they very often ask for something that sounds a lot like a goto, but their need is also always satisfied by a function with multiple returns (which usually involves explaining what a function is).