Results 1 to 6 of 6
  1. #1
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default 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.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    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.

  3. #3
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    The good that goto can do is linear.

    The evil that goto can do is exponential.
    My Hobby Project: LegacyClone

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    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.

  5. #5
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    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.
    Last edited by rdtindsm; 09-27-2009 at 03:59 AM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Quote Originally Posted by rdtindsm View Post
    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 underlying byte implementation is, frankly, irrelevant to a language like Java, at the level we code at.

    Quote Originally Posted by rdtindsm View Post
    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.
    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.

    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.

Similar Threads

  1. how to use back bean JSF page?
    By deru4jesus in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 03-27-2009, 08:51 AM
  2. Back button
    By Tokajac in forum Web Frameworks
    Replies: 1
    Last Post: 11-27-2008, 05:27 AM
  3. I need a back and forward code
    By Jaymz in forum New To Java
    Replies: 9
    Last Post: 10-12-2008, 11:50 PM
  4. Tim's back!
    By tim in forum Introductions
    Replies: 3
    Last Post: 06-29-2008, 05:50 AM
  5. goto statement
    By bugger in forum New To Java
    Replies: 2
    Last Post: 12-12-2007, 03:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •