Welcome to SPN

Register and Join the most happening forum of Sikh community & intellectuals from around the world.

Sign Up Now!

Is GoTo a good programming method?

Discussion in 'Information Technology' started by b_bussoloti, Jul 28, 2006.

  1. b_bussoloti

    b_bussoloti
    Expand Collapse
    Guest

    Folks,
    I've been programming in C/C++, Java using OO Development and my teachers
    always said that using GoTo in these languages will send you straight to hell.
    In the past few weeks I managed to learn a bit of VBA to use in my work and
    in sometimes it seems that using GoTo is a good deal, especially when I need
    to throw an Error of some kind...
    So I ask you:"Is GoTo a good programming method in VB/VBA? Is it not
    comdemnable?"
     
  2. Sprinks

    Sprinks
    Expand Collapse
    Guest

    In general, GoTo statements make code less understandable and maintainable
    because the logic being implemented is less clear. Often, resorting to a
    GoTo statement is done because the programmer has not thought through or
    diagrammed his logic from the onset.

    More experienced programmers rarely use GoTo statements for these reasons,
    and tend to "program for usability" by developing general purpose functions
    and procedures that they can debug once and reuse over and over. New
    procedures will to a large degree simply call these functions and procedures,
    making program flow transparent to both themselves and to inheritors of their
    code.

    An exception to this guideline is in error handling, where you wish to exit
    the procedure gracefully should an unforeseen error occur:

    On Error Goto ErrHandler

    ..... program code

    SubExit:
    Exit Sub

    ErrHandler:
    Select Case Err.Number
    Case 1234
    ' Do something specific for this error
    Case 2345
    ' Do something else specific for this error
    Case Else
    ' Display a generic message
    MsgBox "There has been an error. " & _
    "Please contact the program administrator." & vbCrLf &
    vbCrLf & _
    Err.Number & vbCrLf & Err.Description
    End Select
    Goto SubExit


    "b_bussoloti" wrote:

    > Folks,
    > I've been programming in C/C++, Java using OO Development and my teachers
    > always said that using GoTo in these languages will send you straight to hell.
    > In the past few weeks I managed to learn a bit of VBA to use in my work and
    > in sometimes it seems that using GoTo is a good deal, especially when I need
    > to throw an Error of some kind...
    > So I ask you:"Is GoTo a good programming method in VB/VBA? Is it not
    > comdemnable?"
     
  3. Ron2006

    Ron2006
    Expand Collapse
    Guest

    Programming philosophy........... so early in the week.

    The danger in goto is that you can go anywhere, which can drive someone
    trying to follow your code (or yourself 6 months and 3 applications
    from now) crazy.

    The usefullness (and I feel acceptable use) is to throw an error, to go
    to the end of a (sub)routine because NO other code applies.

    DON'T go to some place outside of your (sub)routine. You will get the
    program and the programmer confused.

    Probably the same rule applies for goto as does for all of life.

    Moderation in all things.

    IMHO

    Ron
     
  4. b_bussoloti

    b_bussoloti
    Expand Collapse
    Guest

    Thanks Ron,
    nice philosophical answer, it sure make's me think on my life

    "Ron2006" wrote:

    > Programming philosophy........... so early in the week.
    >
    > The danger in goto is that you can go anywhere, which can drive someone
    > trying to follow your code (or yourself 6 months and 3 applications
    > from now) crazy.
    >
    > The usefullness (and I feel acceptable use) is to throw an error, to go
    > to the end of a (sub)routine because NO other code applies.
    >
    > DON'T go to some place outside of your (sub)routine. You will get the
    > program and the programmer confused.
    >
    > Probably the same rule applies for goto as does for all of life.
    >
    > Moderation in all things.
    >
    > IMHO
    >
    > Ron
    >
    >
     
  5. b_bussoloti

    b_bussoloti
    Expand Collapse
    Guest

    Another question. Could I use GoTo in this case?:

    On Error GoTo ErrHandler

    ....... program code

    GoTo Finish (If I don't use this and implement my ErrHandler on the bottom
    of the code, every time the execution goes well it would enter the ErrHandler
    part in the ned, so I use this to skip that and End my Sub)

    ErrHandler
    (...)
    Exit Sub

    Finish:
    End Sub

    "b_bussoloti" wrote:

    > Folks,
    > I've been programming in C/C++, Java using OO Development and my teachers
    > always said that using GoTo in these languages will send you straight to hell.
    > In the past few weeks I managed to learn a bit of VBA to use in my work and
    > in sometimes it seems that using GoTo is a good deal, especially when I need
    > to throw an Error of some kind...
    > So I ask you:"Is GoTo a good programming method in VB/VBA? Is it not
    > comdemnable?"
     
  6. Douglas J Steele

    Douglas J Steele
    Expand Collapse
    Guest

    I'd recommend using:

    On Error GoTo ErrHandler

    ....... program code

    Finish:
    Exit Sub

    ErrHandler:
    (...)
    Resume Finish

    End Sub

    Using "Resume" ensures that the Error object is reset, so that you no longer
    have an error. Note that often there's "clean-up' type code that's required
    at the end of a routine that should be executed whether or not an error
    arose.

    --
    Doug Steele, Microsoft Access MVP
    http://I.Am/DougSteele
    (no e-mails, please!)


    "b_bussoloti" <bbussoloti@discussions.microsoft.com> wrote in message
    news:4E827CAB-D40E-43F8-8A7B-4D73173EA673@microsoft.com...
    > Another question. Could I use GoTo in this case?:
    >
    > On Error GoTo ErrHandler
    >
    > ...... program code
    >
    > GoTo Finish (If I don't use this and implement my ErrHandler on the bottom
    > of the code, every time the execution goes well it would enter the

    ErrHandler
    > part in the ned, so I use this to skip that and End my Sub)
    >
    > ErrHandler
    > (...)
    > Exit Sub
    >
    > Finish:
    > End Sub
    >
    > "b_bussoloti" wrote:
    >
    > > Folks,
    > > I've been programming in C/C++, Java using OO Development and my

    teachers
    > > always said that using GoTo in these languages will send you straight to

    hell.
    > > In the past few weeks I managed to learn a bit of VBA to use in my work

    and
    > > in sometimes it seems that using GoTo is a good deal, especially when I

    need
    > > to throw an Error of some kind...
    > > So I ask you:"Is GoTo a good programming method in VB/VBA? Is it not
    > > comdemnable?"
     
  7. Sprinks

    Sprinks
    Expand Collapse
    Guest

    Yes, Doug is right. "Resume Finish" is much better than a "Goto Finish"
    because it resets the Error.

    By the way, you can find a succinct (and humorous) answer to your original
    question in Commandment #9 of the following:

    http://www.mvps.org/access/tencommandments.htm

    Sprinks


    "b_bussoloti" wrote:

    > Another question. Could I use GoTo in this case?:
    >
    > On Error GoTo ErrHandler
    >
    > ...... program code
    >
    > GoTo Finish (If I don't use this and implement my ErrHandler on the bottom
    > of the code, every time the execution goes well it would enter the ErrHandler
    > part in the ned, so I use this to skip that and End my Sub)
    >
    > ErrHandler
    > (...)
    > Exit Sub
    >
    > Finish:
    > End Sub
    >
    > "b_bussoloti" wrote:
    >
    > > Folks,
    > > I've been programming in C/C++, Java using OO Development and my teachers
    > > always said that using GoTo in these languages will send you straight to hell.
    > > In the past few weeks I managed to learn a bit of VBA to use in my work and
    > > in sometimes it seems that using GoTo is a good deal, especially when I need
    > > to throw an Error of some kind...
    > > So I ask you:"Is GoTo a good programming method in VB/VBA? Is it not
    > > comdemnable?"
     
  8. Tony Toews

    Tony Toews
    Expand Collapse
    Guest

    b_bussoloti <bbussoloti@discussions.microsoft.com> wrote:

    >So I ask you:"Is GoTo a good programming method in VB/VBA? Is it not
    >comdemnable?"


    About the only time I really use it is when doing some basic data
    validation at the beginning of a routine. Or I might even, horror of
    horrors, use Exit Sub.

    For example ensuring that required fields have been entered. If not
    display a message and exit immediately.

    In the middle of code I will very seldom use this but there are times
    when it's required. However, in Access this almost always means that
    you have to close a recordset and do other cleanup. So I will set a
    boolean variable, exit the For loop, do the clean up and then goto to
    the exit routine tag.

    Tony
    --
    Tony Toews, Microsoft Access MVP
    Please respond only in the newsgroups so that others can
    read the entire thread of messages.
    Microsoft Access Links, Hints, Tips & Accounting Systems at
    http://www.granite.ab.ca/accsmstr.htm
     
  9. b_bussoloti

    b_bussoloti
    Expand Collapse
    Guest

    That's all i've been doing so far Tony.
    Thanks you all for the feedbacks

    "Tony Toews" wrote:

    > b_bussoloti <bbussoloti@discussions.microsoft.com> wrote:
    >
    > >So I ask you:"Is GoTo a good programming method in VB/VBA? Is it not
    > >comdemnable?"

    >
    > About the only time I really use it is when doing some basic data
    > validation at the beginning of a routine. Or I might even, horror of
    > horrors, use Exit Sub.
    >
    > For example ensuring that required fields have been entered. If not
    > display a message and exit immediately.
    >
    > In the middle of code I will very seldom use this but there are times
    > when it's required. However, in Access this almost always means that
    > you have to close a recordset and do other cleanup. So I will set a
    > boolean variable, exit the For loop, do the clean up and then goto to
    > the exit routine tag.
    >
    > Tony
    > --
    > Tony Toews, Microsoft Access MVP
    > Please respond only in the newsgroups so that others can
    > read the entire thread of messages.
    > Microsoft Access Links, Hints, Tips & Accounting Systems at
    > http://www.granite.ab.ca/accsmstr.htm
    >
     
  10. (PeteCresswell)

    (PeteCresswell)
    Expand Collapse
    Guest

    Per Sprinks:
    > End Select
    > Goto SubExit


    I have the same kind of error handling, but I use "Resume" instead of "GoTo"
    --
    PeteCresswell
     

Share This Page