Should you use GoTo?

GOTO and GOSUB had a place and purpose in PROCEDURAL Languages… but basically no place in Object Oriented programming at all. ASM is procedural… Xojo, VB.NET etc are not. OLD line number Basics were also Procedural

@Michael Hußmann … Did your AI applications implement dynamic learning and polymorphic scenarios where the code itself had to change “at will” of the AI…sometimes no longer using portions of the code once new methods had been “learned”, writing it’s own code if needed? In the scenario I speak of, the main functions of the AI were hardcoded, but it has it’s own interpreted code from which it dynamically changed (eliminating code could have been deleted, although some code must be revisited for conditional testing when 'all-else-fails) Hard coding can only achieve so much. Even the human brain uses GOTO sequences when it re-wires or forms new neural channels (I believed goto was useless too until I argued it out with a professor from MIT hell-bent on replicating consciousness)… Then, it made sense. But under normal circumstances such as conversational AI and game-based AI, GOTO is not necessary, I agree.

I’m afraid I need some education here:
I remove selected rows from a multi-selectable listbox. What would be the shortest and most effective code to replace this old Goto snippet here?

[code] Dim lb As Listbox = Me.ListboxLinkBox
Dim nRows, i As Integer
Dim FirstSelectionIdx As Integer = -1

doSTART:
nRows=lb.ListCount-1
For i=0 to nRows
  If lb.Selected(i) then
    
    // Store index of first selected record
    If FirstSelectionIdx = -1 Then FirstSelectionIdx = i
    
    // Remove Row from Listbox
    lb.RemoveRow(i)
    
    // Use the 'bad' GOTO because with each RemoveRow(i),
    // we might run into an Objectnotfound exception
    GOTO doSTART
  End if
Next[/code]
  For i=nrows downto 0
      If lb.Selected(i) then
        
        // Store index of first selected record
        If FirstSelectionIdx = -1 Then FirstSelectionIdx = i
        
        // Remove Row from Listbox
        lb.RemoveRow(i)
        
   
      End if
    Next

Sigh - of course, downto! Sometimes I forget the most obvious.

and change this too

// Store index of first selected record
If FirstSelectionIdx = -1 Then FirstSelectionIdx = i

to (you want to keep the LAST one since you are going reverse order)

// Store index of first selected record
FirstSelectionIdx = i

If Execution going “close to the metal” with processor ticks counting on the results. → GOTOs are allowed.
If structured programming and best practices are more important than counting ticks → Run away from GOTOs

You are confusing dynamic rules with processing, your knowledge tree is just data not processing code.

As for “Polymorphic scenarios” if you cite a rule as IT NEEDS TO BE THIS WAY, there is no dialog. The only time I remember writing polymorphic code was in assembly language creating jumping tables and some tricks, and at this level, GOTOs are the IFs, so they need to be there everywhere.

[quote=56750:@Dave S]and change this too
… [/quote]
Right. Thanks!

[quote=56683:@Eric Williams]Perhaps I should send you my code for inspection.l :slight_smile:

I’m pretty confident it was the right thing to do in my specific circumstance. Thousands of lines of text, each one needs to be inspected, it’s a lot faster to use IF…THEN instead of RegEx, so once a match is made we skip all the rest of the tests.[/quote]

Them’s fightin’ words. :slight_smile:

I’ll take up the challenge of using IF statements vs. using RegEx. The native implementation can be slow in some circumstances, but I haven’t found a case yet where coding the long way is faster than the MBS implementation of the PCRE engine, as long as you can express your tests as a pattern, of course.

I’d also be interested to see if this is a good real-world example that I can use for my next column in XDev (with your permission, of course).

[quote=56711:@Michel Bujardet]Goto is a controversy based on quasi-religious beliefs :wink:
[/quote]

No, not true. GOTO is a problem because it sets up unpredictable execution paths that both programmers and automated testing tools have problems fully conceptualizing. The same kinds of problems arise with “return” statements that are not at the end of a function. Note that you’ll find people here who would never use a goto, but have no trouble returning mid-function ;-). In the golden age of programming when programmers were being trained to write code on big expensive military and aeronautics projects, such widely frowned upon practices resulted in hundreds of millions of dollars of cost overruns, crashed and destroyed prototypes, and probably more than a few deaths.

In the past 25 years, the only place I’ve ever contemplated (and then used) a goto was in low-level optimized C code that included AltiVec and SSE instructions. This was highly structured, repetitive, tight code. I’m pretty sure I could open it up today and make perfect sense of it because it’s heavily documented as well.

I disagree with Matthew Combatti’s reasoning for high level languages like Xojo and PHP. If you’re having a performance problem that Goto seems to solve, you haven’t found a good structure for your code blocks, or perhaps the language is doing something unexpected like yielding to another thread. I’m not going to take the time to find that better structure for you, but if you’re using Gotos in high level languages, it does exist. It may be more costly for you to find than the spaghetti code you’re generating though.

At any rate, to those who wonder about this, it’s your code. Do as you wish. When you work with others, enforce your will if you can and you wish. Just keep your gotos away from any software that might hurt someone else :-).

[quote=56791:@Brad Hutchings]At any rate, to those who wonder about this, it’s your code. Do as you wish. When you work with others, enforce your will if you can and you wish. Just keep your gotos away from any software that might hurt someone else :-).

[/quote]

Glad to see you are for the freedom of religion, Brad :smiley: LOL

Thou shalt not use fiendish GOTO and crooked half way Returns. Memory leaks and punishing bugs would ensue thy sins :wink:

In the instance of quasi Xojo of PHP:

Function ExecutePHP(script as string, postdata as string)as string

Do while still a php line delimited by ";"
//we're in a loop until last line with over 3000 if thens
//output is actually a string builder to append to the existing output string
//but in this case lets just say output = output + whatever

line = line + 1

if cmd = "print" then
 output = output + ParseParam(GetParam(para,1))
 goto NextLine
end if

if cmd = "file_get_contents" then
 //open file get contents as string
 output = output + filestringcontents
 goto NextLine
end if

if cmd = "strpos"
  output = output + instr(0, ParseParam(GetParam(para,1)), ParseParam(GetParam(para,2)))
 goto NextLine
end if

if cmd = "if"
  //handle parsing of if-then statements
end if

only 2996 more if thens to go....

NextLine:
Loop

return Output
exit function

//Error reporting
exception nilobj as NilObjectException
return "An error occurred on line " + str(line) + ". There was a NilObjectException"

//and the rest of the other dozen and a half error handlers
End Function

Goto increases the speed by 25 fold… so what would be more ‘luxurious’ of a method than is as above. Without goto it still functions, but goes from 20microseconds to 500+ microseconds to process? Just curious for an answer how it should be then since the code is straight-forward, no room for error to occur on parsing, and Goto has exponentially increased its processing speed? This is just a rough outline of the PHP interpreter coded in Xojo looks…

In Xojo I would use Continue instead of the Goto since it’s all inside the while loop.

Continue will skip over the remainder of the if thens? I was not aware of this keyword :slight_smile: always something to learn :slight_smile: I will do a speed test and see if there is a difference.

Nevermind. I just never thought to use continue. After looking at the reference…it has been quite a while since I’ve used continue… long enough that goto in this instance came to mind :slight_smile: Thanks Bob!

No worries. The Xojo language has a number of shortcuts that are useful. Not always easy to find about them. And like with many things it’s not perfect in all situations.

[quote=56794:@Michel Bujardet]Glad to see you are for the freedom of religion, Brad :smiley: LOL

Thou shalt not use fiendish GOTO and crooked half way Returns. Memory leaks and punishing bugs would ensue thy sins :wink:
[/quote]

If you want to believe it’s a religious argument, I’d prefer to just be kind and not tip over your apple cart, because it’s probably inconsequential. When you want to improve your game, and think I might be able to offer some tips, I probably have a few. Coding without goto or multiple exit points simply requires you to have a few basic execution patterns ready to deploy. After a little bit of practice, it becomes second nature. And you avoid so many pitfalls by default.

and in PHP I would have used SWITCH (ie. SELECT CASE)

If all of your IF’s are of the form if cmd = "something" then, then I’d expect SELECT CASE to work quickly here too, and make it easier to read:

select case cmd
case "print"
…
case "file_getcontents"
…
case "strpos"
…
case "if"
…
case …
…
end select

The code is actually more complex than shown and couldn’t use select case (was worse than if/then without gotos)…in my first attempts I used select case and the code was obnoxiously slow in comparisson to using if/thens… (kind of like the new mysql plugin for xojo…greatly slowed down than previous versions). In most cases, I’ll suffer speed and processor over best practices :slight_smile:

*Bob - Replacing goto with continue works and the speed test is almost identical (varies from 14-30ms each run…as with goto and if/then)