Is GoTo a good programming method?

  • Thread starter Thread starter Guest
  • Start date Start date
G

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?"
 
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
 
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
 
Thanks Ron,
nice philosophical answer, it sure make's me think on my life
 
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
 
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.
 
b_bussoloti said:
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
 
That's all i've been doing so far Tony.
Thanks you all for the feedbacks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top