• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

The GOTO Gotcha!

Started by Donald Darden, August 03, 2007, 10:16:31 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

MikeTrader

>Is here anyone who never uses GOTO?

I try to eliminate all GOTO's

The main reason is that in a big program I might have ASM and Basic. In the ASM code there are lots of them so if your naming is not unique, you are going to have problems!

I use a lot of cut an paste ASM and have to rename most ofthe jmp/GOTO's
In basic code I make use of the DO/LOOP for ways to jump out - a technique I learned from Jose here. I have yet to encounter a situation that cannot be handled quite cleanly with an IF or DO/LOOP combo.

Personally i hate deeply nested IF statements. I try to code using the reverse logic

instead of
IF condition = %TRUE THEN

  ' code to execute
END IF

I prefer:

IF NOT Condition then EXIT DO/FUNCTION
' code to execute
'
'
'
'

This avoids code becoming so far indented you cant figure out what code is inside which Condition...



Donald Darden

Indenting is a very useful tool for showing the structure of code and making it easier to follow.  However, I typically limit my indentation step to 2 places, not the four or more that others use.  This means I can show more indented code than those others can.  I tried 1 step, but it's easier to follow 2 step increments vertically when you are looking for the beginning or end point of an indent stage.

I don't really care if people use GOTO or not.  I think most of the resistance to using it are either outmoded or without due consideration, which was my point in the first place.  To say that you also believe (like a religious thing) that the use of GOTO is either immoral or increases your risk of somehow reducing your program to a pile of cold spagetti code seems rather redundant.  The use of GOTOs is only an excuse for writing bad code; not a cause for it.

I might also point out that avoiding the explicit use of GOTO is to ignore that the compiler implements it everywhere, or at least the assembler equivalent.  For instance, every THEN, ELSE, ITERATE, EXIT, LOOP, and CASE statement has a goto structure involved.

José Roca

 
The DO/LOOP technique mentioned by Mike is useful to avoid multiple nested IFs.

For example, instead of:


   ' Create an instance of the HTTP service
   ppWHttp = WinHttpCreateObject("WinHttp.WinHttpRequest.5.1")
   IF ISTRUE ppWHttp THEN
      ' Open an HTTP connection to an HTTP resource
      IWinHttpRequest_Open(ppWHttp, "GET", "http://www.powerbasic.com/")
      IF ISFALSE WinHttpError THEN
         ' Send an HTTP request to the HTTP server
         IWinHttpRequest_Send ppWHttp
         IF ISFALSE WinHttpError THEN
            ' Get the response entity body as a string
            strResponseText = IWinHttpRequest_GetResponseText(ppWHttp)
            IF ISFALSE WinHttpError THEN
               MSGBOX strResponseText
            END IF
         END IF
      END IF
   END IF


I sometimes use:


   DO
      ' Create an instance of the HTTP service
      ppWHttp = WinHttpCreateObject("WinHttp.WinHttpRequest.5.1")
      IF ISFALSE ppWHttp THEN EXIT DO
      ' Open an HTTP connection to an HTTP resource
      IWinHttpRequest_Open(ppWHttp, "GET", "http://www.powerbasic.com/")
      IF WinHttpError THEN EXIT DO
      ' Send an HTTP request to the HTTP server
      IWinHttpRequest_Send ppWHttp
      IF WinHttpError THEN EXIT DO
      ' Get the response entity body as a string
      strResponseText = IWinHttpRequest_GetResponseText(ppWHttp)
      IF WinHttpError THEN EXIT DO
      MSGBOX strResponseText
      ' Exit
      EXIT DO
   LOOP


But I'm not afraid of GOTO, if judiciously used, so sometimes I use:


   ' Create an instance of the HTTP service
   ppWHttp = WinHttpCreateObject("WinHttp.WinHttpRequest.5.1")
   IF ISTRUE ppWHttp THEN GOTO LExit
   ' Open an HTTP connection to an HTTP resource
   IWinHttpRequest_Open(ppWHttp, "GET", "http://www.powerbasic.com/")
   IF ISTRUE ppWHttp THEN GOTO LExit
   ' Send an HTTP request to the HTTP server
   IWinHttpRequest_Send ppWHttp
   IF ISFALSE WinHttpError THEN GOTO LExit
   ' Get the response entity body as a string
   strResponseText = IWinHttpRequest_GetResponseText(ppWHttp)
   IF ISFALSE WinHttpError THEN GOTO LExit
   MSGBOX strResponseText

LExit:


And sometimes I use exception handling.

For some reason, some people always write clean code no matter which technique or language uses, whereas others don't.

Theo Gottwald

I do it like Jose.

The Reason is, that I want my Sub-Programs (as well as my programs) to have a defined "Exit-point".

If I have a lot of "EXIT SUB" everywhere in my code, this would not be the case.

Donald Darden

Ond style QBasic error trapping using ON ERROR GOTO caused a lot of people to write massive multi-error detection processes, and they even had to use ERL to determine which line number was involved when the error occurred.

VERY awkward.

I found it much better to either check for and process the error directly, or use the ON ERROR GOTO statement to jump to a unique process that only checked for an error that was related to the current task.

Another method was to use a variable to capture the error state if one occurred.  Thus, I would use ON ERROR GOTO to jump to the following statement:

goterror:
  errstat = ERR
  IF errstat = 0 THEN errstat = -1
  RESUME NEXT

Trying different methods can be useful, allowing you the chance to try and get a balance between common code and specific needs.  I noted that the particular compiler being used, and the manner in which it supported error trapping, was the most important determinant in which method worked best.  In fact, error processing can be one of the more difficult portions of code to convert when adapting source code from one BASIC compiler to another. It's been one of the areas in PowerBasic that has changed the most since the early versions.