Page 4 of 6

Posted: Mon May 15, 2006 2:40 pm
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?

Posted: Mon May 15, 2006 2:56 pm
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:

Posted: Mon May 15, 2006 6:58 pm
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:

Posted: Mon May 15, 2006 7:14 pm
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!

Posted: Mon May 15, 2006 7:26 pm
by thefool
I can come with some good examples on where and how to use goto.

Posted: Mon May 15, 2006 7:37 pm
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

Posted: Mon May 15, 2006 7:48 pm
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.

Posted: Mon May 15, 2006 8:06 pm
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..

Posted: Mon May 15, 2006 8:24 pm
by Hades
And his Goto example will get him into deep trouble. :wink:

Posted: Mon May 15, 2006 8:25 pm
by thefool
why?

Posted: Mon May 15, 2006 8:29 pm
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?

Posted: Mon May 15, 2006 8:32 pm
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.

Posted: Mon May 15, 2006 8:51 pm
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.

Posted: Mon May 15, 2006 9:37 pm
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..

Posted: Mon May 15, 2006 9:58 pm
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.