Your round, Pupil!
Is use of GOSUB bad coding?
- netmaestro
- PureBasic Bullfrog

- Posts: 8452
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
agree 100%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 teachersIf you avoid goto at all cost you should logically avoid Break and Continue as well, because they're just Goto's in disguise
A very good point!!!jack wrote:agree 100%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 teachersIf you avoid goto at all cost you should logically avoid Break and Continue as well, because they're just Goto's in disguise
- Joakim Christiansen
- Addict

- Posts: 2452
- Joined: Wed Dec 22, 2004 4:12 pm
- Location: Norway
- Contact:
I don't agreePupil 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
If you avoid break and continue it would just make your code slow and stupid
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
NextI believe you just missed the point.Joakim Christiansen wrote:I don't agreePupil 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
If you avoid break and continue it would just make your code slow and stupid![]()
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
Play around learning some assembly language..."goto"s are used constantly.
All your code are belong to us
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)
(I'm never going to catch up with the improvements to this program)
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.
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.
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.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.
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
No, thats not what i'm saying(i do realize that this might not be directly aimed at me, but i'll aswer anywayHades 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?
I agree that the scope is one thing that makes them different.Hades wrote: With Goto you can jump everywhere without limits. That's the difference. And that makes the code hard to read and maintain.
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:
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.
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 doHades 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.
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..
- Joakim Christiansen
- Addict

- Posts: 2452
- Joined: Wed Dec 22, 2004 4:12 pm
- Location: Norway
- Contact:


