Is use of GOSUB bad coding?

Everything else that doesn't fall into one of the other PB categories.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

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... )
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

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?
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.... :wink:
--Kale

Image
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

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:
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?
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.... :wink:
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

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!
--Kale

Image
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

I can come with some good examples on where and how to use goto.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

thefool wrote:I can come with some good examples on where and how to use goto.
lol, let's just end this discussion rigth here :D
dioxin
User
User
Posts: 97
Joined: Thu May 11, 2006 9:53 pm

Post by dioxin »

Example of using GOTO:

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

..
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.
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

Joakim Christiansen wrote:
thefool wrote:I can come with some good examples on where and how to use goto.
lol, let's just end this discussion rigth here :D
seriously!

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..
User avatar
Hades
Enthusiast
Enthusiast
Posts: 188
Joined: Tue May 17, 2005 8:39 pm

Post by Hades »

And his Goto example will get him into deep trouble. :wink:
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

why?
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Hmmm, so are we really saying that the use of GOTO's and GOSUB's are simply less 'elegant' then things like BREAK or CONTINUE?
User avatar
Hades
Enthusiast
Enthusiast
Posts: 188
Joined: Tue May 17, 2005 8:39 pm

Post by Hades »

@thefool
:oops:

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.
dioxin
User
User
Posts: 97
Joined: Thu May 11, 2006 9:53 pm

Post by dioxin »

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.
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

Hades wrote:@thefool
:oops:

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.
hehe :)

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..
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

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.
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.
--Kale

Image
Post Reply