Is use of GOSUB bad coding?
still no good example of the valid use of a goto or gosub... makes me think... in fact, i'd even like to see a good example of a break statement... that shouldn't be so hard, should it?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
Gosub/Gotos are bad IMHO but Break can be used to optimise code a great deal! I mean why traverse hugh loops in their entirity looking for matching values when you can just leave the loop when you find what you are looking for. Break, can speed code up no end and it's a very easy command to use and understand in your code, unlike Gosub/Goto, which can lead you anywhere....blueznl wrote:still no good example of the valid use of a goto or gosub... makes me think... in fact, i'd even like to see a good example of a break statement... that shouldn't be so hard, should it?

Not trying to throw more fuel on the fire
but if Break can be used to exit a loop (stopping when a certain condition occurs) and do something, whats the difference in using a GOTO or GOSUB to accomplish the same thing? Isn't GOTO, GOSUB, BREAK and CONTINUE just calling the same flow control method by different names? And, i'm confused as to why GOTO and GOSUB would lead you anywhere when you need to specifically give them a target to GOTO or GOSUB to, when BREAK or CONTINUE do the same thing just in a different place where you don't need to give them a specific target and they just do it because they happen to be the next command in line?

Kale wrote:Gosub/Gotos are bad IMHO but Break can be used to optimise code a great deal! I mean why traverse hugh loops in their entirity looking for matching values when you can just leave the loop when you find what you are looking for. Break, can speed code up no end and it's a very easy command to use and understand in your code, unlike Gosub/Goto, which can lead you anywhere....blueznl wrote:still no good example of the valid use of a goto or gosub... makes me think... in fact, i'd even like to see a good example of a break statement... that shouldn't be so hard, should it?
Im just saying code a program over a few thousand lines of code using goto's then come back in six months and try to understand it. 
There really is no excuse for writing poor code, even if it is for your own use. Also working in teams, it's essential that you make the code as clear and easily readable for the rest of the team, and for that, goto's are a no no!

There really is no excuse for writing poor code, even if it is for your own use. Also working in teams, it's essential that you make the code as clear and easily readable for the rest of the team, and for that, goto's are a no no!
- Joakim Christiansen
- Addict
- Posts: 2452
- Joined: Wed Dec 22, 2004 4:12 pm
- Location: Norway
- Contact:
Example of using GOTO:
Example of using GOSUB.
First, the alternative without GOSUB:
or we could use GOSUB and save loads of typing (especially if the variables have long names), loads of time and loads of unnecessary parameter pushing and it's just as readable, morereadable if the parameters have long names ..
Of course, it could be that PureBasic allows local parameters to be seen outside the procedure in which they are defined which would mean they didn't need to all be passed each time to the check function but some would argue that parameters not remaining local is even worse programming than the use of GOSUB.
I know both of the above can be acomplished without GOTO or GOSUB, but why would you want to? It would be less readable and so less maintainable.
Paul.
Code: Select all
function test(a)
for x = 1 to 1000
for y = 1 to 1000
for z = 1 to 1000
q=f(a,x,y,z)
if q>SomeImportantLimit then GOTO Failed
next
next
next
update something important with q
exit function
Failed:
HandleProblem
end function
Example of using GOSUB.
First, the alternative without GOSUB:
Code: Select all
function check(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,state)
if f(all parameters) > limit then
do one thing
else
do other thing
endif
end function
..
a=1:b=2:c=3...z=26
state=check1
check(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,state)
state=check2
check(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,state)
state=check3
check(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,state)
state=check4
check(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,state)
state=check5
check(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,state)
or we could use GOSUB and save loads of typing (especially if the variables have long names), loads of time and loads of unnecessary parameter pushing and it's just as readable, morereadable if the parameters have long names ..
Code: Select all
check:
if f(all parameters) > limit then
do one thing
else
do other thing
endif
return
a=1:b=2:c=3...z=26
state=check1
gosub check
state=check2
gosub check
state=check3
gosub check
state=check4
gosub check
state=check5
gosub check
..
I know both of the above can be acomplished without GOTO or GOSUB, but why would you want to? It would be less readable and so less maintainable.
Paul.
seriously!Joakim Christiansen wrote:lol, let's just end this discussion rigth herethefool wrote:I can come with some good examples on where and how to use goto.
for app protection.. You need it for self modifying exe's.. you need it for spaghetti code.. fake code.. you need it...
Dioxin's gosub example may be possible using procedures, but im pretty sure the gosub both saves more discspace AND is faster..
SFSxOI,
that's not what I'm saying.
I say that GOTO and GOSUB have their legitimate place. They can be very useful and they aren't the evil codewreckers that some would have you believe.
I see no reason to avoid using them just because others disapprove of them because of an ideal they wish to impose on programming style. Trying at all costs to avoid them will often lead to less easy to follow code.
Let others scoff and laugh all they want but remember, there is a huge body of programmers and computer related personnel out there that will look down on everyone here because you all admit to programming in BASIC. Are they right when they mock users of BASIC? Of course not. Are folk right when they mock users of GOTO? Of course not.
Paul.
that's not what I'm saying.
I say that GOTO and GOSUB have their legitimate place. They can be very useful and they aren't the evil codewreckers that some would have you believe.
I see no reason to avoid using them just because others disapprove of them because of an ideal they wish to impose on programming style. Trying at all costs to avoid them will often lead to less easy to follow code.
Let others scoff and laugh all they want but remember, there is a huge body of programmers and computer related personnel out there that will look down on everyone here because you all admit to programming in BASIC. Are they right when they mock users of BASIC? Of course not. Are folk right when they mock users of GOTO? Of course not.
Paul.
heheHades wrote:@thefool
![]()
Sorry, forget it. It will work. It's just because it's so much against the way I'm thinking to just jump out of a loop, I thought it has to break something.

well perhaps its not a very "fine" thing to do, but it should work.
dioxin: dont forget many purebasic programmers are quite known on the assember and winapi area too..
Lol! I would love for you to demonstrate that last point with a real world example. You are just plain crazy if you think that using goto's make code easier to follow.dioxin wrote:I say that GOTO and GOSUB have their legitimate place. They can be very useful and they aren't the evil codewreckers that some would have you believe...
...Trying at all costs to avoid them will often lead to less easy to follow code.