Author Topic: Why never to use Goto.  (Read 23378 times)

0 Members and 2 Guests are viewing this topic.

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: Why never to use Goto.
« Reply #30 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.)
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline TsukasaZX

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 415
  • Rating: +46/-3
  • Never Gonna Give You Up!
    • View Profile
Re: Why never to use Goto.
« Reply #31 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... `-`;
« Last Edit: August 11, 2010, 07:12:17 pm by TsukasaZX »
IRC EFNet | Nick Su-Hime | Chan #omnimaga, #unss, #cemetech
Yumé - 100% Complete!
Yumé 2: Rika's Story - 10%
 - Hiatus until summertime
Yumé: The Eternal Dreams - 50%
 - World and events
TIBiC/GO C Library - N%

"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.

  ▲
▲ ▲ Triforce!

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: Why never to use Goto.
« Reply #32 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
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline Happybobjr

  • James Oldiges
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2325
  • Rating: +128/-20
  • Howdy :)
    • View Profile
Re: Why never to use Goto.
« Reply #33 on: August 12, 2010, 12:31:10 pm »
"BOO!", says goto to lbl.
School: East Central High School
 
Axe: 1.0.0
TI-84 +SE  ||| OS: 2.53 MP (patched) ||| Version: "M"
TI-Nspire    |||  Lent out, and never returned
____________________________________________________________

Offline TravisE

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 182
  • Rating: +33/-0
    • View Profile
    • ticalc.org
Re: Why never to use Goto.
« Reply #34 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.
ticalc.org staff member—http://www.ticalc.org/

Offline Happybobjr

  • James Oldiges
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2325
  • Rating: +128/-20
  • Howdy :)
    • View Profile
Re: Why never to use Goto.
« Reply #35 on: August 15, 2010, 05:40:28 pm »
good way to put it.
School: East Central High School
 
Axe: 1.0.0
TI-84 +SE  ||| OS: 2.53 MP (patched) ||| Version: "M"
TI-Nspire    |||  Lent out, and never returned
____________________________________________________________

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Why never to use Goto.
« Reply #36 on: August 15, 2010, 05:48:15 pm »
I wonder if Goto can cause memory leaks on Casio BASIC...

Offline Happybobjr

  • James Oldiges
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2325
  • Rating: +128/-20
  • Howdy :)
    • View Profile
Re: Why never to use Goto.
« Reply #37 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?
School: East Central High School
 
Axe: 1.0.0
TI-84 +SE  ||| OS: 2.53 MP (patched) ||| Version: "M"
TI-Nspire    |||  Lent out, and never returned
____________________________________________________________

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: Why never to use Goto.
« Reply #38 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?
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Why never to use Goto.
« Reply #39 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).

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Why never to use Goto.
« Reply #40 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. :)

Offline patriotsfan

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 118
  • Rating: +2/-0
  • Go Pats!
    • View Profile
Re: Why never to use Goto.
« Reply #41 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! ;)
« Last Edit: August 15, 2010, 09:36:31 pm by patriotsfan »


Doomsday: 12/21/2012 6:11 AM EST 11:11 AM GMT

Offline ralphdspam

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 841
  • Rating: +38/-1
  • My name is actually Matt.
    • View Profile
Re: Why never to use Goto.
« Reply #42 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. 
ld a, 0
ld a, a

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: Why never to use Goto.
« Reply #43 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.

Offline ralphdspam

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 841
  • Rating: +38/-1
  • My name is actually Matt.
    • View Profile
Re: Why never to use Goto.
« Reply #44 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.
« Last Edit: February 22, 2011, 10:40:34 pm by ralphdspam »
ld a, 0
ld a, a