goto

BlitzMax Forums/BlitzMax Programming/goto

Tachyon(Posted 2008) [#1]
From the example in the documentation:

Print "one"
Goto here
Print "two"
#here
Print "three"

This won't compile in SuperStrict mode.

I know using "goto" is bad programming, but I have one small area where this would be the perfect solution for me if it worked as described in the example.


CS_TBL(Posted 2008) [#2]
That's right. And I bet 99.99% of the users here would not advice to use goto anyway.


tonyg(Posted 2008) [#3]
Not sure what you're asking. Goto isn't available in SuperStrict mode although the documentation doesn't give much info about it.
If you explain what the special case is people might have some suggestions.


Brucey(Posted 2008) [#4]
but I have one small area where this would be the perfect solution

Then your design is flawed ;-)


ImaginaryHuman(Posted 2008) [#5]
Use superstrict while you design your code and then switch it off when you compile your final version?

Goto is not allowed in SuperStrict mode. There's nothing wrong with it if that's how you like to program.


Tachyon(Posted 2008) [#6]
It's all good...it was easy enough to alter this particular function to so it would flow like I needed. I was really looking for a (lazy) way of doing it when I came across the goto command in the documentation and thought: hey, why not give it try?

But this doesn't change the fact that the example posted in the documentation is fundamentally wrong, which was really more the point to my original post.


tonyg(Posted 2008) [#7]
But this doesn't change the fact that the example posted in the documentation is fundamentally wrong, which was really more the point to my original post.


What is wrong with it though? The example for Goto works fine it's just that Superstrict doesn't allow it and is undocumented.
Unless I am missing something.


Otus(Posted 2008) [#8]
What is wrong with it though? The example for Goto works fine it's just that Superstrict doesn't allow it and is undocumented.
Unless I am missing something.


Actually, it's perfectly well documented. Here's what the docs say about goto in BASIC compatibility:

The Goto command is supported, but only in non-strict mode.



Tachyon(Posted 2008) [#9]
Apparently you are missing something.

In the Documentation there is an important bit of undocumented information.

This one glaring problem with BlitzMax is why so many people often return to ask fundamental questions.


Brucey(Posted 2008) [#10]
ah, yes, well, we try not to talk about the documentation around here...


Tachyon(Posted 2008) [#11]
@Otus: I did eventually find that bit of documentation. There are two sections called "Basic Compatibility", one under Language and one under Modules/Basic. A simple fix for this would have been to put that info with the goto documentation itself; or better yet, remove the command altogether as it seems to be so reviled and doesn't have any usefulness.

@Brucey: agreed.


GfK(Posted 2008) [#12]
In the Documentation there is an important bit of undocumented information.
How can there be undocumented information in the documentation? It'd surely be documented if it was in there.

Anyway, don't know why everybody's ready to burn people at the stake for using goto. If it can solve a problem and save you hours of work, use it. Who cares?


CoderLaureate(Posted 2008) [#13]
10 CLS
20 PRINT "JIM IS COOL!";
30 GOTO 20

>RUN

JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!
JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!
JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!
JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!
JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!
JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!
JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!
JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!
JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!JIM IS COOL!

*Break in Line 20
Ready
>_

My first program **ever** written back in 1980. Should give you an idea of how obsolete GoTo's are.


Retimer(Posted 2008) [#14]
Then your design is flawed ;-)


I don't use goto for the fear that i'll just cause myself infinite loop glitches, other then that if you're careful I don't see what the problem is with it..in fact I could see it saving conditional statements and saving many lines of code in many cases. ?


Beaker(Posted 2008) [#15]
Using Goto isn't lazy. I'm lazy, cos I don't want my code to break in wierd ways, nor do I want it to be impossible to read/debug 2 years from now.

If you value your sanity, don't use Goto and always use SuperStrict.

[Use it if you want, but don't come running to me if your clients/users break your legs.]


puki(Posted 2008) [#16]
I like Goto.


Xerra(Posted 2008) [#17]
Seriously goto is a travesty of a command these days. I grew up programming on the old 8 bit micros with the flawed basic of the Vic 20 and the C64 and knew no better.

Sure, that was how things were programmed back then but it was an absolute pig trying to get myself out of the habbit of programming in such a p1ss poor way when modern languages evolved the development language so much.

Hell, it was just as hard trying to adapt to working without line numbers for the first year or so when I came back to programming after many years away. I do consider goto to be lazy programming now and any program can be written without having to refer to it.

Case in point is "Jim is Cool!" program further up. I used to do the same thing on the old display computers in Debenhams after school when I was a kid. It's a classic example of a quick and dirty bit of code to get a result.

SuperStrict
Repeat
	Print "Tony is Cool!"
Until KeyHit(Key_Escape) 


That's not much longer to type but does the same thing and even has an exit point built in.

Don't know why I'm so anti-Goto but it's probably because I used it along with Gosub for so long that it became almost as addictive as Lager. How many times I would scan through a listing wondering why I got an "undefined function error" or similar and spent ages trying to track down the bug. Always it was because I'd kept gosubing to a subroutine and forgetting to return from it or because my program flow had strayed into a routine and was trying to return from a routine that hadn't been branched to in the first place.

It's easilly argued that use of goto/gosub could be justified if used properly but I don't think it has a place today for practicalities sake when you can drop in neat functions which can share or hold exclusive variable definitions or even have methods within types that can all be dropped at the end of a program listing out of the way once working correctly so you can forget about them because they won't get called accidentally etc.

And my favourite argument is SuperStrict not liking it. Proves that I'm not the only one who thinks it's obsolete - Mark Sibly obviously doesn't like it either.


grable(Posted 2008) [#18]
Oh ho, not this debacle again.

Gotos are perfectly fine to use, and they are NOT evil!
However, if you ONLY use gotos to control flow in your program YOU are evil ;)

EDIT:
SuperStrict not liking it. Proves that I'm not the only one who thinks it's obsolete - Mark Sibly obviously doesn't like it either.

Not necessarily, SuperStrict is there to catch common user errors, Goto being one of them.


GfK(Posted 2008) [#19]
Oh ho, not this debacle again.

Gotos are perfectly fine to use, and they are NOT evil!
However, if you ONLY use gotos to control flow in your program YOU are evil ;)

*applause*

As a side-note, Idigicon's "Derby Day" was written entirely using Gosub/Return. Not a single function anywhere.


tonyg(Posted 2008) [#20]
@Tachyon
Apparently you are missing something.

Sorry but I was taking you word-for-word :
the example posted in the documentation is fundamentally wrong

I agree it would have been nice and very easy to have it documented in the Goto entry. I've given up on reporting any documentation or feature stuff though.


MGE(Posted 2008) [#21]
Goto is actually used alot in low level coding, I don't understand why any serious developer frowns on it's use. I used to write JMP() tables back in the day that did some cool stuff. ;)


H&K(Posted 2008) [#22]
Idigicon's "Derby Day" was written entirely using Gosub/Return. Not a single function anywhere.

Surly we have to concider gosub return as a function call, just with all viables as global.

That is pre "sub" or "function" isnt gosub/return HOW we used to sub program our programs?

ANyway I dont see whats wrong in letting a programmer have (what is in effect) a jump command. Yes its over use makes code a nightmare, BUT somtimes it offers elegant solutions.


Muttley(Posted 2008) [#23]
I think the last time I actually used Goto in anger was in a text-on;y adventure game I wrote on the Commodore 64 in C= BASIC back in about 1986. I used it in a jump table for displaying the locations IIRC.

I don't think I've actually needed it since...


plash(Posted 2008) [#24]
SuperStrict not liking it. Proves that I'm not the only one who thinks it's obsolete - Mark Sibly obviously doesn't like it either.
You kidding? Sibly doesn't even use good coding practices, he wrote all the modules in Strict mode (the only real tickle I have with him atm).

Now I'm sure someone will bring up the fact that SuperStrict wasn't around when he wrote them all.. but that doesn't mean they shouldn't be rewritten.


Czar Flavius(Posted 2008) [#25]
he coded all the modules in Strict mode (the only real tickle I have with him atm).
So? The only difference between Strict and SuperStrict is cosmetic. I code in Strict. Being as most of my variables are Ints, it gets a pain having to write it all the time.


Grey Alien(Posted 2008) [#26]
Goto doesn't work in Strict mode either.

I don't use goto for the fear that i'll just cause myself infinite loop glitches
Infinite loops can be caused by recursive function calling, and plain old Repeat/Until and While/Wend loops.


CS_TBL(Posted 2008) [#27]
Do other languages have a goto command? If not, it may be practical to learn to code without goto so that moving over to another language is easier.


D4NM4N(Posted 2008) [#28]
This won't compile in SuperStrict mode.

Lol, never mind superstrict, you're lucky a vitual MEGA-strict dominatrix doesnt pop out of cyberspace and chase you round the house in your underpants for even considering using it :D

-Just kiddin- they can be handy occasionally if on a very small and local scale, but i try not to use them at all myself. It doesnt surprise me it doesnt work with stricts as there is no structure to them and are pure legacy.


Retimer(Posted 2008) [#29]
Do other languages have a goto command?


yes


Czar Flavius(Posted 2008) [#30]
Yes, but chances are the people on the C++ forum will be even more disgusted by the mere notion that you even thought about considering thinking about asking about using it.


Tachyon(Posted 2008) [#31]
...the people on the C++ forum will be even more disgusted by the mere notion that you even thought about considering thinking about asking about using it.

Oh I would just die if those popular kids on the C++ forums knew about my choice of programming language or the fact that it supports 'goto'. :D


Muttley(Posted 2008) [#32]
C++ supports goto. ;)

In fact Stroustrup describes it as the "infamous" goto. :)


Retimer(Posted 2008) [#33]
Oh I would just die if those popular kids on the C++ forums knew about my choice of programming language or the fact that it supports 'goto'. :D


I got a place I would like them to 'goto'. =P

It's just another preference debate. Don't avoid it because people who don't prefer it call it stupid. Who cares.

Tachyon, you should just go with strict mode and use goto. Program the way you're most comfortable with.


Tachyon(Posted 2008) [#34]
Tachyon, you should just go with strict mode and use goto. Program the way you're most comfortable with.

Thanks for the advice. The fact is that my original post was simply saying that "goto" doesn't work as advertised...but I had already implemented a 'proper' solution before I even posted it.

For the record, I only use SuperStrict. I think the compiler should default to SuperStrict and require a command like "SuperLax" if you want to program lazily. ;)


plash(Posted 2008) [#35]
For the record, I only use SuperStrict. I think the compiler should default to SuperStrict and require a command like "SuperLax" if you want to program lazily. ;)
Hehe, if only... (it would save us like 12 bytes of code!!)


WERDNA(Posted 2010) [#36]
sorry to revive such an old post, but I had to search the forums for why my goto loop
wasn't working in Strict mode :)

I personally think goto sucks, EXCEPT for turn based rpgs, such as the classic Wizardry
type games. I've found goto to be invaluable for dealing with the combat system :D


ImaginaryHuman(Posted 2010) [#37]
Yah, I remember some post by Mark saying goto will be disabled when you use Strict because Strict is supposed to `help you program better` especially if you're a beginner. I use Strict all the time, just to make things a little easier to debug, so I generally don't use Goto. But it has its uses. I wish there was a Gosub also - you need some way to store a place to `return to` before you jump to the subroutine with Goto, but there isn't really a way to do that in Blitz since you can't modify the program pointer and there's no way to use a variable to jump back to where you left off.


Dreamora(Posted 2010) [#38]
Goto has technically to be disabled because variable declaration and variable scopes are totally impossible with "happy memory jumping".

Also the main place where goto was used was to jump out / continue nested loops and for that mark implemented loop labels and exit <label>, continue <label> :)


ImaginaryHuman(Posted 2010) [#39]
One other feature I occasionally miss is jump tables... ie in assembler you define labels just before pieces of code, those labels turn into relative addresses (relative to a base address), then you load the base address into a register and jump to some offset from that address (using the label as a reference to the offset). This way you programmatically jump to one of any number of pieces of code in exactly the same amount of time without having to do lots of tests and branches.


Dreamora(Posted 2010) [#40]
isn't that what select technically does if the compiler handles it smart? (select blocks don't have case local variable scope just select local one, which would imply an at least similar implementation)


Muttley(Posted 2010) [#41]
WERDNA:
I personally think goto sucks


You should have stopped there. ;)

WERDNA:
EXCEPT for turn based rpgs, such as the classic Wizardry
type games. I've found goto to be invaluable for dealing with the combat system :D


I'm not quite sure why you'd need goto for such a simple combat system. Care to explain your thinking?

I'd bet money on there being a much better way of implementing (better as in easier to read, maintain and debug) it without even the slightest hint of a need for even 1 goto command. ;)


Czar Flavius(Posted 2010) [#42]
isn't that what select technically does if the compiler handles it smart?
I read before that it's just turned into a series of If/Elses.


WERDNA(Posted 2010) [#43]
@Muttley

Function TurnBasedFight()
'set a few initial variables.

#player Turn

'players turn

'at end of players turn
Goto #Enemy Turn

#Enemy Turn

'Enemy's turn.

'At end of Enemy's Turn.
Goto #Player Turn

It works for me :)
Its simple, and I don't have to deal with functions. Although its true I haven't
done a turn based RPG in awhile now, and 'might' think differently when I start
work on one now.


Czar Flavius(Posted 2010) [#44]
That's horrible!
Repeat
	battle_over = player.fight(enemy)
	battle_over = enemy.fight(player)
Until battle_over



_Skully(Posted 2010) [#45]
If you're going to use GOTO in your code... just don't plan to make a career out of programming... GOTO some other field :)

Goto makes spagetti code which is incredibly hard to debug and follow.


WERDNA(Posted 2010) [#46]
yeah, I know, lol.

I still like using it for Turnbased Rpgs :)


Grey Alien(Posted 2010) [#47]
You use GOTO, gasp! I'm never buying another Eschalon again! ;-p


Naughty Alien(Posted 2010) [#48]
...eeehhh...use whateva u want guys, long as it does produce what suits your ideas, and superstrict is kool :)


Grey Alien(Posted 2010) [#49]
I have a fondness for Goto because my first program looked something like CoderLaureate's except it didn't have the CLS line.


Leon Drake(Posted 2010) [#50]
my favorite goto says a lot.


10 Bible = "God exists"
20 Belief = Bible
30 If Proof = false Goto 10



mwhahahahahahahahahaha


_Skully(Posted 2010) [#51]
I have a fondness for Goto because my first program looked something like CoderLaureate's except it didn't have the CLS line.


Anyone that started on a TRS-80 Model I like I did would know the hello program very intimately. A root but like all roots they end up in the museum of language structure ;)


10 Bible = "God exists"
20 Belief = Bible
30 If Proof = false Goto 10


Error... proof is undefined


Leon Drake(Posted 2010) [#52]

Error... proof is undefined



default value for a variable is false.


Czar Flavius(Posted 2010) [#53]
What if proof is a null object?


_Skully(Posted 2010) [#54]
default value for a variable is false.


According to your beliefs ;)


What if proof is a null object?


That would be exceptional


Blueapples(Posted 2010) [#55]
http://en.wikibooks.org/wiki/BlitzMax/Modules/BASIC/BlitzMax_runtime#Goto

I haven't checked in on the progress of the WikiBook but I must say, it is looking awesome (really glad I started it and a few people have really been taking care of it). Even includes a note that Goto is not supported in Strict.


Xerra(Posted 2010) [#56]
10 Bible = "God exists"
20 Belief = Bible
30 If Proof = false Goto 10


Bad coding, imho. No strict in there :-)


beanage(Posted 2010) [#57]
Goto is NOT necessarily bad design. Doesnt mean I use it. I actually hate it. But there are situations, where it is more intuitive to use Goto, for example, in complex multi-result loop termination.

And last but not least, even Linus Torvald used Goto in some places in the Linux kernel, and he's a pro, sure about that :)


CS_TBL(Posted 2010) [#58]
I somehow feel that these complex loop terminations are still easy to do in a functional environment, let alone an object oriented environment. Matter of splitting up code and perhaps alternative code design.

But.. can you come up with an example where goto is ideal to terminate complex-result loops?


_Skully(Posted 2010) [#59]
And last but not least, even Linus Torvald used Goto in some places in the Linux kernel, and he's a pro, sure about that :)


I somehow doubt he used basic to write the linux kernel


beanage(Posted 2010) [#60]
I somehow doubt he used basic to write the linux kernel

Nah, common mistake I guess.. C, C++, Pascal / basically most high level languages support the goto command.

You can look this up on wiki: http://en.wikipedia.org/wiki/Goto ,
and in the Linux kernel docs chapter [a http://lxr.linux.no/#linux+v2.6.26.3/Documentation/CodingStyle#L388]centralized exiting of functions[/a].


_Skully(Posted 2010) [#61]
Considering what the ASM equivelant of a Goto is... its probably the fastest solution unless the compilers are well optimized


beanage(Posted 2010) [#62]
Considering what the ASM equivelant of a Goto is...

I think it all comes down to the fact, that goto really is the bruteforce implementation of a turing machine..


Dreamora(Posted 2010) [#63]
The idea of modern, managed languages isn't to be the fastest but the most productive, stable and leak free.

If you wanted to be fast by any sane, insane and brute force meaning you would use archaic languages or Archaic++, not BM, C#, Java or alike ;)