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.

DarkBASIC Professional Discussion / Are GOTO and GOSUB bad constructs?

Author
Message
Da_Rhyno
13
Years of Service
User Offline
Joined: 25th May 2011
Location:
Posted: 28th Sep 2011 07:03
I originally used those for when I coded, and it seemed a lot simpler using them. However I've heard that they're akin to bad coding style.

What do you all think?
CumQuaT
AGK Master
15
Years of Service
User Offline
Joined: 28th Apr 2010
Location: Tasmania, Australia
Posted: 28th Sep 2011 08:09
The GOTO war has raged for years. Most people say "don't use it, it's bad coding". I say use whatever you like, just use it WELL. There are far too many bad coders out there who use coding standards as a raft to cling to. The truth is, if you're a good enough coder, you should be able to use any coding style you want. So long as the program is stable, efficient and works perfectly, then you can use whatever you like.

As for GOSUB, I've never heard of that being a no no to use. A lot of new programmers make the mistake of jumping out of a GOSUB using a GOTO, which you definitely should not do, but otherwise, it's a very handy and essential part of programming!

Malevolence: The Sword of Ahkranox
www.msoa-game.com
www.facebook.com/malevolencegame
Da_Rhyno
13
Years of Service
User Offline
Joined: 25th May 2011
Location:
Posted: 28th Sep 2011 08:48
Thanks for the thoughts! I kind of have the same feelings, though in some cases it seems that functions are more easily manageable.
miso
14
Years of Service
User Offline
Joined: 16th Jun 2010
Location: Budapest, Hungary, 127.0.0.1
Posted: 28th Sep 2011 09:01
I don't want to go into the goto war too deeply. Using goto's is perfectly fine, until you know what do you do.

In most cases, programmers don't work alone, and sometimes a third person has to change something. In this case, if the program uses a lot of gotos, it becomes hard to track.

You can always simulate a subroutine using a function with a short, descriptive name.

No place like 127.0.0.1 .
CumQuaT
AGK Master
15
Years of Service
User Offline
Joined: 28th Apr 2010
Location: Tasmania, Australia
Posted: 28th Sep 2011 10:49
Miso has it totally correct. The whole reason coding standards exist is because more often than not, other people will have to go through your code, and, having not written it themselves, they need to be able to track through it quite easily.

Subroutines are just functions that don't return anything, so interchanging a subroutine with a no-parameter function is pretty much the same deal. People generally prefer functions because they are often option explicit and thus very easily transferable between projects

Malevolence: The Sword of Ahkranox
www.msoa-game.com
www.facebook.com/malevolencegame
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 28th Sep 2011 11:29
I tend to always use an initiate gosub, for setting variables and arrays, doing that with a function can be clunky. For instance, you have to globally assign your variables if you declare them inside a function.

I wrote a game completely in functions once, and there is really nothing to be gained in avoiding subroutines, least of all avoiding them because you think they're bad practice, or encourage bad program layout. Personally I like to use subroutines, they can encompass a whole slew of commands and I think it helps us seperate game logic from code functions... I always get lost in a sea of function names, a half-dozen or so having game sub-routines suits me fine.
We are coding in BASIC at the end of the day, if you find yourself needing more complex program layout, then maybe it's time to look at GDK.

Health, Ammo, and bacon and eggs!
Diggsey
19
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 28th Sep 2011 11:54 Edited at: 28th Sep 2011 11:54
Generally, gosub is fine and goto is not, but there's no point having an argument over it. If you program long enough you'll work it out for yourself, and most importantly you'll understand why...

[b]
BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 28th Sep 2011 13:16
I agree with all of the above, especially about other people having to read your code.

In favour of functions, I look at it like this in terms of debugging:

1. When you call a function it will come back to where it left, other alternatives may choose not to
2. If it doesn't come back, you can focus your efforts on the function.
3. You can check variables before and after the function call. If a variable is wrong...blame the function and focus your efforts.

For me, functions offer focus.

Pincho Paxton
22
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 28th Sep 2011 17:56
I prefer to stick with the coding that I have used from the start.. Gosub Return. Not keen on functions, hate types.

tiresius
22
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 28th Sep 2011 18:16
Just goggle "goto" here and you will find a lot of posts about it already.

My feelings are goto should not be used except in special circumstances. Once you know those special circumstances, enjoy. I personally have yet to find a need for it.

Gosubs are desirable for readability and as your project grows it will be mandatory to keep things together.

Functions are desirable for portability/modularity, even if it is a programmer-maintained modularity. It is also the only way to use local scope concepts. Since gosubs access the program scope you could inadvertently initialize a variable needed elsewhere. I write my projects using all functions, try to explicitly define every variable (local and arguments), and use several source files. That is how I organize large projects and it works out pretty well for the most part.


A 3D marble platformer using Newton physics.
JackDawson
13
Years of Service
User Offline
Joined: 12th Jul 2011
Location:
Posted: 29th Sep 2011 06:27 Edited at: 29th Sep 2011 06:27
I agree. If your going to show your code to others, then GOTO / GOSUB is bad form. I have been coding in many languages for over 30 years, as have others. One problem MOST coders have is what is known as spaghetti programming where a LOT of GOTO / GOSUBs are used. BUT.. if your not going to show your code to anyone AND you know your code extremely well, then use what your comfortable with. Just as long as you can come back a year later and go, "oh I can still read and understand this".

The problem with gosubs is most people tend to over use them. Nothing wrong with using it once or twice to setup globals. Maybe even some TYPES. But to use them AS your functions makes things difficult to keep up with when your in NEED of those GOSUBS all the time. Then it has way too many branches and bingo, your lost in your own code. FUNCTIONS on the other hand can be called just by calling their name. SO its much cleaner to code in. AND it makes it easier for others to read it.

I go crazy when I read code from those who want to submit their code to help the community and they use nothing but GOSUBs.. OMG I am like.. WTF is this happy horse shii.... you have to be a nuclear scientist to understand it. :/

Again though, if your not caring about what others see as far as your code goes, then do what ever you want. There is no wrong way if its your own code, its just not a "standard" for OTHER people to read.

"Life is like a box of chocolates.. eat it before it melts."
Attila
FPSC Reloaded TGC Backer
20
Years of Service
User Offline
Joined: 17th Aug 2004
Location:
Posted: 30th Sep 2011 16:15
GOSUB is better than using Function you do not need to declare globals because all variables created are accessible in the main-program and ther gotos. In native Basic functions where mainly used for mathematical functions and/or string-operations, because some basic-dialects did not provide the functionality offered by other variants (a.e. the mid$() in DarkBasic does not the same thing as mid$() in MS/GW-Basic). Therefore you needed to create e a function to have the same functionality when porting a program to another machine using another basic variant.

GOTO is bad when used between GOSUB-Routines because return (as does endfunction too) removes the return-address from the stack and if you jump out of a routine the return address will remain on the stack and maybe it will be used unexpected (especially if you jump with GOTO into a routine terminated with a return.

GOTO's to the end of the program are acceptable because after the end your code will not care about the stack...

Functions as used today are often used as in C or PHP (languages where the compiler is so stupid, you have to terminate a command with ";" or he will not find it). A function males sense when the variables used in the function should not be accessible to the program and only the result of your computing interests you.
As creating a substring$-function replacing mid$() [returning only one char in DB] by a subtring$() where a portion of a string is returned.

Both types of splitting up your code make sense depending on what you want to do. If you need variables created or altered in the routine in other parts of the programm GOSUB is better, when you just are interested in the result a function can be helpful (and even used in another program).
zenassem
22
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 1st Oct 2011 03:04 Edited at: 1st Oct 2011 03:07
I will sometimes use GOTO's while I am working out a problem. After I get things fleshed out, I will rework the code to remove the need for GOTOs. For me, it's not because it's right or wrong per se; but I know that it will be harder for me to remember and trace the flow of the program, if I have to return to it at a later date.

I actually miss the ON combined with GOTO command at times. It seemed easier to read than "switch-case" or "if-then-else" statements for testing purposes.

For eg.

ON <value> GOTO label1:,label2:,label3:

or with line numbers...

ON <value> GOTO 1000,1200,1400,1600,1800

Your signature has been erased by a mod please reduce it to 600 x 120.
WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 1st Oct 2011 04:47
Quote: "For eg.

ON <value> GOTO label1:,label2:,label3:

or with line numbers...

ON <value> GOTO 1000,1200,1400,1600,1800"


Wow! I remember those days too... I couldn't imagine going back to that now I've been using "Switch Case"...

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 1st Oct 2011 13:45
Quote: "BUT.. if your not going to show your code to anyone AND you know your code extremely well, then use what your comfortable with."

Counter example: The Linux kernel. just picking out the very first C source file I found listed, it has 5 gotos. Some sources have fewer or none, some have far more.

I would suggest that this very public code is well written and very understandable due to having those gotos as they make the error-paths very easy to see. Even the loops that jump back into code to retry something (which is something I personally avoid) are readable and understandable.

In my opinion (and it is only an opinion, not dogma), GOTO should almost never be the first choice, but if it's the BEST choice (due to clarity or performance), then I go that route without any guilt.

Quote: "GOSUB is better than using Function you do not need to declare globals because all variables created are accessible in the main-program and ther gotos"

Did I understand that correctly?

You do realise that functions and procedures were invented precisely because of the problems with having every variable being global, don't you? Ever tried to rewrite a recursive parameterised function as a subroutine?

@zenassem,
I can't provide an ON GOTO or ON GOSUB, but with function pointers and arrays, I can provide an ON CALL FUNCTION equivalent.



@WLGfx,
The point of the ON GOSUB, is that it allows you to take the different sections of code out of line with the main logic, making things clearer to read. I would argue that this would be clearer to read than embedding the code into a switch statement, especially if there is a substantial amount of code.



The point of the ON GOTO ... well, with more modern languages, I can't really see the point of it either, but I'm open to being persuaded.

zenassem
22
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 1st Oct 2011 14:20 Edited at: 1st Oct 2011 14:23
Quote: "I can't provide an ON GOTO or ON GOSUB, but with function pointers and arrays, I can provide an ON CALL FUNCTION equivalent."




Absolutely brilliant/elegant!! Would never have thought of being able to do that... and so straight foward now that I am looking at it! This one gets added to my DBpro recipe book. Thanks!

Your signature has been erased by a mod please reduce it to 600 x 120.
Attila
FPSC Reloaded TGC Backer
20
Years of Service
User Offline
Joined: 17th Aug 2004
Location:
Posted: 1st Oct 2011 15:15 Edited at: 1st Oct 2011 15:39
Quote: "
You do realise that functions and procedures were invented precisely because of the problems with having every variable being global, don't you? "


They where invented in PASCAL to teach structured programming, but while the professor (N. Wirth I remember) used it to teach, some people made it a common standard. Later on it was integrated in C (which is a descendant of Pascal)

Depending on your programing style, having all variables global can produce a mess or make your life easier.
I think that having everything usable everywhere makes my life easier, but it implicates that variable names are unique.
To make a program easier to maintain it helps me much more to add comments to remember the algorithms I used used when programing the code.

But do not forget BASICs best thing, you do not have to declare everything which allows for fast and easy prototyping, easy debugging and fast result to show your boss or to your clients. There is a reason that VisualBasic 6 is still around and XNA has its problems to become a standard. (well there are still some COBOL and FORTRAN programers around too)

A typical BASIC function would be sqrt(), which will deliver one result and the variables used to compute the square root will not be needed in the program.

But using a function to alter a objects position and to store the new position in variables is only possible if you declare the variables (as array in DBClassic or as array or globals in DBpro).

If you think declaring variables is a great thing you are better of with COBOL, Pascal, C or ASM. (or using a BASIC that can be set to "option type explicite")

Quote: "Ever tried to rewrite a recursive parameterised function as a subroutine?"

Sure - my first was reading (and printing out) a directory-tree of 'unknown' and 'unlimited' depth, and I never used GOTO in it, but while's and gosub's. BASIC did not have always the FUNCTIONs.

A place to use a goto could a routine reading a file not finding the required input record. Then the processing has to be aborted and this could be done by setting an abort-switch or by going to the the EndAndAbort-routine with goto. Depending on what you want to do. While I prefer the GOSUB with ErrorSwitch-variant as shown here:



Note: the variable ErrorMessage$ will be filled in the subroutine ReadData: but used in the main program. Would ReadData() be a function this would not be possible.
WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 1st Oct 2011 16:31
ON CALL FUNCTION would be a fine addition to the language. Most switch case's end up calling functions anyway to tidy up the code. I wouldn't even ask for a vote for an ON GOTO...

Quote: "But do not forget BASICs best thing, you do not have to declare everything"

That's one of the things I dislike about BASIC, I like to know that the compiler has come across a typo in my code. I'd definitely prefer to declare my variables. But it is one thing I'll have to put up with though.

Whether I program in C++ or BASIC I never use GOTO or even GOSUB, as has been pointed out, FUNCTIONS have variables local to it and if required they can still access global variables. The only ever time I would use something similar to a GOTO or GOSUB is when I'm working with assembler code because you have no choice.

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Da_Rhyno
13
Years of Service
User Offline
Joined: 25th May 2011
Location:
Posted: 1st Oct 2011 18:04
IanM - I never in my life would have guessed that there were such things as Function Pointers in DBP since pointers are so limited in DBP. Thanks for that bit of code, I can use that in a event handler! =D
Da_Rhyno
13
Years of Service
User Offline
Joined: 25th May 2011
Location:
Posted: 1st Oct 2011 18:08
Quote: "
Whether I program in C++ or BASIC I never use GOTO or even GOSUB, as has been pointed out, FUNCTIONS have variables local to it and if required they can still access global variables. The only ever time I would use something similar to a GOTO or GOSUB is when I'm working with assembler code because you have no choice.
"


That's probably the one reason I like GOTOS or GOSUBS at all... I can relate them to a JMP or CALL command as in assembly.
JackDawson
13
Years of Service
User Offline
Joined: 12th Jul 2011
Location:
Posted: 1st Oct 2011 20:02
Quote: "That's probably the one reason I like GOTOS or GOSUBS at all... I can relate them to a JMP or CALL command as in assembly."


Yea, I tend to agree. Assembly is great, but you can get caught up in the GOTO madness. Its why I make and declare my own functions in Assembly first then I can just call them, depending on how you manage memory.

"Life is like a box of chocolates.. eat it before it melts."
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 1st Oct 2011 21:51
Quote: "They where invented in PASCAL to teach structured programming"


Procedures came around in the late '50s for ALGOL60 and FORTRAN II, as an outgrowth of lexical scoping rules and block-structured constructs (if...endif, while...endwhile etc) and were later included in other languages too (APL, BCPL etc). They were seen as a very successful experiment, and arguably the most successful extension to computer science that there has ever been.

Pascal came along in 1970, and had nothing to do with functions/procedures and local/global variables, and everything to do with the teaching of structured programming. Note that when Wirth designed Pascal, he took out GOTO (his own personal bias), but kept global variables in.

Every mainstream programming language in use today provides parameterised functions/procedures with local variables. The only possible exception to that rule might be ASM, but even so ASM programmers will almost always use some sort of convention to provide locally scoped parameters and variables.

Quote: "Sure ..."

Missed the point, which was having a function that accepts parameters calling itself recursively with a different set of parameters but with the original parameters kept intact. Nothing to do with GOTO, and everything to do with your statement implying that local variables are somehow a step backwards from globals ('GOSUB is better than using Function you do not need to declare globals because all variables created are accessible in the main-program and ther gotos').

To implement a recursive function without local variables and/or scoped parameters requires you to maintain a stack of those variables, one stack needed per subroutine. Using globals only also means that reusing a library of common routines becomes unsafe ('Did I already use that global variable name elsewhere in my code?').

Basically, there are very good reasons that the rest of the world has moved on to using parameterised functions and local variables.

Quote: "I never in my life would have guessed that there were such things as Function Pointers in DBP since pointers are so limited in DBP"

It's a Matrix1Utils extension, and you're very welcome

EdzUp
22
Years of Service
User Offline
Joined: 8th Sep 2002
Location: UK
Posted: 2nd Oct 2011 22:45
Another thing comes to mind that quickly flicking through the topic is this:
1) If you intend at a later date to move to another language, do remember that in some coding circles GOSUB and GOTO just dont exist or are seriously frowned upon for using C/C++ is one.

2) As has mentioned it works either way but error tracking is a pain with goto/gosub

3) Source debugging and function folding in the IDE help a great deal with functions so looking at a big source file is easier with folded functions.

4) Passing parameters to function is just as easy if not easier with functions than having global variables

5) Local variables inside functions are IIRC removed when the function ends, making your program more lean and not so memory hungry.

All of these things make for good programming practice and if you wanted to move to another operating system do remember that goto/gosub is not in every language out there and can mean a serious lot of recoding to get your game to work with it.

-EdzUp
Graveyard Dogs

Login to post a reply

Server time is: 2025-05-09 06:57:54
Your offset time is: 2025-05-09 06:57:54