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 / The goto command

Author
Message
Valdez515
15
Years of Service
User Offline
Joined: 4th Jul 2009
Location: United Kingdom
Posted: 11th Nov 2011 22:40
Firstly - I apologise for posting here in a separate thread so soon but I had not foreseen this issue.

I have been developing a basic program that will function as a basic address book. Unfortunately, I was speaking to my friend, who is more experienced, and he informed me that using the goto command is widely considered as poor programming as pinpointing issues is difficult with it. A few minutes' research on the internet confirmed this, with some going as far as to say it will develop terrible habits and prevent you from getting positions in the industry.

There's no fear of that last part but it nonetheless left me feeling concerned. The alternative, a while loop, seems useless in situations in my program such as this: (note: the code is not finished, and please bear with the abbreviated variables and awful labels)



Should I implement while loops or leave it?
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 12th Nov 2011 05:20
Grog!!! You're needed here stat!

The reason goto's are considered poor programming is because, like your friend said, bugs can be considerably hard to find as gotos jump all over your program. Right now your program is pretty small so 1 or 2 gotos aren't too much trouble to track but imagine writing a large program with gotos jumping around thousands of lines of code. It will not be fun to debug that! This is one of the reasons why people say to not use goto at all, so you don't ever develop a need for it.

Now about your code, remember there's 1001 ways to accomplish the same thing when it comes to programming. It might be worth looking into subroutines and using the 'GOSUB' command. Most people would advise you to ditch the goto command and use loops, subroutines and functions.

zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 12th Nov 2011 06:52 Edited at: 12th Nov 2011 06:57
In the way that you are using them, I see no problem with your GOTO commands. It's clean, it's simple, it's efficient, it's easy to read, easy to follow, easy to modify, and as long as you are taking responsibility... I see no need to change it.

Yes this could be rewritten in a loop, and make use of functions, subroutines, exits or flags and a bunch of other options. But none of them will be as clean & concise as you already have it. It's not the days of line numbered BASIC, and jumps like this should not be discarded automatically. GOTO can be very powerful and clean,, and that's exactly how you have utilized it.

I know some will argue with me,, but I would disagree with any one who says GOTO's are "always"... bad, not useful, problematic, and poor programming practic etc... I say BAH to them. BAAAAHHHHHHH!!!

My advice... Keep it the way it makes sense to you.

Your signature has been erased by a mod please reduce it to 600 x 120.
Valdez515
15
Years of Service
User Offline
Joined: 4th Jul 2009
Location: United Kingdom
Posted: 12th Nov 2011 10:52 Edited at: 12th Nov 2011 16:12
Thank you for the advice. I shall aim to avoid goto where possible.
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 12th Nov 2011 11:12
Quote: "Grog!!! You're needed here stat!"


Here I come to save the day!

Hodgey did a great job explaining the evilness of GOTO and suggested it's better to use GOSUB because when you're done with the routine you use RETURN to go back to the line after the GOSUB. If you did the following code snip with GOTO instead you'd have to use 2 labels to do the same thing.

An example of GOSUB:


Your code snip can be written like this:


In the above snip instead of using INPUT it uses KEYSTATE() to check for particular keys on the keyboard (rather than letting the user hit any keys they want and waiting for them to press enter). Since it is only checking for Y or N keys there's no need for an "Invalid Response". And because it is asking if the user wants to create an account the account creation routine (or a function call or GOSUB command) could be added between the IF/ENDIF check of the Y key. Once the user has hit Y or N the EXIT command will eventually be seen and thus the program flow will continue after the LOOP.

Valdez515
15
Years of Service
User Offline
Joined: 4th Jul 2009
Location: United Kingdom
Posted: 12th Nov 2011 11:49
Sorry my second post does not refer to you Grog - I had submitted be fore you posted and due to the verification process for newcomers it has not yet been approved.

Thank you for the code; I shall implement it.
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 12th Nov 2011 16:30 Edited at: 12th Nov 2011 16:34
I still think there are times when a GOTO is fine to use, epecially when working outside of the OOP paradigm.

Quote: "
"That's not to say that you should blindly follow the guidelines in this book. They aren't rules. Too many programmers have taken the guideline "Don't use goto statements" as a commandment from God that should never be broken. When asked why they're so strongly against gotos, they say that using goto statements results in unmaintainable spaghetti code. Experienced programmers often add that goto statments can upset the compiler's code optimizer. Both points are valid. Yet there are times when the judicious use of a goto can greatly improve the clarity and efficiency of the code. In such cases, clinging to the guideline "Don't use goto statements" would result in worse code, not better." ~ Writing solid code: Microsoft's techniques for developing bug-free C programs
"


Some examples where a GOTO makes the code very clear:
- Using a single goto and single label to exit several levels of scope.
- Using a goto in the middle of complex construction to short cut to the top of a loop.
- Starting a highly optimized do { ... } while loop that is best initiated by jumping into the center first.

I don't contest that these situations can be written without utilizing gotos. But I also agree with another of this source's quote.

Quote: "
The Phony goto Debate

A primary feature of most goto discussions is a shallow approach to the question. The arguer on the "gotos are evil" side usually presents a trivial code fragment that uses gotos and then shows how easy it is to rewrite the fragment without gotos. This proves mainly that it's easy to write trivial code without gotos. ~ "Code Complete" (p. 349)
"



http://www.azillionmonkeys.com/qed/goto.html

I won't bother getting into "Tail Calls"... as I believe most compilers optimize for that situation automatically.

Your signature has been erased by a mod please reduce it to 600 x 120.
Valdez515
15
Years of Service
User Offline
Joined: 4th Jul 2009
Location: United Kingdom
Posted: 12th Nov 2011 16:51 Edited at: 12th Nov 2011 17:33
Surely a linear, one way program that branches/forks justifies use of the goto command? Functions and subroutines would be pointless as you would have no wish to return to the old point. Or would you enclose it in one huge if statement?

EDIT: Sorry for asking yet another question, but can anyone pinpoint why at line 10 it doesn't wait for the user to press a key? Somehow, information must be being fed to it:

zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 12th Nov 2011 17:56 Edited at: 12th Nov 2011 18:24
Quote: "Surely a linear, one way program that branches/forks justifies use of the goto command? Functions and subroutines would be pointless as you would have no wish to return to the old point. Or would you enclose it in one huge if statement"


If I understood you correctly... Yes I would agree that it's another time where a GOTO just...
1. Has more clarity
2. Is easier to maintain
3. Is more efficient

While there are a number of control structures that can handle the above scenario; thus, eliminating the GOTO. They usually result in making the code...
1. More Obfucscated then is necessary
2. Less intuitive to alter/maintain
3. Less efficient

There are times where you can break from a loop,, exit from a function, or return from a gosub. But there are "ALSO" those times when a clean and simple GOTO label: actually looks better, reads better, codes easier, and performs better.

The question I ask myself is... "Why purposely make something trivial????".

Sometimes it really is a matter of K.I.S.S. (Keep It Simple Stupid)

Jumping to a label is not the same as jumping to a line number, nor is it equivalent to an "ON X GOTO <line number>, <line number>, <line number>" that could lead a programmer into a tangled web of altered references during code modification/upkeep.

Why push addresses and or variables onto a stack if you are certain you have no intention on returning in the first place? It would be equivalent to adding IF conditions that are "Always" 100% true. Since when is being inneficient ideal? Especially given that more often than not, by doing so, the result is functionally identical yet reduces the clarity of the code. That just not common sense to me.

Unless you are strictly working under something akin to OOP, it's just not that important to take an efficient, clear, intuitive code fragment... and alter it to a less efficient, less clear, less intuitive code fragement. Simply to avoid the use of a perfectly sensible and suitable command - for the sole purpose of avoiding said command.

GOTOs can get you into the same amount of trouble in a modern BASIC as any other method of program-flow-control. GOTO's do not inherently create spaghetti code any more than GOSUB/RETURN's, Function/ExitFunction/EndFunction, if/then/else's, switch/case's, deeply nested for/next , do/while, & repeat/until's do.

Your signature has been erased by a mod please reduce it to 600 x 120.
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 12th Nov 2011 19:42
Quote: "EDIT: Sorry for asking yet another question, but can anyone pinpoint why at line 10 it doesn't wait for the user to press a key? Somehow, information must be being fed to it:"


It's because KEYSTATE() detects a key the second the user hits it and by the time it gets to WAIT KEY the user is still pressing the key and is detected as a keypress for the WAIT KEY.

The easiest way to solve it is to create a REPEAT/UNTIL loop that does not leave till the user lets go of all keys using the SCANCODE() command.



But in that case I recommend not to ask the user anything. If a new file is needed and one already exists just delete that file automatically then recreate it. The user doesn't need to approve of every step.



Valdez515
15
Years of Service
User Offline
Joined: 4th Jul 2009
Location: United Kingdom
Posted: 12th Nov 2011 20:47
Thank you for the code.

Quote: "But in that case I recommend not to ask the user anything. If a new file is needed and one already exists just delete that file automatically then recreate it. The user doesn't need to approve of every step."


True, but if some clueless user clicks on the program they could have everything erased, and though I'm not planning on distributing it I would like to make it as if I were to.
Quel
15
Years of Service
User Offline
Joined: 13th Mar 2009
Location:
Posted: 12th Nov 2011 21:05
Who uses goto to jump here and there and here and there and back again?...

I use it to exit multiple loops nested in multiple if's. How would you anti-goto people exit from that kind of stuff without goto?

Grog and some here say there aren't any situations where goto is the only way to go, however i bound to bounce into these every hour of work.

-In.Dev.X: A unique heavy story based shoot'em ~35%
-CoreFleet: An underground commander unit based RTS ~15%
-TailsVSEggman: An Sonic themed RTS under development for idea presentation to Sega ~15%
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 12th Nov 2011 23:06
Quote: "I use it to exit multiple loops nested in multiple if's. How would you anti-goto people exit from that kind of stuff without goto?"

I wouldn't, I'd let the loops finish. If I needed to make the loop ineffective (ie so it loops through but doesn't do anything), I'd use a boolean statement to cancel things out. On the front end, using the goto statement seems to be okay for exiting multiple loops but in the background the stack seems to have a few problems. [/href=http://forum.thegamecreators.com/?m=forum_view&t=96505&b=7]This[/href] does a good job of explaining it.

One other thing about gotos is that others can have a hard time following your code. This can be problematic if you work in a team.

@Valderz: You'll have to weigh up the good points against the bad points of using gotos and decide for yourself. Just remember old habits die hard.

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 13th Nov 2011 10:45
@ Valdez515:

Quote: "True, but if some clueless user clicks on the program they could have everything erased, and though I'm not planning on distributing it I would like to make it as if I were to."


It sounds like you should just be checking if the file exists and if it does load the data... only delete the file when you want to save the changes to the data. If the file doesn't exist create the file when there's something to save.

If you're just saving strings in a normal array you can make it easier on yourself by using the LOAD ARRAY() and SAVE ARRAY() commands. They load/save the data without messing with the file commands and there's no need to delete the file before writing new data.


@ Quel:

Quote: "I use it to exit multiple loops nested in multiple if's. How would you anti-goto people exit from that kind of stuff without goto?
"


I'd put it all into a function and use EXITFUNCTION to leave whenever it needs.

Quote: "Grog and some here say there aren't any situations where goto is the only way to go, however i bound to bounce into these every hour of work."


True, I haven't run into any situation where GOTO is the only way to go. I'm sure I've said this before but I don't personally use GOTO or even GOSUB anymore but I'm not 100% Anti-GOTO. There's a double meaning in my signature... the letters that are bright blue spell AGO which means in the past. I believe newbies should learn about GOTO and how it works but the moment they learn about GOSUB and/or functions they should leave GOTO in the past.


@ Hodgey:

Quote: "One other thing about gotos is that others can have a hard time following your code. This can be problematic if you work in a team."


Amen! With GOSUB and functions we know that it'll eventually come back to the line after the GOSUB and function call so it's easy to follow the program flow.

Valdez515
15
Years of Service
User Offline
Joined: 4th Jul 2009
Location: United Kingdom
Posted: 13th Nov 2011 12:46
Thanks for the advice.
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 13th Nov 2011 21:01
Sorry but I must just throw in my two cents here (coz that's all it's worth).

I agree with Sir Grog and the Anti-Goto Knights on the disadvantages of GOTO. But I'd like to take it a step further and say that it may be bad for beginners to get into the habit of using GOTO OR GOSUB at all. The reason comes from personal experience. My first attempt at a program was a mess that got trashed and rewritten (poorly, but I was a kid in my defense).

I would advise you to learn how to do things without GO-anything first. Once you can grasp everything better, then DECIDE whether to use it. Coding (when not working co-op) comes down to personal taste and just because I wouldn't touch GO-anything with a barge-pole (at least not in BASIC), doesn't necessarily mean you won't find it easier and more practical. Whatever works for you, but first do things the more streamlined way as it will make your first app MUCH easier to handle.

Anyway, just my personal experiences and something to think about.

Valdez515
15
Years of Service
User Offline
Joined: 4th Jul 2009
Location: United Kingdom
Posted: 13th Nov 2011 21:14 Edited at: 13th Nov 2011 21:15
I had a similar experience that I should have learnt from. When I was younger we were learning BASIC at school. For a project we had to write a simple program - it could be a quiz or an adventure game. Mine was the only one that wasn't completely linear, and it was full of gotos. When I encountered a fatal bug I couldn't trace it by the deadline (though the abysmal IDE didn't help much either). I'll ensure something like that never happens again.
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 13th Nov 2011 21:39
@ nonZero: What's wrong with GOSUB? From my point of view a subroutine is just a function without the scope change. A GOSUB will also return to its point of call so the linear execution of the program isn't disrupted. I will admit though, I did see someone post (I think maybe TheComet) an example of a shocking use of GOSUBs. I think it involved a recursive GOSUB gone wrong. Grog, I think you were present for that as well.

Quote: "I'll ensure something like that never happens again. "

There's only one way to do that.

Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 14th Nov 2011 04:34
Well all of these are valid points, though how I just do it is, if it doesnt NEED a return, and you're just skipping somewhere to initiate something ect., then use goto's. Now if you're initializing something and something that doesnt need to be done is after the initializing, then use a gosub so you can return before using that code and messing with the whole program. Though you can be completely fine using gosub's or goto's some people are just neat-freaks.

If it works, and you understand it. Its good code.

-------------------------------------------------------------
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 14th Nov 2011 09:18 Edited at: 14th Nov 2011 09:19
@Hodgey:

Quote: "What's wrong with GOSUB? From my point of view a subroutine is just a function without the scope change"


I have nothing against GOSUB if other people want to use it, but I personally don't like using it because, as I said, we each have preferences and GOSUB is not one of mine. I had a baaad experience with it as a young teen that left me scarred for life. I had 6 months therapy to get over it. Just kidding... Or am I? Anyways, to get back to the question, I personally don't like to use GOSUB or GOTO(in BASIC) because I like to be able to pass data to my calls. I also like the ability to return data from my calls. Then there are aesthetics. I like unification and consistency. So, if I use functions for one thing, I use them for all things. There is nothing you can do with GO-x that you can't do with functions in decently constructed code (as implied by Grog). Here are some examples below (I appologise for my verbosity, as always):







Anyways, that's just my preference and I'm weirdo so I dunno how much my opinion counts. I personally find it messy and would personally advise beginners against too much of it (if at all) because I got myself into a mess. But our brains work differently so I don't for one minute deny that there are people who may prefer it. It was just my very humble opinion of what may make a beginner's life easier.

@Darkzombies:
Quote: "If it works, and you understand it. Its good code."


Well, maybe if you're the only one who sees your code and making bad codez doesn't hinder performance... But it's different if some poor other guy has to work with you or debug your code. Certainly, we can all agree, that reading someone else's code can be a nightmare if it is a mess, no matter how well they understand it

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 14th Nov 2011 22:58
Quote: "I have nothing against GOSUB if other people want to use it, but I personally don't like using it because, as I said, we each have preferences and GOSUB is not one of mine."

That's fair enough. I'll let you in on one advantage of it though, you can declare types as well as arrays and variables inside a gosub. It may come in handy oneday, it has for me.

@ DarkZombies: I have to agree with nonZero on this one. Typically if I see someone in need of help and their program has more than a couple of gotos in it the first thing I say is to rewrite it without the gotos. I don't feel like going on a wild chase following goto after goto to find the bug. For me: goto + bug = nightmare. So if you want others to see your code, it's a good idea to keep it structured with a linear flow.

zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 15th Nov 2011 01:22 Edited at: 15th Nov 2011 01:46
I just feel a need to clarify one point.

GOTO's do not necessarily make the code harder to read or to follow.
There are many cases where just opposite can be demonstrated. GOTO's used in situations that I described above, are often easier to conceptualize, read, trace/follow, debug, maintain.

I understand the concept of why it's advised not to use them. It's a recommendation that in "MOST" cases is a good one. But it shouldn't be said as "Fact" that GOTO's inherently make code worse or make it into spaghetti.

I've seen many listings that are a complete mess, and they don't contain a single GOTO.

Programmer's are responsible for how easy their code is to understand to themselves and others. If they chose an obfuscated method... then that's on them. GOTO's can certainly be used poorly and make a program very difficult to follow... but no-more-so than every other form of branch code, layered nesting, breaks/exits.

Who here hasn't seen functions that are doing so much that they are hard to trace or debug?

Same goes for Subroutines.

What happens inside them, or the logic that "sometimes" surrounds them, can easily get out of control. But that doesn't make functions bad. And it doesn't make subroutines and GOSUBs bad either.

GOTO's when used correctly, have a lot of power. They can actually be in code that is elegant - at times more elegant than a "non-GOTO" alternative.

Your signature has been erased by a mod please reduce it to 600 x 120.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 15th Nov 2011 01:33
Abso-positively-lutely.

Pragmatism over dogmatism, every time.

If using GOTO makes the code easier to read and understand, then use it. While for the most part, GOTO can be avoided (and should be), it certainly shouldn't be replaced with function calls and boolean flags simply to avoid using it.

Quote: "you can declare types ... inside a gosub"

You don't have to execute a type for it to be used in your program. In fact, with one exception, you can place your type commands anywhere in your program and never allow the flow of execution to run through them.

The exception is that if you want to pass a UDT into a function as a parameter, the type must be defined before the function, but it still doesn't need to be executed. Your code will compile if you fail to do this, but won't operate correctly.

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 15th Nov 2011 02:14 Edited at: 15th Nov 2011 02:15
Quote: "GOTO's do not necessarily make the code harder to read or to follow. "

I agree 100% but a lot of the time, it makes it very hard. I know there are plenty of developers out there that can use gotos efficiently and in a structured manner but most of the examples I've seen have resulted in a complete mess. I will admit, I've never seen a situation where goto is the more favourable option.

Quote: "Who here hasn't seen functions that are doing so much that they are hard to trace or debug? "

*raises hand* ....

Ok, so in conclusion with gotos (as it is with every other command/keyword/etc), it's not the language, it's the programmer.

Quote: "You don't have to execute a type for it to be used in your program. In fact, with one exception, you can place your type commands anywhere in your program and never allow the flow of execution to run through them."

Intersting, I didn't know that one.

nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 15th Nov 2011 06:51
@Hodgey: Cool, I didn't know you could declare UDT data in GOSUB, never occurred to me to try. While I prolly won't use it, it'll be of great help if I'm debugging something for someone else.

@IanM: Thanks for that info on not needing the program to flow through type declarations (I should've worked it out myself the moment I heard two-pass but I'm a little slow that way).

@Zenassem: I agrree with you that GO-x can be used elegantly...in the right hands. Which I advise people with little experience not to use it.

Btw...

***Thread has been hijacked.***

lol, sorry OP. It happens in all forums. We mean no harm and offer blessings of peace and harmony XD

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 15th Nov 2011 09:50
Woah, that'll teach me to disappear for 2 days.

True, GOTO in the wrong hands will eventually be a very bad thing that may cause them to not want to program anymore from sheer frustration. I consider GOTO to be in the list of commands that are there just for older code snips that all of us would write entirely different and with more efficiency now. GOTO is still there for us in case we need it but 99.9% of the time we don't need GOTO at all.

Really I agree with everybody that any command (not just GOTO) can be misused and make something that should be elegant into a horrible mess of code. Of course that's why we love to help people here.

Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 15th Nov 2011 16:35 Edited at: 15th Nov 2011 16:42
This thread is going to get filled with posts I can see.

I have stopped using GOTO, but would be a liar to say that I never used it. I previously used it as an alternative for a 'CONTINUE FOR' call:


Eventually I started to use the Else keyword and block statements instead of Goto. These days I have been using functions after a few levels into a block statement; there are rearly any branch statements 3 levels down in my work, out of personal preference.

I still use GoSub, but only to declare globals and load assets without taking up space in the main source file. I prefer working with prefixed local variables so that I have piece of mind that these are unlikely to conflict with one of the thousands of other variables.


Stereotype;

That's what springs to my mind here

I feel usually people adapt the phrase 'it is not the tool, it is the programmer' to talk about it being the programmers responsibility to deliver the product not the tool; in this context perhaps it is not how bad the Goto command is but how well it is used.

If a hammer and chisle is not used right, there will be blood (I mean damage).

So here is my stereotype; if I use GOTO then "I am not fit for the job, NEXT!!"

Fortunately for me my goals do not involve getting programming job interviews in the first place, so I can keep my embarrasing habits secret. .

Another thing is the customer doesn't care what goes in, they care about what they get out; if it works, and if it makes me enjoy myself I don't give a monkey how you did it. Yes, it is no excuse for untidy code, but the code in the first post isn't that bad minus a few indentation issues.

Again:
Print "Stop chatting Chris..."
Goto Again

Valdez515
15
Years of Service
User Offline
Joined: 4th Jul 2009
Location: United Kingdom
Posted: 15th Nov 2011 17:16
Quote: "***Thread has been hijacked.***

lol, sorry OP. It happens in all forums. We mean no harm and offer blessings of peace and harmony XD"




No problem; observing the discussion is quite interesting.

[Insert witty remark here]
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 15th Nov 2011 17:47 Edited at: 15th Nov 2011 18:06
@Ianm, Hodgey, nonZero, Grog Grueslayer... I agree!

@Chris Tate,, I agree with 99% of your post... except for the stereotype. I guess Linus Torvalds isn't right for the job, nor Donald Knuth. (See second quote below) =P . j/k I understand your reasoning.

-----

A related interesting bit...

Quote: "
Structured programming, canonical structures: Per the Church-Turing thesis any algorithm can be computed by a model known to be Turing complete, and per Minsky's demonstrations Turing completeness requires only four instruction types—conditional GOTO, unconditional GOTO, assignment, HALT.

Kemeny and Kurtz observe that while "undisciplined" use of unconditional GOTOs and conditional IF-THEN GOTOs can result in "spaghetti code" a programmer can write structured programs using these instructions;

on the other hand "it is also possible, and not too hard, to write badly structured programs in a structured language".[38]

Tausworthe augments the three Böhm-Jacopini canonical structures39] SEQUENCE, IF-THEN-ELSE, and WHILE-DO, with two more: DO-WHILE and CASE.[40] An additional benefit of a structured program will be one that lends itself to proofs of correctness using mathematical induction.[41]
"


http://en.wikipedia.org/wiki/Algorithm

[quote]
... Donald Knuth's "Structured Programming with go to Statements"[5] analyzes many common programming tasks and finds that in some of them GOTO is the optimal language construct to use.

Some programmers, such as Linux Kernel designer and coder Linus Torvalds or software engineer and book author Steve McConnell also object to Dijkstra's point of view, stating that GOTOs can be a useful language feature, improving program speed, size and code clearness, but only when used in a sensible way by a comparably sensible programmer.[6][7][quote]

Your signature has been erased by a mod please reduce it to 600 x 120.
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 15th Nov 2011 18:23
I did get a chuckle out of this...


=P

Your signature has been erased by a mod please reduce it to 600 x 120.
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 15th Nov 2011 19:09
Quite right Zenassem.

After reading the point about GOTO "improving program speed", I got curious and performed a loop test.

In DBPRO on my machine it appears that after running this code a large number of times; the GOTO loop (which I never use) is usually 5 to 10% faster than the Do, Repeat and While loops, even though they have similar condition checks.


As for the For loop, even with using a variable range, it pretty much runs 10 times faster than the rest.

No what suprises me is how fast the 'For If' loop is, I am not familiar with DBPRO infrastructure; but this loop runs 4 - 5 times faster than the other loops which perform a similar condition check

Nothing major as you all know this stuff, it is just some evidence that in DBPRO you shouldn't use the other loops (structural or not) for looping through a fixed enumeration. (No pun intended)

nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 15th Nov 2011 21:35 Edited at: 15th Nov 2011 21:37
@Chris: WOW!!! FOR the win (Extremely bad coding joke). Nice work. And I think I know why too...

This is speculation because I don't know exactly what's under DBP's hood, but here goes anyway: I worked a little with ASM on MASM32. Anyhow, I got to learning a little bit about cpu instructions (I retired as a n00b because it was just too time consuming and my life's a mess atm). So, goto (as implied in Zenassem's post) is a lower-level CPU command. DO: LOOP is technically the highest-level version of GOTO.
This:

likely translates into something this:

Well that's my aproximate translation from BASIC to CPU to BASIC. (Reminds me of a game a friend of mine used to play with me by doing a google-translate of eng-[language]-eng).

So, no wonder GOTO is quicker. As for FOR...NEXT that's got me a little unsure since it's technically a conditional GOTO as well. Although it's counter-based so really then it would theoretically take advantage of your cpu's clock speed as it is simply counting loops, not performing any additional conditional checks. In other words, on a dedicated 3.1 GHz single core, it would run better than a dedicated 2.4 GHz dual/quad core (In any language, even those that support multi-core. This leads me to wonder if something that supported multi-core threading couldn't bridge the gap between DO:LOOP and FOR...NEXT). Just crazy speculation, need to read up on this better.

Also all these tests except pure FOR...NEXT contained IF and math which definitely accounts for the discrepancy between FOR-IF...NEXT and FOR...NEXT since FOR-IF's IF is performing a condition check, ie there's one extra instruction per loop.

Okay, that was a major geek-off! I hope I'm onto something and not just hallucinating due to sleep deprivation. If I am, I shall commit seppuku to regain my family's honour... Oh wait I don't have a clean blade - I would never want to risk infection when committing suicide >_<

Shall definitely read up on this one and post what I find out if someone doesn't beat me to it.

I am sooo glad this thread happened!

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 15th Nov 2011 21:48
I think IanM does know what's under DBP's hood so he might be able to offer an explanation.

Quote: "Okay, that was a major geek-off! I hope I'm onto something and not just hallucinating due to sleep deprivation."

To be honest at one point I was thinking about the inner workings of the Do Loop and did think it came down to a goto command. So I don't think you are hallucinating.

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 15th Nov 2011 22:38 Edited at: 15th Nov 2011 22:39


translates directly to:


The reason that the FOR loop is faster is because it disables many checks that DBPro does (keypresses, mousemoves, window changes etc). The GOTO loop doesn't skip these checks.

Here's the assembly for a DO loop (note the CALL EBX):


Although the assembly for the FOR loop is longer and more complex (and isn't the best TBH), it doesn't have that call to the ProcessMessages routine:


When you compile code, you can find the assembly for the last compilation in the TEMP directory within your installation directory - the new editor write it to a file named '_Temp.dbm', and you just need a text editor to look at it.

BTW, if you compile and run the following code, you can see the effect of the lack of message processing on DBPro (suggestion: don't run in full screen, and have task manager started so that you can kill the program):


Try and move your window or escape from the program now!

The equivalent DO loop is easy to get out of using the escape key.

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 15th Nov 2011 23:23
Quote: "Although the assembly for the FOR loop is longer and more complex (and isn't the best TBH)"

How does it compare to C++'s for loop?

Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 15th Nov 2011 23:57
Nice one Ian and nonZero, I learned something from that.

nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 16th Nov 2011 17:06
@IanM: Tks a million for the breakdown. Very useful to know. It seems that this thread will dramatically alter my approach to DBPro.

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 16th Nov 2011 20:06
Quote: "How does it compare to C++'s for loop?"

It doesn't - not only are they not in the same ballpark, they're not even the same sport.

Forget about comparing to C++ as they're not comparible - instead, consider the following loop:


Step through it and work out how many times you need to look at:
- The start point
- The end point
- The step size

That's not a trick question - I count:
- The start point - once, right at the beginning
- The end point - twice, once as an initial check to see if the loop is already out of range, and once after looping through
- The step size - twice, once during the initial check to see if the loop counts up or down, and the second time at the end of the loop to adjust the index (and if it has changed, to see if the loop counts up or down).

The number of times actually taken for each point is:
- The Start point - once.
- The end point - four.
- The step size - Seven!

Use the following code to try it yourself and see it visually:


Now that all said, if you don't use functions in your FOR loop ranges/step then this won't have enough of an impact for you to even notice.

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 17th Nov 2011 01:36
Quote: "It doesn't - not only are they not in the same ballpark, they're not even the same sport."

I know there's a little bit of a syntax difference but from what you've said it sounds like their inner workings are completely different so they probably are.

That second code snippet is very eye-opening. However, I'm curious as to why LoopStep is last on the list when run. Shouldn't it be LoopFinish because once 'i' is equal to Loopfinish because there isn't any point checking the LoopStep...is there?

Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 18th Nov 2011 23:09 Edited at: 18th Nov 2011 23:10
Good grief.

There may be some very few occasions where GOTO genuinely makes the code easier to read. For those among you who still use GOTO, it's highly likely that you are still new to programming and your projects rarely reach several pages of code. In short-scoped programs like this, GOTO is just dandy, as it's quick and easy to trace logic through a program of this size. If you're only jumping in and out of short constructs, I don't truly have a problem with GOTO.

When you become an experienced programmer, however, and your programs become tens of thousand of lines long you'll realise that the only time GOTO is even a little bit acceptable is when the label it links to is on the same page of code. If it's any further away than that, you're spaghetti'ing (which essentially just means it's harder to jump from point to point to follow what's heppening). If you only do this once or twice, it's kind of a little bit sort of forgivable, but it's definitely, definitely not a habit to get into. I maintain that it's lazy and unprofessional as it departs from accepted standards (whether 'correct' or not, avoiding GOTO is just, plain, simply gospel in professional circles).

I'm not a fan of GOSUB either, though that might be a bit more dogmatic than pragmatic. It's harder for me to justify my distaste for GOSUB, but I don't like labels. I guess that's just a preference thing.

Quote: "If it works, and you understand it. Its good code."

Who said that? Was it DarkZombies? <Insert very, very long string of expletives here>

If it works, it just means that there's correct logic. That doesn't mean it's efficient logic, easy to read logic, maintainable logic, or anywhere near the most direct logic. If you can understand it, that doesn't mean anybody else can. That's not only important if you want a job in the industry, but also if you want anybody else to help you fix something that's broken, which happens all the time on these forums even for us experienced codemonkeys. I enjoy very much helping out new guys with their code, but seeing GOSUBs sets my teeth on edge - and if I see a single GOTO anywhere in your code I will close your post without further thought.

At the very least, even if you don't agree, do it anyway: when you ask an experienced coder for help, its their opinions that will determine whether or not they reply

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 19th Nov 2011 09:42
Quote: "and if I see a single GOTO anywhere in your code I will close your post without further thought."


I consider it a challenge and my sworn duty to deGOTO code.

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 19th Nov 2011 12:00
Quote: "Quote: "and if I see a single GOTO anywhere in your code I will close your post without further thought."
"

I get the same feeling sometimes. If I see someone wanting help and it has more than a couple of gotos then I tell them to have a go at re-writing their code without the gotos.

@ Agent: it's nice to see a local on the forums. Although my location doesn't say it I'm from Sydney as well.

Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 19th Nov 2011 21:31
What scares me when I look at 'help me' posts is not the gosub commands, not even the goto commands, because such requests usually only feature a few 100 lines of code anyway; a quick search and find usually pinpoints the labelling problems. Any person writing 5000 lines and above will eventually find out how much confusion label hopping and certain other habits generate, after a few weeks away from even your own source code.

What scares the crap out of me is meaningless almost 'nameless' variable names and function names such as x_2 = GTM( Player1, Camera1 ), improper indentation and swarms of calculations without comments;

I'd rather help the person who bothered to comment the code and write it out neatly than to help someone who fills the file with lowercase soup.

lilyeryhe
13
Years of Service
User Offline
Joined: 21st Nov 2011
Location:
Posted: 22nd Nov 2011 03:47
Nice one Ian and nonZero, I learned something from that.

lilyeryhe
13
Years of Service
User Offline
Joined: 21st Nov 2011
Location:
Posted: 22nd Nov 2011 03:48
Thank you for your sharing.

Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 6th Dec 2011 16:11
Quote: "swarms of calculations without comments;"

I always comment complex calculations, and even complicated IF statements. I deliberately break down some AND-related IF's across several lines for ease of commenting. Here's an example of complex condition checking in Solodor's source:



I could have written that set of IF's like this:



Much harder to read, even to the person who wrote it.

I don't do this so that other people can read my code - I do this so that *I* can read my code. Some stuff in my programs are so complicated that by the time I come back to a given function I've forgotten how the logic operates. It's absolutely essential that code is readable and well commented, even for yourself.

TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 9th Dec 2011 01:32
Here's a quote from flashing snall:

Quote: "I have a story. Once long ago I was introduced to the magical wonders of Dbpro. Grog was there to guide my foolish questions, I remember it to this day. And as my wheels got turning, I learned of the villainous GOTO. I tapped into its corruption, thinking that the power to jump for line to line would strengthen my game. And for a week or so, it did. Progress came like strawberries in summer and I the programmer spirits smiled upon me. But alas, it was all for naught, for soon there after the GOTOs rioted in the source. Their tricky logics flummoxed my very being, and sent the project hurtling towards an unknown nether world of spaghetti madness. I never saw the source code again.

Since that dreadful fate, I have forbidden myself to be tempted by the lustful GOTO command. It is evil."


TheComet

Bulsatar
13
Years of Service
User Offline
Joined: 19th Apr 2011
Location:
Posted: 9th Dec 2011 05:17
Just to throw in a newbie's .02 (to dbpro, not programming), I find that using GOTO is really only efficient to bounce around inside of a function. That way you can check for conditions and exit loops or bypass the code altogether if need be and you never need to figure out what you are going to pass at a given time, just pass it whatever it wants each time no matter the value.
Other than that (and even sometimes with functions or gosubs depending on how you set up your code), if you are bouncing in and around your entire code with gotos then it can probably be set up a different way.
My general rule, if I bounce more than 3 times on a given routine, I REALLY need to change what the hell is going on!!!

example:
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 9th Dec 2011 14:58 Edited at: 9th Dec 2011 15:06
No goto necessary:


All GOTOs removed without decrease in efficiency. Most of the time, you can do without GOTO, as you can see.

It's good for jumping to cleanup code within a function where you need to release temporary resources, or for exiting from deeply nested loops.

[edit] Just to make a point, in those cases all jumps are forward jumps, and are for small distances in the code. If you can't see the GOTO and the label on-screen together, you're doing it wrong.

nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 9th Dec 2011 15:48
Quote: " If you can't see the GOTO and the label on-screen together, you're doing it wrong. "


...I see a new meme being born

*Insert snapshot of code filled with GOTOs and no visible labels*
*Add large, bold text stating "You're Doing It Wrong"*

Login to post a reply

Server time is: 2024-11-22 12:41:43
Your offset time is: 2024-11-22 12:41:43