Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Mon Dec 11, 2017 2:30 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 55 posts ]  Go to page Previous  1, 2, 3, 4  Next
Author Message
 Post subject: Re: Various questions.
PostPosted: Tue Jan 29, 2013 3:55 pm 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
briman wrote:
GoTo is fine , if you are planning on making spaghetti for dinner :D

Ragu? or Prego?

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
Reply with quote  
 Post subject: Re: Various questions.
PostPosted: Tue Jan 29, 2013 4:02 pm 
Offline
User avatar

Joined: Fri Sep 30, 2005 9:27 am
Posts: 249
Location: USA, CA
DaveS wrote:
briman wrote:
GoTo is fine , if you are planning on making spaghetti for dinner :D

Ragu? or Prego?


prego of course, because "It's in there" (R)

:D

_________________
-Briman


Top
 Profile  
Reply with quote  
 Post subject: Re: Various questions.
PostPosted: Tue Jan 29, 2013 4:42 pm 
Offline

Joined: Wed May 25, 2011 10:18 am
Posts: 33
DaveS wrote:
While I agree programming is an art, and there is no "right" way, there is alot of "WRONG" ways....

And I will speak up here and say

"THE USE OF 'GOTO' AND 'GOSUB' IN AN EVENT DRIVEN OBJECTED ORIENTED PROGRAMMING LANGUAGE IS EVIL"

And should be avoided at all cost..... [along with data bindings]

Other than that... the rest is up to you


The use of CONTINUE, BREAK and EXIT should be considered first.

I use these usually, and it is not that I use Goto regularly, but there are situations where Goto is just fine. Deep nested long code is a potential source for more bugs, and Goto can help to make things clearer.
However, I never use it to go upward in the code, always only downwards. I would agree that jumping forth and back is a source for nice spaghetti.

Kind regards
Christian

_________________
me.self works with:
Real Studio Pro (always the latest), 10.7.x, Mac mini i5, Love


Top
 Profile  
Reply with quote  
 Post subject: Re: Various questions.
PostPosted: Tue Jan 29, 2013 4:48 pm 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
I stand to disagree 100%.... and I've been at this a LONG time

If ANY developer on my team EVER submitted code to me for review that contained GOTO/GOSUB.

First time we would have a "discussion"
Second time... he/she would be fired.. period.

And I will tell you, that feeling is not just mine.. but shared with a huge percentage of IT professionals....

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
Reply with quote  
 Post subject: Re: Various questions.
PostPosted: Tue Jan 29, 2013 5:14 pm 
Offline

Joined: Wed May 25, 2011 10:18 am
Posts: 33
Hard words. So what are the reasons to not directly skip a portion of code with Goto?

Kind regards
Christian

_________________
me.self works with:
Real Studio Pro (always the latest), 10.7.x, Mac mini i5, Love


Top
 Profile  
Reply with quote  
 Post subject: Re: Various questions.
PostPosted: Tue Jan 29, 2013 5:35 pm 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
two words.

STRUCTURE
MAINTENANCE

Do "gotos" work? Sure... and for every GOTO you need a destination. Don't use a GOTO, you don't need a label gumming up the structure. Are Goto/Labels easily followed? In a small program, sure.. In a large one.. nope.

Maintenance.... with lousy structure comes difficult maintenace. On my development teams. READABILITY is #1, PERFORMANCE is #2 ..... and usually if they get #1 right, #2 follows automatically


bad
do
i=i+1
if i>5 then goto labelx
//
other stuff
//
labelx:
loop



better
do
i=i+1
if i>5 then exit do ' you know EXACTLY what is going to happen without wondering where is it going?
//
other stuff
//
loop




bad
do
i=i+1
if i>5 or i=22 then goto labelx
//
other stuff
//
labelx:
loop



better
do
i=i+1
if i>5 or i=22 then continue ' you know EXACTLY what is going to happen without wondering where is it going?
//
other stuff
//
loop

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
Reply with quote  
 Post subject: Re: Various questions.
PostPosted: Tue Jan 29, 2013 5:47 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
I think for your first example you meant to have the label outside the loop.
do
i=i+1
if i>5 then goto labelx
//
other stuff
//
loop
labelx:

I agree with Dave. I think that if you feel you might need a goto, it's an indication that it's time to refactor your code. That may be the biggest problem with goto, it's a quick fix that keeps you from really thinking hard about what you're doing.


Top
 Profile  
Reply with quote  
 Post subject: Re: Various questions.
PostPosted: Tue Jan 29, 2013 8:35 pm 
Offline

Joined: Wed May 25, 2011 10:18 am
Posts: 33
I fully agree on readability, and your examples make sense. However, in deeper nested and probably long methods Goto can improve readability if you name the target label meaningful. You see at a glance that all the following code will not be executed and what will happen (with a meaningful label), no matter how nested, looped, long or complicated the following code is. Here's an example that roughly illustrates a real method. It is in reality about 3 pages long, including comments of course. And no, my methods are usually by far not that long.

This is the version I wouldn't hire someone for:

if Test1() then

if Test2() then

if Test3() <> 0 then

if Test4() then

if Not Test5() then

if Test6() then

if gReleaseF then // Here we want to skip the following test which are only neccessary in the release version.

if Test7() AND NOT Test77() then

if Test8() then
if Test88() = "blah" then
var1 = 3
else
var1 = 7
end
if Test9() then
var3 = var1
if Test10() then
gCTRL.appLaunchedSuccessfullyF = true
end

end

end

end
else
gCTRL.appLaunchedSuccessfullyF = true

end

else
// do something

end

end

end
end

end

end


if gCTRL.appLaunchedSuccessfullyF then
// do my final stuff
OrderMilk()
OrderMoreMilk()
EvenMore()
else
lg(".MethodLeavedEarly:" + methodName + "(" + pos + ")")
end


Exception err
ExceptionStandardHandling(err, methodName, pos)






The following code is IMHO:
- easy to read
- the logic is easy to follow
- the logic is easy to change
- the order of tests is easy exchangeable
- additional code is easy to add

if Not Test1() then     GOTO MethodLeavedEarly
if Not Test2() then GOTO MethodLeavedEarly
if Test3() = 0 then GOTO MethodLeavedEarly
if Not Test4() then GOTO MethodLeavedEarly
if Test5() then GOTO MethodLeavedEarly

if Not Test6() then
GOTO MethodLeavedEarly
else
// do something
end

// Here we want to skip something that's only neccessary in the release version.
if Not gDebugReleaseF then GOTO SuccessfullyLaunched

if (Not Test7()) AND Test77() then GOTO MethodLeavedEarly
if Not Test8() then GOTO MethodLeavedEarly

if Not Test88() = "blah" then
var1 = 3
else
var1 = 7
end

if Not Test9() then GOTO MethodLeavedEarly

var3 = var1

if Not Test10() then GOTO MethodLeavedEarly


SuccessfullyLaunched:
gCTRL.appLaunchedSuccessfullyF = true
// do my final stuff
OrderMilk()
OrderMoreMilk()
EvenMore()

Exception err
ExceptionStandardHandling(err, methodName, pos)
If 1 = 2 Then ' Just to isolate from normal execution
MethodLeavedEarly: '
If gDebugAppFlowMethods > 0 Then lg(".MethodLeavedEarly:" + methodName + "(" + pos + ")")
End




Kind regards
Christian

_________________
me.self works with:
Real Studio Pro (always the latest), 10.7.x, Mac mini i5, Love


Top
 Profile  
Reply with quote  
 Post subject: Re: Various questions.
PostPosted: Tue Jan 29, 2013 8:44 pm 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
we can argue for ever... and probably never convince you that I'm right :D

but I would suggest this as an alternative

if Test1()  or Test2()  or Test3()<>0 or Test4() or Not Test5() or Test6() then
if gReleaseF then // Here we want to skip the following test which are only neccessary in the release version.
if Test7() AND NOT Test77() then
//
// stuff
//
end if
end if
end if


Alot less indenting.... and possibly easier on the compiler

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
Reply with quote  
 Post subject: Re: Various questions.
PostPosted: Tue Jan 29, 2013 8:49 pm 
Offline

Joined: Mon Aug 14, 2006 9:33 pm
Posts: 1774
If not test1() Then
LeaveEarly()
Return
End if


the "Goto" is superfluous.
That having been said, you are obviously committed to your way of doing things and no amount of preaching from the rest of us is likely to change your mind. Good Luck.
P.S. PLEASE don't teach young programmers this way of doing things. :-)

_________________
Roger Clary
Class One Software
Educational Software for Lifelong Learning
http://www.classonesoftware.com


Top
 Profile  
Reply with quote  
 Post subject: Re: Various questions.
PostPosted: Tue Jan 29, 2013 10:41 pm 
Offline
User avatar

Joined: Sun Jan 25, 2009 5:11 pm
Posts: 576
Location: Guatemala, Central America
In the last example I wouldn't use a goto either, I would declare a variable to use it as a flag, do all the checking and raise lower the flag as needed then check against the flag wheather the proceeding code has to be executed or not and either exit the sub or continue.

Goto is evil.

_________________
Future RS guru.
Ride the world!


Top
 Profile  
Reply with quote  
 Post subject: Re: Various questions.
PostPosted: Tue Jan 29, 2013 10:43 pm 
Offline
User avatar

Joined: Mon Feb 05, 2007 5:21 pm
Posts: 600
Location: New York, NY
DaveS wrote:
but I would suggest this as an alternative

Or this.
dim proceed as boolean

// Tests - if any are false, we can stop
select case false
case Test1()
case Test2()
case Test3() <> 0
case Test4()
case not Test5()
case Test6()
case gReleaseF
case Test7() and not Test77()
case Test8()
else
proceed = true
end select

if proceed then
if Test88() = "blah" then
var1 = 3
else
var1 = 7
end
end if
if proceed and Test9() then
var3 = var1
if Test10() then
gCTRL.appLaunchedSuccessfullyF = true
end
end

Is this the "right" way? It's up to you, but my point is, there are plenty of alternatives that can make the code easier to follow without resorting to GOTO.

_________________
Kem Tekinay
MacTechnologies Consulting
http://www.mactechnologies.com/

Need to develop, test, and refine regular expressions? Try RegExRX.


Top
 Profile  
Reply with quote  
 Post subject: Re: Various questions.
PostPosted: Wed Jan 30, 2013 5:20 am 
Offline

Joined: Wed Sep 17, 2008 2:45 pm
Posts: 220
Just for the sake of the anti-GOTO alliance, I'll make a reply.

DaveS is right, GOTO's are not good. Why? There based on an idea that is 40 years old, even long before OOP said 'hi' in the programmers world.
GOTO is procedural, RS is mostly OOP based. So there is absolutely no reason why you would even think about using it. (Before this thread, I had no idea it was even implemented in RS)

That said, many of the functions I saw in this post weren't really OOP based, functions should be small, and not having 10 if statements with some GOTO's in it.

_________________
My native language is not English.


Top
 Profile  
Reply with quote  
 Post subject: Re: Various questions.
PostPosted: Wed Jan 30, 2013 7:49 am 
Offline
User avatar

Joined: Fri Jul 09, 2010 7:16 am
Posts: 103
Location: Den Haag, Netherlands
I've programmed in Dibol 11 (many, many, many years ago).
In Dibol 11 the only option was programming with GOTO's, IF's but without the ELSE.
With Dibol 87 it was possible to use CALL, USING, IF THEN ELSE.
Since Dibol 87, I hardly ever used GOTO's.
The only time GOTO was used was to go to the end of the program.
In Real Studio I never felt the need for using GOTO's.

_________________
MacBook Pro (late 2008), OSX Mountain Lion 10.8.2, Real Studio 2012r2


Top
 Profile  
Reply with quote  
 Post subject: Re: Various questions.
PostPosted: Wed Jan 30, 2013 10:22 am 
Offline

Joined: Wed Jan 12, 2011 12:48 pm
Posts: 53
Location: Fresno, CA
You know there are a lot of functions/methods/styles that are difficult to use (and often used incorrectly or in inefficient ways).

GoTo is one such example. I love GoTo and while I rarely find need for it, knowing it's there makes me smile.

Everyone always assumes the idea of GoTo is to move forward.

One of the fantastic capabilities GoTo offers us is the ability to go backwards in the code with no penalty. When you compile your code it becomes something besides text and line numbers. GoTo allows you to think about your code as data itself and move through it at will.

Now I repeat it's difficult to come to an example of why this would need to be done. I know with text parsing and building compilers in Real Studio I have used it. One example being perhaps I do not desire to have a method in my class to be called each time I need to do something. Maybe I need to literally nest methods inside of an existing method. One way to accomplish this would be with GoTo's. I have used this in RbScript too where I may need to add pseudo-methods that aren't tied to a context. Using introspection, goto's, operator_converts, etc. you can actually make RealBasic feel like a dynamic language which is fun.

Just food for thought in advanced use cases. Typically speaking for standard day to day stuff I can't imagine GoTo being 'proper'.

_________________
Thanks,
Phillip Zedalis
Managing Developer
http://www.1701software.com
773-236-1701


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 55 posts ]  Go to page Previous  1, 2, 3, 4  Next

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group