Page 1 of 6

Is use of GOSUB bad coding?

Posted: Wed May 10, 2006 5:53 pm
by SFSxOI
OK, I understand that the GOSUB may be considered a sort of hold over from earlier basic version (is it??)....but is it considered bad coding practice to use GOSUB?

Posted: Wed May 10, 2006 6:07 pm
by Killswitch
It is considered bad practice to use GOSUB, but that doesn't mean you can't - or shouldn't.

Re: Is use of GOSUB bad coding?

Posted: Wed May 10, 2006 7:05 pm
by Kale
SFSxOI wrote:...it considered bad coding practice to use GOSUB?
Yes!


(IMHO)

Posted: Wed May 10, 2006 7:15 pm
by dracflamloc
Its ugly and can cause tons of bugs in the future.

Posted: Wed May 10, 2006 10:11 pm
by Barney
Hmm... Why would use of GOSUB be ugly. Down at the machine code level using a function or procedure call is just the same. Push a return address onto the stack, call a piece of code and return to caller. It's neither ugly, nor pretty and it's basically the same logic for all kind of higher level calls.

Also... let's not forget we've put man onto moon and we used quite a lot of GOTO's and GOSUB's doing it ;)

Barney

Posted: Wed May 10, 2006 10:35 pm
by Dare2
De-Optimised code:

Code: Select all

ready.l = #True

If ready
  Gosub moon
Else
  Goto Mission_Abort
EndIf
End

moon:
MessageRequester("HELLO HOUSTON","The Eagle has landed",#MB_ICONEXCLAMATION)
Return   ; <-- The important bit if you're an astronaut

Debug "OOPS"

Mission_Abort:
Debug "DARN"
End

Posted: Wed May 10, 2006 10:54 pm
by jack
in my opinion, a Goto or Gosub used judiciously is OK, however you can
quickly make undecipherable spagetti code.

Posted: Wed May 10, 2006 11:23 pm
by rsts
There's really nothing wrong with a well formed sub-routine. Procedures tend to be considered better for re-useable code.

Goto's on the other hand are generally thought of as something you want to avoid, as they tend to be more difficult to follow and debug.

cheers

Posted: Wed May 10, 2006 11:26 pm
by Kale
IMHO Gosubs should be avoided at all cost because of the difficulty in following and debugging such code. Properly designed software should not need Gosubs.

Posted: Wed May 10, 2006 11:26 pm
by MadMax
I haven't used a goto or gosub in years. There is no need to use them, and only leads to trouble for yourself. Last time I used GOTO was about 5 years ago, I didn't take something into account, so I had the option of rewriting most of the code or using a GOTO to save my day. But normaly you wouldn't want to use it, not because "the gurus" say it's bad practice, but because it's cumbersome, overcomplicates code, makes debugging a pain, etc etc. But on the other hand, if that's your style and you are happy with it... :roll:

But, why one would want to use GOTOs, line numbers, single lettered variables, etc is beyond me. :?

Posted: Wed May 10, 2006 11:41 pm
by Kale
MadMax wrote:But, why one would want to use GOTOs, line numbers, single lettered variables, etc is beyond me. :?
Agreed! With todays modern processors and high spec systems, writing code should focus more on readability and maintainability. Gosubs, IMHO, are relics of the past, where quick and dirty hacks were needed. When i personally write code today and come across a problem discribed by madmax where you need to either re-write large chunks of code or use dirty hacks, i always re-write! Believe me, it will pay off in the long run! ;)

Posted: Thu May 11, 2006 1:50 am
by johnfinch
Gosub was the first form of Basic procedural programming. More modern 'Procedures' are far more elegant and useful. Goto, on the other hand, was always thought of as poor programming practice (hence the creation of the Gosub command). I agree with everyone here, use Procedures, maintain logical and easily maintained code, and, if you need to use a Goto, you need to re-write your code logic.

Posted: Thu May 11, 2006 4:17 am
by Phoenix
Using Gosubs, single-letter variables, and such depends on what sort of person you are. If your code is for your own use and you have no problem organizing yourself, then there is no valid reason not to use it. But if your code is for others, then try not to use it because your code may be too hard for them to follow. Personally, I have no issues with it and can follow spaghetti code quite easily with no confusion, but then again, I'm told I'm like Dustin Hoffman in the movie "Rain Man". Heh.

Posted: Thu May 11, 2006 11:57 am
by netmaestro
GOSUB is the early attempt at enabling structured, readable code. But as they can't receive parameters or return values, they aren't much good for that. Just interesting relics, imho. No real place in modern coding, I imagine they're just kept around for backwards-compatibility reasons.

Posted: Thu May 11, 2006 12:30 pm
by Pupil
Most anything you need to use Gosub for can easily be replaced by a procedure which imho improves readabillity alot. However on the subject of GOTO, i can't really understand all the rabid goto haters, you've been brainwashed by your teachers ;) If you avoid goto at all cost you should logically avoid Break and Continue as well, because they're just Goto's in disguise ;)