locked
is goto still the way to go? RRS feed

  • Question

  • I have a sp that has many if statements to assign a value to a variable.  once the value is assigned i dont want to keep testing, so would i still use

    @myVar= myVal

    Goto finished

    ?

    Thursday, May 13, 2010 7:48 PM

Answers

  • There is a thread on another forum on this exact topic you may review http://tek-tips.com/viewthread.cfm?qid=1602038&page=2

    Quoting George Mastros from that thread:

    In my opinion...

    Goto statements should be avoided because they can make code more difficult to read and understand.  Goto is a VERY simple concept to understand.  As such, programmers (and people in general) have a tendency to over use well understood concepts.

    Most of the time, GOTO is not needed.  In fact, I don't have a single goto in any of my SQL code (and there is a lot of code).  Since it it not needed, and can be abused, it's best to avoid it's use altogether.

    Many programmers have the same attitude towards entire programming languages.  Take VB6 for example... When many programmers hear about VB6, they snigger and some begin laughing uncontrollably.  Why?  Because it is easy to write extremely awful code with it.  It does not matter that you CAN write very powerful applications that are elegantly written and relatively easy to maintain.  It's got that bad reputation because the language was abused by a lot of people.

    The GOTO statement has that same bad reputation.


    Premature optimization is the root of all evil in programming. (c) by Donald Knuth

    Naomi Nosonovsky, Sr. Programmer-Analyst

    My blog
    • Marked as answer by KJian_ Wednesday, May 19, 2010 10:11 AM
    Thursday, May 13, 2010 8:02 PM
  • I have a sp that has many if statements to assign a value to a variable.  once the value is assigned i dont want to keep testing, so would i still use

    @myVar= myVal

    Goto finished

    ?


    If you are using it for error handling then avoid it. instead use TRY and Catch .

     

    Like if use

    update table...

    if @error <0

    then GO TO fin

     

     

    Fin:

    Rollback tran

     

    avoid it

     


    Paresh Prajapati
    http://paresh-sqldba.blogspot.com/
    LinkedIn | Tweet Me | FaceBook | Brijj
    • Marked as answer by KJian_ Wednesday, May 19, 2010 10:11 AM
    Friday, May 14, 2010 1:59 PM

All replies

  • There is a thread on another forum on this exact topic you may review http://tek-tips.com/viewthread.cfm?qid=1602038&page=2

    Quoting George Mastros from that thread:

    In my opinion...

    Goto statements should be avoided because they can make code more difficult to read and understand.  Goto is a VERY simple concept to understand.  As such, programmers (and people in general) have a tendency to over use well understood concepts.

    Most of the time, GOTO is not needed.  In fact, I don't have a single goto in any of my SQL code (and there is a lot of code).  Since it it not needed, and can be abused, it's best to avoid it's use altogether.

    Many programmers have the same attitude towards entire programming languages.  Take VB6 for example... When many programmers hear about VB6, they snigger and some begin laughing uncontrollably.  Why?  Because it is easy to write extremely awful code with it.  It does not matter that you CAN write very powerful applications that are elegantly written and relatively easy to maintain.  It's got that bad reputation because the language was abused by a lot of people.

    The GOTO statement has that same bad reputation.


    Premature optimization is the root of all evil in programming. (c) by Donald Knuth

    Naomi Nosonovsky, Sr. Programmer-Analyst

    My blog
    • Marked as answer by KJian_ Wednesday, May 19, 2010 10:11 AM
    Thursday, May 13, 2010 8:02 PM
  • If i am at your place then i will use the same way as its straight foward and the only method to remove so many conditions.
    Friday, May 14, 2010 12:13 PM
  • I never used GoTo

    And I remember that I read somewhere in BOL that GOTO is not recommended.

     


    SQL Server and T-SQL Tutorials
    My Personal Site
    Our true mentor in life is science
    Friday, May 14, 2010 12:26 PM
  • >Because it is easy to write extremely awful code with it.

    I think it's because so many people *do* write bad code. Personally, i rarely hear people bad mouth VB; the Just VB coders.

    The comparison to GOTO is excellent. GOTO is great command, but can be abused. dbdrts01 is using it well. I have done such things many times.

    Friday, May 14, 2010 12:27 PM
  • I have a sp that has many if statements to assign a value to a variable.  once the value is assigned i dont want to keep testing, so would i still use

    @myVar= myVal

    Goto finished

    ?


    If you are using it for error handling then avoid it. instead use TRY and Catch .

     

    Like if use

    update table...

    if @error <0

    then GO TO fin

     

     

    Fin:

    Rollback tran

     

    avoid it

     


    Paresh Prajapati
    http://paresh-sqldba.blogspot.com/
    LinkedIn | Tweet Me | FaceBook | Brijj
    • Marked as answer by KJian_ Wednesday, May 19, 2010 10:11 AM
    Friday, May 14, 2010 1:59 PM