Re: GOTOs to the People - Discussion please!

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




"Lorin" <Lorin@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:A9570BA0-E676-4268-83E6-9C5E4E0A6A7D@xxxxxxxxxxxxxxxx

On Error GoTo Geneva ' for a convention

This brings up the discussion of GOTO since On Error GOTO is a GOTO.

This is the VB6 code I use on occassion if pressed

Public Function MySub(sMyFile as String) as String()
ON ERROR GOTO MySubErr
Dim lFNbr as long
Dims sBuffer as String
Dim sArgs() as String
Dim iX as Integer

sBuffer = String$(50," ")
' some code
Do
100 Open sMyFile For Binary As lFnbr

200 Get lFNbr,, sBuffer ' what happens if there are not that many
characters in the file?
If Split(sBuffer,",")(0) = "VALID" Then ' just making a point
sArgs=Split(sBuffer, ",") ' now get all
For iX = 0 to UBound(sArgs)
if sArg(iX) = "OOPS" then Exit Do ' a convieient
was
to escape using this GOTO
' Note it skips
out
of the FOR as well as the DO
if sArg(iX) = "GOSH" Then GoTo FlyingLeap ' another type of
GOTO
' Now which is easier to follow?
' Can your eye find the label first or the end of the Loop?
' Labels are automatically left justified.
' what if this was in a complex multilayered nested bit of
code?
Next iX
MySub = sArgs
EndIf
Loop While 0 ' solely the target of a GOTO, can you spot it?
' mixed message, I know, but what the heck.

FlyingLeap: ' label are cheap, and I can search to find the source of
this
destination
' some additional code goes here to do something very usefull

MySubExit:
Close lFnbr ' it always closes due to controlled exiting
' cleanup even from errors
Exit Sub

MySubErr:
Debug.Print Err.Description
Select case ERL
Case 100
' do something here in code
' if there is a possibility on an error at this point then call a
subroutine with its own error handling to do the work and catch an error
Case 200
' do something else here in code
Case Else
' handle other errors here
End Select
' all errors resume to MySubExit
Resume MySubExit

End Sub ' MySub <---- do you know why I always do this?

I know, all you purists go HISSSS and BOOOO.
THis is not Spaghetti, just clear and responsible coding.

Any discussion????

Stirring the pot.


VB6's comprehensive frame-based exception handling for guarding code blocks
was rather advanced for its time. (cf. VC++ at the time had at least 3
different incompatible methods). Utilizing exception handling is absolutely
essential in developing a professional application, since in a compiled app
an unhandled error is FATAL and will likely cause the entire app to crash.

Error handling was implemented using Labels opposed to adding new keywords
or complex contructs to the language. When utilized correctly it can be
quite eloquent. While it may use the label mechanism (and syntax) don't
confuse it with the archiaic use of Gotos.

The only 'purity' in my hiss and boo is to note your code is *pure* classic
spaghetti code the result of muddled thinking.

-ralph



.



Relevant Pages

  • Tail recursion syntactic sugar faked with TAGBODY-based construct?
    ... and provide a GOTO that takes argument expressions. ... in which a a label parameter is shadowed, ... (let (labels forms thunks thunk-gensyms) ... vars gensyms)) ...
    (comp.lang.lisp)
  • Re: On Local Error Goto Somewhere
    ... an EXIT DO or EXIT FOR. ... going if there is no label to show you the destination. ... IMO the GoTo statement has never been "THE" problem. ... ON ERROR GOTO MySubErr Dim lFNbr as long ...
    (microsoft.public.vb.general.discussion)
  • Re: function pointers
    ... The simple goto. ... fixed target label. ... The computed goto, aka switch, select, or caseof, depending ... In C it is the switch. ...
    (comp.lang.c)
  • Re: COBOL aint quite dead - yet !
    ... If you accept a definition of GOTO's as a transfer of control, ... would probably not have said that a goto by any other name would still be a ... label then, in a program that has gotos, it is not possible to know ... the SECTION keyword is an assurance that there will be no down- ...
    (comp.lang.cobol)
  • [BUG] futex_handle_fault always fails.
    ... jump to the pi_faulted label. ... From the retry label, with ret still zero, we again hit EFAULT on the ... goto retry; ...
    (Linux-Kernel)