Is use of GOSUB bad coding?

Everything else that doesn't fall into one of the other PB categories.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

:D

Your round, Pupil!
@}--`--,-- A rose by any other name ..
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Actually I use GOTO quite a bit in debugging as typing a label and a GOTO is quicker than commenting out a whole block of code if I want to skip over it to see why the latest Dare2 suggestion broke my code :twisted:
BERESHEIT
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

:lol:
@}--`--,-- A rose by any other name ..
dioxin
User
User
Posts: 97
Joined: Thu May 11, 2006 9:53 pm

Post by dioxin »

There's nothing wrong with using GOTO or GOSUB, they're very useful at times.

If you end up with bad code, don't blame the GOSUB, blame the programmer.

Paul.
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

Pupil wrote: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 ;)
agree 100% :D
Phoenix
Enthusiast
Enthusiast
Posts: 141
Joined: Sun Sep 04, 2005 2:25 am

Post by Phoenix »

jack wrote:
Pupil wrote: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 ;)
agree 100% :D
A very good point!!!
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

Pupil wrote:If you avoid goto at all cost you should logically avoid Break and Continue as well, because they're just Goto's in disguise
I don't agree :P
If you avoid break and continue it would just make your code slow and stupid :D

Code: Select all

ForEach(Person())
  If Person()\Name = "Timmy"
    MessageRequestor("Yeah!","Timmy was found!")
    Break ; <-- We need a sexy break here because we found Timmy and we can stop searching now!
  EndIf
Next
WytRaven
New User
New User
Posts: 5
Joined: Tue May 25, 2004 7:01 am

Post by WytRaven »

Joakim Christiansen wrote:
Pupil wrote:If you avoid goto at all cost you should logically avoid Break and Continue as well, because they're just Goto's in disguise
I don't agree :P
If you avoid break and continue it would just make your code slow and stupid :D

Code: Select all

ForEach(Person())
  If Person()\Name = "Timmy"
    MessageRequestor("Yeah!","Timmy was found!")
    Break ; <-- We need a sexy break here because we found Timmy and we can stop searching now!
  EndIf
Next
I believe you just missed the point.

Play around learning some assembly language..."goto"s are used constantly.
All your code are belong to us
MikeB
Enthusiast
Enthusiast
Posts: 183
Joined: Sun Apr 27, 2003 8:39 pm
Location: Cornwall UK

Post by MikeB »

I don't think I have used a GOTO or a GOSUB since starting with PB. I used to use them with BLITZ on the Amiga before changing to assembly, which was the only way to really get the Amiga going. (Assembly language on the PC is beyond me). However to get back to the subject, I agree that GOTO's can turn your code into spaghetti, but as far as I can see there is little to choose between procedures and GOSUB's except for the fact that passing parameters to a procedure is much neater than using subroutines. Getting results back are much the same except where you only want one return which is obviously easy with a procedure and needs use of variables with a subroutine, as does a procedure for more than one. Otherwise I think that a procedure being called in the same way as a normal command makes for uniformity. Of course now with Macros small GOSUBs are redundant.
Mike.
(I'm never going to catch up with the improvements to this program)
User avatar
Hades
Enthusiast
Enthusiast
Posts: 188
Joined: Tue May 17, 2005 8:39 pm

Post by Hades »

I haven't ever used Continue and I try to avoid Break, but sometimes there's no real option.
But IMO Break and Continue are very different to Goto. It is clearly defined where they go. They are part of a loop, Select or something like that.
In every control structure (If, Repeat, While...) in ASM, there is at least one jump to a label, like with Goto. So you want to say because of that it's all the same?
No! They all have a clear start and a clear end. Goto hasn't. With Goto you can jump everywhere without limits. That's the difference. And that makes the code hard to read and maintain.

And Gosub is just a poor man's Procedure call. I don't see any reason to use it.
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

Hades wrote: But IMO Break and Continue are very different to Goto. It is clearly defined where they go. They are part of a loop, Select or something like that.
I strongly disagree, your logic is somewhat lacking, why? -Because you explicitly tell to which label you will go with a GOTO so it's not undefined or badly defined, it's the opposite, it's clearly defined as is the case with break and continue.
Sometimes i think that it's even harder to find out what you are continuing or breaking out of if you have several nested If:for:while:repeat:select structures that cover more than can fit on a screen at one time, than finding where a label is!
Hades wrote: In every control structure (If, Repeat, While...) in ASM, there is at least one jump to a label, like with Goto. So you want to say because of that it's all the same?
No, thats not what i'm saying(i do realize that this might not be directly aimed at me, but i'll aswer anyway ;)), there's also a significant difference between If, Repeat, While etc and Break, Continue -they have a counter part, Break and Continue don't so you can't relly group them together like that IMO.
Hades wrote: With Goto you can jump everywhere without limits. That's the difference. And that makes the code hard to read and maintain.
I agree that the scope is one thing that makes them different.
However when i use GOTO it's often withing a limited part of the code so for me it's not hard to read or maintain. I also find that sometimes you make the code more unreadable if you at all cost avoid goto and use a multiude of nested If's or whatever to get around what could easily be solved by using a goto.

@Joakim Christiansen

Code: Select all

ForEach(Person())
  If Person()\Name = "Timmy"
    MessageRequestor("Yeah!","Timmy was found!")
    Goto lbl_endsearch ; <-- We need a sexy break here because we found Timmy and we can stop searching now!
  EndIf
Next
lbl_endsearch:
:P
User avatar
Hades
Enthusiast
Enthusiast
Posts: 188
Joined: Tue May 17, 2005 8:39 pm

Post by Hades »

Sure, the Goto will go to the label. Would be a bit difficult do write a program if it would go where it wants to. :roll:

But where is that label? You have to search for it. Easy for a few lines, horrible for a big project.

I've learned Basic on a VC20 in the early 80s. So I was coding with Goto's and Gosub's for many years. But I'm happy it's a thing of the past (at least for me).

I have no problem with you using Goto and Gosub. Do what you like. But it doesn't make it good coding style.
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

Hades wrote:Sure, the Goto will go to the label. Would be a bit difficult do write a program if it would go where it wants to. :roll:

But where is that label? You have to search for it. Easy for a few lines, horrible for a big project.

I've learned Basic on a VC20 in the early 80s. So I was coding with Goto's and Gosub's for many years. But I'm happy it's a thing of the past (at least for me).

I have no problem with you using Goto and Gosub. Do what you like. But it doesn't make it good coding style.
Just saying -you use GOTO ergo you're coding style is bad- is a bit to simple, it's like saying -your shirt is wrinkly, your home must be very untidy- (That's about the same leap of assumtion that you do :))

I'm not saying that the use of GOTO can't/won't mess things up, however bad coding style can and will mess any code up, be it with GOTO or without..
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

All people using GOTO's are gays!! :P
User avatar
Hades
Enthusiast
Enthusiast
Posts: 188
Joined: Tue May 17, 2005 8:39 pm

Post by Hades »

@Pupil

Using Goto at all is bad coding style. That's all I wanted to say. You don't have to agree.
It's like I said: Do what you like.
Post Reply