New topic: 

Distinguishing between End, End If, and End Select

<http://forums.realsoftware.com/viewtopic.php?t=46869>

         Page 1 of 1
   [ 6 posts ]                 Previous topic | Next topic          Author  
Message        barrytraver          Post subject: Distinguishing between End, 
End If, and End SelectPosted: Thu Feb 07, 2013 9:35 pm                         
Joined: Fri Sep 30, 2005 1:53 pm
Posts: 813
Location: Philadelphia, PA                This is one of "Various questions" 
discussed in another thread in this Forum, but I think it is important enough 
to have a thread of its own.

In REALbasic the "If" in "End If" and the "Select" in "End Select" are optional 
(that is, unnecessary).  Let's assume that Programmer 1 uses just plain "End" 
throughout a program, not only for "End", but also for "End If" and "End 
Select" Let's further assume that Programmer 2 (or his boss) prefers using "End 
If" and "End Select" because the three choices makes the code easier to follow.

Here's the question"  What tests can Programmer 2 apply to every "End" in a 
program by Programmer 1 in order to determine whether it should be "End", "End 
If", or "End Select"?

It's not as easy as might at first appear.  Take, for example, this code:
daynujmber = 6
If daydesired Then
  Select Case dayNumber
  Case 6
  MsgBox "It's Friday!:
  End
Case Else
  MsgBox "It's not Friday!"
End
Else
' Do nothing.
End
End


I realize that's not good code, but the point is that it is not easy to 
determine which "End" is meant.

It's simple with nested For/Next loops.  The rule is "Last in, first out."  if 
you have "For X", "For Y", and "For Z" in that order, you need a "Next Z", 
"Next Y", and "Next X" in that order later in the code.  But since "End" can go 
almost anyplace in the code, how can you distinguish between them?

There have to exist some rules for this, since even though the programmer may 
use a simple "End" throughout I would assume that the compiler needs to know 
the meaning of each "End" in the program (whether it is "End If", "End Select", 
or "End").

Thanks in advance for any thoughts on ths question.

Barry Traver   
                             Top                npalardy          Post subject: 
Re: Distinguishing between End, End If, and End SelectPosted: Thu Feb 07, 2013 
10:03 pm                       Real Software Engineer          
Joined: Sat Dec 24, 2005 8:18 pm
Posts: 7716
Location: Canada, Alberta, Near Red Deer                last in first out works 
just fine here as well

daynujmber = 6
If daydesired Then // needs END #1
  Select Case dayNumber // needs END #2
  Case 6
  MsgBox "It's Friday!:
  Case Else
  MsgBox "It's not Friday!"
  End // END #2
Else
  ' Do nothing.
End // END #1


it can get convoluted with #if Targetxxx embedded like

#if targetMacOS
  If a = 1 then // needs END #1
  #endif
  
  if b = 2 then // needs END #2
  end if  // END #2
  
  #if targetMacOS
  end if // END #1
#endif


But it IS still last in first out      
_________________
My web site Great White Software
RSLibrary.com REALbasic learning  
                             Top                ktekinay          Post subject: 
Re: Distinguishing between End, End If, and End SelectPosted: Thu Feb 07, 2013 
11:23 pm                                 
Joined: Mon Feb 05, 2007 5:21 pm
Posts: 431
Location: New York, NY                I've been thinking about this, and came 
up with this: Go through the code line by line. For every line that starts with 
IF or SELECT, add it to an array. When you encounter an END, pop the last item 
off of the array and parse that (lastItem.NthField( " ", 1 )) and add it to the 
END. IF the blocks aren't balanced, this will fail but you can even add that as 
comments to the end of the code.      
_________________
Kem Tekinay
MacTechnologies Consulting
http://www.mactechnologies.com/

Need to develop, test, and refine regular expressions? Try RegExRX.
  
                             Top                DaveS          Post subject: 
Re: Distinguishing between End, End If, and End SelectPosted: Fri Feb 08, 2013 
12:31 am                                 
Joined: Sun Aug 05, 2007 10:46 am
Posts: 4506
Location: San Diego, CA                do it just like the compiler would... as 
said above LIFO.. last in first out

When you hit IF/SELECT  push onto the stack... when you hit END, pop it off

when you get to the end of the procedure.. the stack had better be at zero.....
if it is > 0 then you had more IF/SELECT than END  , if <0 then you had more 
END than you did IF/SELECT      
_________________
Dave Sisemore
MacPro, OSX Lion 10.7.4 RB2012r1
Note : I am not  interested in any solutions that involve custom Plug-ins of 
any kind  
                             Top                timhare          Post subject: 
Re: Distinguishing between End, End If, and End SelectPosted: Fri Feb 08, 2013 
12:50 am                         
Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12055
Location: Portland, OR  USA                Make sure you account for 
single-line IFs.
if somecondition then
  if foo then bar
  if foo then bar else baz
end

I find it unnecessary to use end-if/end-select since the IDE draws a line from 
if to end and will hightlight or even collapse the whole thing.  There are 
better visual identifiers, in my opinion, than "end if".  "Next I" has a 
stronger case, but it's still visually evident what it's connected to.   
                             Top                kermit          Post subject: 
Re: Distinguishing between End, End If, and End SelectPosted: Fri Feb 08, 2013 
2:20 am                         
Joined: Mon May 30, 2011 12:56 am
Posts: 603                I too never use a goto.
The problem with goto as I understood it goes back to the days when people 
didnt use multi-file projects.. everything was in one long BASIC file, and GOTO 
could be used to leap into the middle of completely unrelated areas of the 
code, such as a subroutine.
That makes things REALLY hard to debug after the fact, even if it worked when 
written.

I did find this amusing:

Quote:If you are talking about multiple nested for statements, you can just use 
the exit command.. If you have something like the following (really quick 
simple code)

for i
  for j
  for k
  exit i  // will break out of the main loop
  next
  next
next

. is it me, or is exit i  here directly equivalent to  GOTO  (some line just 
after the last NEXT statement)?

Its neat, but it is a 'hidden' GOTO


But the avoidance of that would involve the use of boolean 'getmeout' variables.

dim bGetMeOut as boolean = false
for i
  if not(GetMeOut) then
  //i loop stuff
  
  for j
  if not(GetMeOut) then
    
    //j loop stuff
    for k
    if not(GetMeOut) then
    //k loop stuff
    end if
    next
  end if
  next
  end if
next


So if GetMeOut becomes true anywhere in there, the loops stop executing.
No, I wouldnt like to use that myself.

_________________   
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 6 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to