Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: MRide on August 09, 2010, 07:48:58 pm

Title: Why never to use Goto.
Post by: MRide on August 09, 2010, 07:48:58 pm
Why never to use goto.

(http://imgs.xkcd.com/comics/goto.png)
Title: Re: Why never to use Goto.
Post by: apcalc on August 09, 2010, 08:01:33 pm
LOL :) That was a good one!  Some of my earlier programs use a ton a "gotos".  I should be careful to watch out if I will be eaten. :)
Title: Re: Why never to use Goto.
Post by: qazz42 on August 09, 2010, 08:04:38 pm
Man, that happened to me all the time back then, thank god I taste bad
Title: Re: Why never to use Goto.
Post by: fb39ca4 on August 09, 2010, 08:05:00 pm
lol!
When I was a total noob, I used gotos before I learned about while and repeat.
Title: Re: Why never to use Goto.
Post by: bwang on August 09, 2010, 08:06:37 pm
goto has its legitimate uses, though, such as escaping from multiple nested loops in a simple fashion.
Title: Re: Why never to use Goto.
Post by: jnesselr on August 09, 2010, 08:31:01 pm
I love Java's break statement. It changed my life. Before, I would just goto out of the loop.
Title: Re: Why never to use Goto.
Post by: Builderboy on August 09, 2010, 09:26:02 pm
goto has its legitimate uses, though, such as escaping from multiple nested loops in a simple fashion.

Its useful except when it creates memory leaks :P In TiBasic it can have good uses though, i use it a lot in Serenity a lot in the form of massive switch statements, i find its a lot faster then having a ton of If:Then blocks.
Title: Re: Why never to use Goto.
Post by: TIfanx1999 on August 09, 2010, 10:17:33 pm
Lolz, I remember that comic!
Title: Re: Why never to use Goto.
Post by: Happybobjr on August 09, 2010, 10:33:11 pm
i think he went a little to far back.
Title: Re: Why never to use Goto.
Post by: DJ Omnimaga on August 09, 2010, 10:36:07 pm
In Axe Gotos won't cause leaks fortunately. For massive BASIC programs (and even Axe), it gets hard to follow after a while, though. Also when using them in TI-BASIC, you have higher risks of accidentally causing memory leaks in your programs. I personally stopped using them after I finished The Reign Of Legends 3, in June 2004, with the exception of Illusiat TI-81 Remake, since the 81 has no While/Repeat/For() instruction. I have yet to use them in Axe.

Sadly, those memory leaks were not documented very well back in the days and tutorials on which it was were hard to find, so if you play stuff like Illusiat 11 or 12 or even ROL2, I suggest that you save often, else you can end up with ERR:MEMORY. Just to say how badly documented it was back then, FFTOM series have the game quit completly and tell the user to press ENTER to resume gameplay after exiting menus, before/after battles or even in the middle of battles.
Title: Re: Why never to use Goto.
Post by: SirCmpwn on August 09, 2010, 10:38:36 pm
You can use Goto \ End \ End to unleak the memory.  Just have as many Ends as you have loops or Ifs.
Title: Re: Why never to use Goto.
Post by: MRide on August 09, 2010, 10:39:47 pm
I haven't use a Goto in BASIC since I was newb and made a game entirely out of menus.  I have used it a little bit in Axe, though.
Title: Re: Why never to use Goto.
Post by: SirCmpwn on August 09, 2010, 10:41:15 pm
I use Goto all the time.  It's pretty useful if you know what you are doing.
Title: Re: Why never to use Goto.
Post by: Happybobjr on August 09, 2010, 10:44:18 pm
i use it when i am too lazy to rip apart the code and move something.   ie. interrupts.
Title: Re: Why never to use Goto.
Post by: DJ Omnimaga on August 09, 2010, 10:45:11 pm
Well one other issue I got with Goto is when you have an extremly large program and a Lbl is located at the complete bottom of the program, it seems to take quite a while to load. If you use them you really need to use them at the right places and properly
Title: Re: Why never to use Goto.
Post by: Magic Banana on August 09, 2010, 10:47:20 pm
I use Goto all the time.  It's pretty useful if you know what you are doing.
^This.

I still find a use for it, although whenever I use it I just do some checks to make sure no loops are being jumped out of or just kinda force it out (if condition, End, End, End, etc. until you are out then Goto.) I use it to jump back to the top of my code, where I can go anywhere depending on certain variables.
Title: Re: Why never to use Goto.
Post by: MRide on August 09, 2010, 10:48:28 pm
I usually write my code so I don't need Gotos.
I've never encountered something that couldn't be reworked easily. (But then, I've not done much coding where Gotos would be useful)

EDIT: 100 posts!
Title: Re: Why never to use Goto.
Post by: Builderboy on August 09, 2010, 10:55:29 pm
The best scenario i use goto in is this.  If a goto is called at the very beginning of a program, it can be the fastest way to get to the bottom of the program.  When i have large amounts of map data lets say, i have things like this

If T=1:Goto1
If T=2:Goto2
...

Lbl 1
MapData1
Return

Lbl2
MapData2
Return

Horribly memory inefficient, but faster than putting every room inside If:Then Blocks. 
Title: Re: Why never to use Goto.
Post by: MRide on August 09, 2010, 11:03:55 pm
Hm... that's faster?  I'll have to try that out then.  coincidentally, I'm actually doing the exact same thing right now.
Title: Re: Why never to use Goto.
Post by: tifreak on August 09, 2010, 11:19:54 pm
I stopped using Gotos and Lbls once I found an article on BASIC Guru that said it was possible to create games without them. That was.. I don't know, 10 years ago? I made my second game because of that article that used not one lbl or goto.

Simply by using a While loop as a main loop, and utilizing while loops and If:Then conditionals inside that loop, I can maneuver around in a program much faster than any silly Goto/Lbl combo.

The only draw back to coding with While loops in that manner is that the code can get confusing, so you have to take lots of notes on what you are doing, but once you get it figured out, it is worth using. Plus, lack of risking memory leaks is kinda nice. :p
Title: Re: Why never to use Goto.
Post by: Builderboy on August 09, 2010, 11:29:01 pm
I never use Goto's to create loops, only for special jumps or the like.  I sometime abuse them within subprograms if i know the memory leak is going to be erased 1 second after it is created :P
Title: Re: Why never to use Goto.
Post by: TIfanx1999 on August 10, 2010, 12:34:43 am
Sadly, those memory leaks were not documented very well back in the days and tutorials on which it was were hard to find, so if you play stuff like Illusiat 11 or 12 or even ROL2, I suggest that you save often, else you can end up with ERR:MEMORY. Just to say how badly documented it was back then, FFTOM series have the game quit completly and tell the user to press ENTER to resume gameplay after exiting menus, before/after battles or even in the middle of battles.
Yep, I remember those days. The dark ages of TI-BASIC... :P It was odd, because alot of us knew about ERR:MEMORY occuring, we just had the damnedest time figuring out what the hell was causing it, many times which GOTO was the culprit. It's great that so many more things are documented these days and newbs starting out have access to so much good information. :)
Title: Re: Why never to use Goto.
Post by: Happybobjr on August 10, 2010, 01:40:47 pm
Sadly, those memory leaks were not documented very well back in the days and tutorials on which it was were hard to find, so if you play stuff like Illusiat 11 or 12 or even ROL2, I suggest that you save often, else you can end up with ERR:MEMORY. Just to say how badly documented it was back then, FFTOM series have the game quit completly and tell the user to press ENTER to resume gameplay after exiting menus, before/after battles or even in the middle of battles.
Yep, I remember those days. The dark ages of TI-BASIC... :P It was odd, because alot of us knew about ERR:MEMORY occuring, we just had the damnedest time figuring out what the hell was causing it, many times which GOTO was the culprit. It's great that so many more things are documented these days and newbs starting out have access to so much good information. :)

it's heros like you guys that figured it out for our gen. :D
Title: Re: Why never to use Goto.
Post by: eriktheorange on August 10, 2010, 01:48:16 pm
I try to avoid using goto, but often I'm just too lazy.

Need to get back to a bit at the very start of the program? Whatever, goto. I'm just making dumb little games because I'm bored in class, so what?
Title: Re: Why never to use Goto.
Post by: DJ Omnimaga on August 10, 2010, 02:18:55 pm
There's one trick to reduce the slow down caused by Gotos in games. The trick is to put the game main menu at the complete bottom of the code. Start the program with Goto 0, which goes to the menu, and after Goto 0, the game engine and stuff should be there.
Title: Re: Why never to use Goto.
Post by: Happybobjr on August 10, 2010, 04:58:08 pm
There's one trick to reduce the slow down caused by Gotos in games. The trick is to put the game main menu at the complete bottom of the code. Start the program with Goto 0, which goes to the menu, and after Goto 0, the game engine and stuff should be there.

interesting, i will do that :)
Title: Re: Why never to use Goto.
Post by: yunhua98 on August 10, 2010, 05:02:11 pm
I generally prefer using subprograms and then recalling it it in and then use goto to go to it and loops broken by the goto's that jump back.  Like:

:Repeat 1
:Goto A
:End
<code>
:Label A
<subroutine>
:End
Title: Re: Why never to use Goto.
Post by: _player1537 on August 10, 2010, 05:36:15 pm
I generally use them to jump back to the beginning of the code, or like this:
:For(A,0,1
:If Not(A
:Goto AA
:End
<code>
:Lbl AA
<subroutine>
:End
Title: Re: Why never to use Goto.
Post by: fb39ca4 on August 10, 2010, 06:29:27 pm
The best scenario i use goto in is this.  If a goto is called at the very beginning of a program, it can be the fastest way to get to the bottom of the program.  When i have large amounts of map data lets say, i have things like this

If T=1:Goto1
If T=2:Goto2
...

Lbl 1
MapData1
Return

Lbl2
MapData2
Return

Horribly memory inefficient, but faster than putting every room inside If:Then Blocks. 
It's too bad there's no switch statement in TI-BASIC like there is in C.
Title: Re: Why never to use Goto.
Post by: patriotsfan on August 10, 2010, 06:59:20 pm
I don't think I have ever used Goto unless I am creating my own custom menu.
Title: Re: Why never to use Goto.
Post by: calcdude84se on August 11, 2010, 01:05:00 pm
The reason Goto is slow when the Lbl is near the bottom of program is because, IIRC, when the parser encounters a Goto it will search the entire program, beginning to end, until it finds it.
In general, though, yeah, Goto is frowned upon as bad programming practice. Its main advantages are optimizations when you really know what you're doing (or, if in ASM, for example, the language provides no other options.)
Title: Re: Why never to use Goto.
Post by: TsukasaZX on August 11, 2010, 07:10:25 pm
Personally, I don't think labels and Goto's should be considered "bad programming practice". While, yes, they do have their downsides (such as potentially causing memory leaks) and many programmers believe it's better to use While/For/Repeat loops in whatever combination necessary to avoid ever using a label and goto, I don't think we should necessarily call them "bad".

I know from experience that it's often a bit difficult to do things in nested loops rather than with Label-and-Goto and I feel that if we try to push a "gotos are bad programming" sort of agenda, we may end up frustrating new programmers to the point of quitting ("I can't figure out how to do this but I can't use labels and goto's because they'll ridicule/laugh at/hate/whatever me. ARRGH this is too hard, I quit" sort of deal)


@Builderboy Alternatively, put all your map data in a subprogram like this:
Code: [Select]
if VARIABLE = 1
[matrix] -> MATRIX VARIABLE

so on and so forth...
and then whenever you need to change the map, alter the variable and call the subprogram :P

[EDIT] Didn't notice this was a 3 page long thread...  my post might be a bit... redundant... `-`;
Title: Re: Why never to use Goto.
Post by: calcdude84se on August 11, 2010, 07:12:14 pm
I'm not saying that they are bad to use, and if you feel you must use them, go ahead. ;D
No need to scare away new members :P
Title: Re: Why never to use Goto.
Post by: Happybobjr on August 12, 2010, 12:31:10 pm
"BOO!", says goto to lbl.
Title: Re: Why never to use Goto.
Post by: TravisE on August 14, 2010, 06:06:42 pm
I also remember the days when I used to use Goto all the time. I also ended up having to figure out myself what was causing the TI-BASIC “memory” error on the Z80 calcs. What's ironic is that it actually had the reason listed right in my manuals where the error message was explained, but I ignored it because I misunderstood and thought it was wrong—I thought it meant that any time you use Goto in a loop, it would immediately give that error, which of course it doesn't. :P The fact that it has to be done repeatedly before that happens and the manual didn't make this fact clear caused it to be a long time before I finally figured it out.

This was also around my old DOS QBasic programming days, and I remember causing memory leaks at first by using Gotos in terrible places in my code on that platform, too. :D Later, I discovered how to add debugging code that would print the amount of memory remaining and was shocked watching it decrease steadily the whole time the program ran. In QBasic, it was rarely a real issue, though, because there's so much more memory available on a PC compared to a calc.

Over time, as I read programming books and experimented with different styles, I gradually used Goto less and less. These days, I pretty much never use it at all—it's become natural for me to write code without using it.

There is just one exception currently—in recent C programs (i.e., TIGCC) I do make very occasional use of Goto when dealing with dynamic memory allocation, which is otherwise a ridiculous pain when it comes time to properly free up that memory (especially on the 68K calcs, where nothing cleans up after you and the memory is lost forever until RAM is reset!). Without Goto, I would have to repeat the cleanup code many times everywhere, wasting space and giving countless opportunities for hard-to-reproduce bugs/memory leaks to hide (and making the code actually harder to read). In this particular case, I think Goto is okay because the alternative is far worse.

So I consider Goto as being kind of like alcohol. There's nothing wrong with it as long as you use it responsibly and don't get carried away.
Title: Re: Why never to use Goto.
Post by: Happybobjr on August 15, 2010, 05:40:28 pm
good way to put it.
Title: Re: Why never to use Goto.
Post by: DJ Omnimaga on August 15, 2010, 05:48:15 pm
I wonder if Goto can cause memory leaks on Casio BASIC...
Title: Re: Why never to use Goto.
Post by: Happybobjr on August 15, 2010, 05:54:09 pm
I wonder,  I have never had one,  are they as bad as ti in some areas? such as Customer care?
Title: Re: Why never to use Goto.
Post by: thepenguin77 on August 15, 2010, 06:02:03 pm
Hmmm...
Code: [Select]
:0->A
:Lbl 1
:A+1->A
:While 1
:Goto 1

After the error, (total ram available - program size) / A = ~33.

Why does TI need 33 bytes per while?
Title: Re: Why never to use Goto.
Post by: DJ Omnimaga on August 15, 2010, 06:17:05 pm
I wonder,  I have never had one,  are they as bad as ti in some areas? such as Customer care?
I'm not sure anymore. I heard some bad stuff about Casio that sounded like TI. The good thing, though, is that originally, the Classpad 300 (their touchscreen calc) from 2003 was locked down from ASM dev. The BASIC language was much better than the Nspire right now, but most people in the community wanted ASM/C. Casio responded with a SDK 2 years later, something TI has yet to do with the Nspire.

Casio BASIC speed vs CPU ratio is much worse than TI, though. For example, on their 29 MHz calc, a BASIC For( loop counter will run at the same speed as on the 6 MHz 83+. Some commands are faster on this model, though, like text displaying (which lets you do semi flickerless grayscale).
Title: Re: Why never to use Goto.
Post by: ztrumpet on August 15, 2010, 07:54:04 pm
I rarely use in Basic anymore, but I used to.  Look at my original version of Numb3rs to see how bad it was!  By the time I finished Drifter I was slowly drifting away from Gotos, and Exodus doesn't use any.  Gotos, for me, are only good for putting subroutines inside the main program, like player demonstrated on page 2. :)
Title: Re: Why never to use Goto.
Post by: patriotsfan on August 15, 2010, 09:32:31 pm
By the time I finished Drifter I was slowly drifting away from Gotos, and Exodus doesn't use any.
Nice use of pun there! :D

EDIT: I mean word play! ;)
Title: Re: Why never to use Goto.
Post by: ralphdspam on February 22, 2011, 10:10:50 pm
At school, we have to write these basic programs with terribly unoptimized code. 
I know there are situations where the use of Lbl and Goto is inevitable, but all of those programs use the Goto and Lbl commands as like there is no tomorrow.

The programs are so slow that I have a race to see whose optimized code is faster. 
Title: Re: Why never to use Goto.
Post by: jnesselr on February 22, 2011, 10:30:30 pm
*ahem*
HOLY NECROPOST BATMAN
well, not really, 'cause it's > 2 months, but that's another story entirely.  I wish we even could learn to program at my school.
Title: Re: Why never to use Goto.
Post by: ralphdspam on February 22, 2011, 10:40:00 pm
Yes, they try to teach, but most a few see it as an opportunity to play Block Dude.
Title: Re: Why never to use Goto.
Post by: DJ Omnimaga on February 22, 2011, 11:01:13 pm
This topic reminds me how I was forced to use Goto to create loops on the TI-81 due to lack of While/Repeat/For() O.O
Title: Re: Why never to use Goto.
Post by: TIfanx1999 on February 24, 2011, 09:10:30 am
Wow, the TI-81 was pretty limited. It's sad that TI-81 BASIC is still more capable than Nspire BASIC though. :P
Title: Re: Why never to use Goto.
Post by: Deep Toaster on February 24, 2011, 09:12:11 am
At school, we have to write these basic programs with terribly unoptimized code. 
I know there are situations where the use of Lbl and Goto is inevitable, but all of those programs use the Goto and Lbl commands as like there is no tomorrow.

Yeah, even though my math teacher actual knows programming, he always, always uses IS<( / Goto instead of a simple For( loop. To keep things simple for everyone else, he says.
Title: Re: Why never to use Goto.
Post by: TIfanx1999 on February 24, 2011, 09:14:09 am
Wow, I don't think I've ever used IS<(. O.O
Title: Re: Why never to use Goto.
Post by: sjasogun1 on February 24, 2011, 09:14:49 am
Casio doesn't have repeat, so it's almost nessecary to have goto to restart the loop from several conditional statements inside of the loop without causing an error (which it will if you use Do-LpWhile or While-WhileEnd
Title: Re: Why never to use Goto.
Post by: Deep Toaster on February 24, 2011, 09:23:37 am
Wow, I don't think I've ever used IS<(. O.O

Me neither. IMHO For( is actually easier to understand :P
Title: Re: Why never to use Goto.
Post by: Munchor on February 24, 2011, 12:28:13 pm
This topic reminds me how I was forced to use Goto to create loops on the TI-81 due to lack of While/Repeat/For() O.O

Like:

Code: [Select]
0->A
Lbl L
DISP "TRY
A+1->A
If A<100
Then
Goto L
End

Stuff like this?
Title: Re: Why never to use Goto.
Post by: z80man on February 24, 2011, 12:34:15 pm
This topic reminds me how I was forced to use Goto to create loops on the TI-81 due to lack of While/Repeat/For() O.O

Like:

Code: [Select]
0->A
Lbl L
DISP "TRY
A+1->A
If A<100
Then
Goto L
End

Stuff like this?
That reminds me of how I code loops in asm. otherwise in BASIC that's how I coded when I first started using the language.
Title: Re: Why never to use Goto.
Post by: Deep Toaster on February 24, 2011, 02:23:41 pm
This topic reminds me how I was forced to use Goto to create loops on the TI-81 due to lack of While/Repeat/For() O.O

Like:

Code: [Select]
0->A
Lbl L
DISP "TRY
A+1->A
If A<100
Then
Goto L
End

Stuff like this?

Probably with IS>(, I'd imagine.
Title: Re: Why never to use Goto.
Post by: Ashbad on February 24, 2011, 04:04:25 pm
I love Java's break statement. It changed my life. Before, I would just goto out of the loop.

++
Title: Re: Why never to use Goto.
Post by: Deep Toaster on February 24, 2011, 04:23:57 pm
I love Java's break statement. It changed my life. Before, I would just goto out of the loop.

++

Yep. I love the fact that Java doesn't support goto at all but has a break command for when you really need it.
Title: Re: Why never to use Goto.
Post by: DJ Omnimaga on February 25, 2011, 03:20:15 am
Casio doesn't have repeat, so it's almost nessecary to have goto to restart the loop from several conditional statements inside of the loop without causing an error (which it will if you use Do-LpWhile or While-WhileEnd
That sucks. X.x Does Goto cause memory leaks, though?
This topic reminds me how I was forced to use Goto to create loops on the TI-81 due to lack of While/Repeat/For() O.O

Like:

Code: [Select]
0->A
Lbl L
DISP "TRY
A+1->A
If A<100
Then
Goto L
End

Stuff like this?

Probably with IS>(, I'd imagine.
With IS>(, indeed. Here's an example:

0->U
Lbl 1
DispHome
DispGraph
IS>(U,5
Goto 1

That's the equivalent of:

For(Z,0,5
Disp
DispGraph
End

Or maybe it was For(Z,0,4, I dunno...
Title: Re: Why never to use Goto.
Post by: GreaseMonkey on February 25, 2011, 03:32:43 am
Yes, Goto on a CASIO can leak:

Code: [Select]
Lbl 1
Do
Goto 1

Ends with a lovely Mem ERROR.
Title: Re: Why never to use Goto.
Post by: AngelFish on February 25, 2011, 08:12:13 am
Is that really a memory leak, though? You're telling the OS to continually allocate memory for Do statements. It's nothing more than infinite nesting. From what I understand, memory leaks are bugs in the code where the interpreter doesn't properly handle the memory allocations. Your example wouldn't count unless that program somehow terminates earlier than it should.
Title: Re: Why never to use Goto.
Post by: JosJuice on February 25, 2011, 08:26:16 am
Is that really a memory leak, though? You're telling the OS to continually allocate memory for Do statements. It's nothing more than infinite nesting. From what I understand, memory leaks are bugs in the code where the interpreter doesn't properly handle the memory allocations. Your example wouldn't count unless that program somehow terminates earlier than it should.
His program is an example of what has (incorrectly?) been called a memory leak for a long time in the TI community, but you are right.
Title: Re: Why never to use Goto.
Post by: Compynerd255 on February 25, 2011, 10:34:51 am
I'm going to throw in my own two cents.

Goto-Lbl statements are only good when no workable alternative exists. (e.g. subroutines or loops don't exist in the language) For example, While loops in Applesoft BASIC have to be created by IF x THEN GOTO y statements at the end of the loop. Fortunately, this does not create a memory leak, unless you happen to be inside a For loop.

As for TI-BASIC, While loops and If statements can create the effect of a subroutine, but that solution is only for people who want to keep all their code in one program. My game DIE FIEND used this Goto pattern to create subroutines and program branching, but I realized how leaky that code was.
Another alternative is to store your game in multiple subprograms, use an ASM lib to unarchive them (or tell people not to) then ship the game as a group file. That's what I did with Morale, my xLib war game.
And, in Axe, there is virtually no reason for Goto statements at all.
Title: Re: Why never to use Goto.
Post by: Builderboy on February 25, 2011, 01:14:08 pm
I agree with everything except goto statements in axe :P while i think there isn't a common use for them, they can have good uses for optimization, as well as ways to jump out of a large nests of loops.
Title: Re: Why never to use Goto.
Post by: AngelFish on February 25, 2011, 03:22:43 pm
And, in Axe, there is virtually no reason for Goto statements at all.

I think you meant to say
Quote
And, in Axe, there is virtually every reason for Goto statements.
 

:P
Title: Re: Why never to use Goto.
Post by: Compynerd255 on February 25, 2011, 05:05:59 pm
And, in Axe, there is virtually no reason for Goto statements at all.

I think you meant to say
Quote
And, in Axe, there is virtually every reason for Goto statements.
 
:P
No, I actually meant to say that. True, every reason for a Goto statement exists in Axe, and I can even see using it to break out of loops. But for the most part, I don't use them. I come from a C# programming background, and it is more trouble to program a Goto statement in C# than it is to use something else, such as a Break or Continue statement (That would actually be a cool feature request for Axe!). But in Axe, a Goto statement can serve the same purpose.
Title: Re: Why never to use Goto.
Post by: Deep Toaster on February 25, 2011, 08:04:41 pm
Axe is so strictly translated to ASM that goto statements (while a bad habit in just about every other language) is almost essential for optimization.
Title: Re: Why never to use Goto.
Post by: DJ Omnimaga on February 25, 2011, 10:32:28 pm
Is that really a memory leak, though? You're telling the OS to continually allocate memory for Do statements. It's nothing more than infinite nesting. From what I understand, memory leaks are bugs in the code where the interpreter doesn't properly handle the memory allocations. Your example wouldn't count unless that program somehow terminates earlier than it should.
Well TI-BASIC Goto ERR:MEMORY are refered to as memory leaks too, but I don't know if it's the right term. It just has been widely used for over a decade in the TI community. It sucks Casio also has them...