Re: Java needs "goto" (was Re: hi)



Randolf Richardson wrote:
This typically occurs because the reserved word "goto" isn't implemented. There are situations where "goto" would be very useful, such as:

I totally disagree. Assemblers need "goto" but no decent HLL does. That goes for COBOL too and before you ask, yes I've written GOTO-less COBOL.
I have never used goto (or needed it) in C and see no need for it in Java.

0. An alternative to "break label" since label is currently limited in where it can be located (such code could be easier to read)

I don't use this construction in C either and, again, don't need it.

1. The ability to share code between methods within a class, which all end with the same functionality (this could be more efficient than calling another method; javac would need to generate errors such as attempts to access variables that belong to different methods, return type mismatches, etc.)

Encapsulating the code in a private method makes for more readable code as well as eliminating code duplication.

I do agree with your view that infinite loops that depend on exceptions are a bad practice. Using conditionals to trigger a "break" would also be better handled by making that the focus of the loop -- and if they need to compare afterwards, then "do { ... } while (condition);" can certainly solve that problem.

Yes - but that's a result of classes that throw exceptions when their methods would be better off returning control values. IMO if you have to use exceptions to control normal logic flow in code that calls the class methods merely demonstrates bad class design.

I never design classes/methods that force their user to control normal logic flow with exceptions: I use methods that return control information (e.g hasNextObject() to control loops). Similarly, my constructors never throw exceptions: code that may signal errors with exceptions is pulled out of the constructor and encapsulated as a separate method. This is because try...catch blocks that include class instance declarations screw up otherwise clean block structures by preventing me from grouping all object instance declarations at the start of a block rather than sprinkling them through the code.


--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
.



Relevant Pages

  • Re: What do you think about the code?
    ... Well that's team democracy at work. ... Exceptions are just another form of gotos, ... only its "goto that location and tell the code there that this ...
    (comp.lang.c)
  • Re: What do you think about the code?
    ... Do you program in a language other than C? ... Exceptions are just another form of gotos, ... only its "goto that location and tell the code there that this ... Within a fixed scope, but you still identify the error handler (the ...
    (comp.lang.c)
  • Re: What do you think about the code?
    ... Do you program in a language other than C? ... Exceptions are just another form of gotos, ... only its "goto that location and tell the code there that this ... Anything allocated on the stack will get freed when longjmp gets ...
    (comp.lang.c)
  • Re: What do you think about the code?
    ... Within a fixed scope, but you still identify the error handler (the ... If exceptions are available, I'd rather throw ... I'd chuck in a goto rather than have the logic code do ... longjmp cannot call destructors automatically like ...
    (comp.lang.c)
  • Re: returning back to loop check condition without completing the loop
    ... I was taught not to use "Goto" as a primary rule. ... the construction I used is pretty much it. ... want to skip to the next loop iteration from. ... and the inverted logical tests in the various ...
    (microsoft.public.excel.programming)