Results 1 to 4 of 4

Thread: Use of goto

  1. #1

    Use of goto

    Continued from this post:

    Let's first start with the disadvantages of using goto:

    a. It makes the code obfuscated;in plain terms - hard to read.
    b. There are unnecessary jumps in the scripts from here and there which increases the execution time.
    c. It is always possible to avoid goto. I have never found a situation where goto cannot be avoided (Unless the compiler does not support some of the available constructs like break, continue, loops ,etc)

    I could only think up of this many, somebody please add to it.

    Here is the list where goto can be used to the advantage inspite of all of its problems.

    a. Avoiding Recursion. Recursion takes up more of the memory since it keeps on pushing to the stack. If goto can avoid that, use it.

    b. Avoiding multiple jumps to break out of the outermost loop from the inner nested loops. This saves on the number of jumps required to break out of the outermost loop.

    c. Amazingly, goto has been used to OPTIMIZE code in the linux kernel(v2.4 I am not sure if it still lies there, but most probably it does). Here is the reason they have for it. All their lines of code are sequential in terms of probability. Depending upon what will be executed majority of the times, they have arranged the code sequentially and goto has been used to jump to the portions that would be used very rarely. This is to avoid unnecessary jumps that using "if else" introduces.

    Thanks and Regards,

  2. #2

    Re: Use of goto

    You make some interesting statements, most of which I agree with, but I'll throw a few opinions into the fore.

    Advantages - A, certainly. I still remember the code I wrote in MS-Basic. Shudder...

    B - not necessarily - as you say in disadvantage C, gotos _can_ be used to make code faster. I'll also go so far as to say it depends on the language and environment, including whether the compiler does optimizations and what type, or whether the interpreter (in the case of Java, .NET, and probably SilkTest) does any and of what type. Also depends ultimately on CPU architecture.

    C - I'd temper that with "mostly". There have been a few odd circumstances where a goto eliminated a complete redesign.

    Disadvantages - A - well, I think that argument made more sense in the days of old. If you know how deep the recursion would go (maximum), then it's easy enough to plan for. There certainly are many problems that are elegantly solved with recursion.

    B - can be avoided with proper design, I'd think. Know when to extract some code into a procedure, then just use judicious Returns.

    C - particularly applies to assembly code, and since C is generally defined as a "high level assembler", applies to it too. Depends on the language.

    Other comments: My first language was Fortran, followed by Basic and assembly. When I learned Algol and PL/1, I began to see possibilities - that's when I first learned of the "GoTo Considered Harmful" paper. I then spent ten years programming C, and in that time found only 2 or 3 places I needed a goto. Can't say I've ever used one in SilkTest in 7 years.


  3. #3

    Re: Use of goto

    I agree with Dana in that there are times a goto is necessary. I don't like it and usually spend a lot of time trying to figure out how not to use it.

    A co-worker ran into a similar problem lately. Was very depressed about it. Went home and bemoaned to his wife that he'd had to use a goto. I think her reply was "Geek."

    - Dave
    A concerned Borland customer, a fly in the ointment, a wrench in the works.

  4. #4

    Re: Use of goto

    Oh yea, I guess I totally ignored the compiler optimizations when mentioning about jumps. I agree to it. Its compiler plus machine dependent.

    Recursion vs goto: Yea, its a trade-off and the decision got to be right to use which. I have found situations in which goto can be more effective and efficient than recursion, also vice-versa.

    Thanks and Regards,



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
BetaSoft Inc.
All times are GMT -8. The time now is 05:27 PM.

Copyright BetaSoft Inc.