Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / What's Wrong With "GOTO"?

Author
Message
GLaDOS
15
Years of Service
User Offline
Joined: 16th Jul 2009
Location: Italy
Posted: 22nd Dec 2012 18:12
Hi! Im reading some tutorials for darkbasic and every time i read "goto is bad dont use it" or "goto is the death,it will kill your family".i used goto in few of my learning programs and it worked well.What's Wrong with it?

~GLaDOS
Scorpyo
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: italy
Posted: 22nd Dec 2012 18:57
nothing if you use it right and spare
GLaDOS
15
Years of Service
User Offline
Joined: 16th Jul 2009
Location: Italy
Posted: 22nd Dec 2012 19:05
And what you mean by right and spare?

~GLaDOS
sayerbloke
11
Years of Service
User Offline
Joined: 1st Dec 2012
Location:
Posted: 22nd Dec 2012 20:20
It's just a little untidy [some would say lazy], and doesn't give as precise a control over the flow of your program as other forms of Loops. If you are trying to debug your code [or, worse, someone elses code] and it's got no end of GoTos all over it, it's a nightmare trying to work out what the code is doing and where it's going wrong. The Goto command is there for legacy reasons but, as you're finding, you'll find most people wouldn't really recommend using it regularly. I think for a lot of people, it becomes a matter of pride in writing your code in such a way that you don't need GoTo every time you need to "loop". Look into things like "Repeat ... Until", "While ... Endwhile", "For ... Next" loops, etc .
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 22nd Dec 2012 23:46
Basically, if you're a good programmer you will never need to use goto ever. The problem with the command is that it allows you to jump to anywhere in your program, and if done incorrectly, this generates incomprehensible code.

To those that disagree with this statement, please post an example where you really have to use a goto.

TheComet

29 games
18
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 23rd Dec 2012 01:58
I really have to goto the toilet.

one of these days I'll come up with a better signature
GLaDOS
15
Years of Service
User Offline
Joined: 16th Jul 2009
Location: Italy
Posted: 23rd Dec 2012 08:16
Quote: "I really have to goto the toilet.
"

Ahahaha

~GLaDOS
GLaDOS
15
Years of Service
User Offline
Joined: 16th Jul 2009
Location: Italy
Posted: 23rd Dec 2012 08:16 Edited at: 23rd Dec 2012 12:41
Quote: "I really have to goto the toilet.
"

Sorry,Double autopost

~GLaDOS
Scorpyo
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: italy
Posted: 23rd Dec 2012 12:26 Edited at: 23rd Dec 2012 13:57
Quote: "And what you mean by right and spare?"


Means to just use it when gosub would complicate things

example here where I want to go back in the same chunk of code if a condition is not met:



Anything that comes functional and works properly can be used, bearing in mind that gosubs and functions calls should be considered as first choice.

Be open minded, this "goto is bad" myth spread all over the place by people who barely know what they are talking about starts smelling like a stiff dogma.

cheers
Scorpyo
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 23rd Dec 2012 15:33 Edited at: 23rd Dec 2012 15:37
Normally new programmers assume that there are no principles for jumping from one line to another line of code.

The GOTO command tends to be misused; it's a bit like an extremely powerful hand held powertool that is tempting to use, but can easily cause damage if used incorrectly. GOTO has so much unconstrained power you can crash, disrupt or obfuscate your program quite easily with it.

I rearly use it, maybe once or twice in every 10,000 lines of code; I use it usually due to program design issues as a temporary quick fix to escape a For Loop step without exiting the entire loop, like so.



But that would only be a temporary fix until I can be asked to branch things off correctly.

The other program flow tools are better, keep it flowing like a tree, from root, to trunk, to branch, to leaf and back to the root for the next loop; that way the chances of conflicts and confusion is kept to a minimum.

Think about bugs and crashes, how will you find the root to the problem if there are no clear roots or branches? And DBPRO has no serious debugging tools (after so long).

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 23rd Dec 2012 17:09
Scorpyo, your example would be better served with a WHILE loop.



Login to post a reply

Server time is: 2024-11-23 13:15:27
Your offset time is: 2024-11-23 13:15:27