Page 1 of 1 1
Topic Options
#193261 - 2009-03-30 08:37 PM Enhancing my Sanity
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
(the UDF, that is...)

This post illustrates where Sanity can be helpful. I have recently enhanced the UDF to alert for unterminated command pairs (If/EndIf, Do/Until, While/Loop, and Select/EndSelect) and had been scouring the board for examples of "insane" code. The raw code in the above post has over 250 levels of unterminated IF statements.

The question I have is - what is a REASONABLE level of open If, While, Do, and Select statements? I currently permit 40 levels in the UDF, after which the UDF breaks down and cries (at the condition of your code, and the insanity of it all, not that it can't keep up). Actually, Sanity simply aborts with a "Too many open levels- check your code!" error.

So - is 40 open If statements a reasonable limit? I apply the same limit to the other command-pairs, even though I belive that 40 is too excessive (by about 38!) for nested loops and selects. Personally, I've never used more than 4-5 nested levels of If or loops, even in the complex code projects, but I want to provide something that is useful to all.

The new Sanity will generate an additional report, something like this:
 Code:
Searching for available UDFs.......................
 187 UDFs located in 112 files.
No UDFs needed for this generation!
Generation of test2.kix is complete!

000001: Main
000005:  If $X = 1
000007:  EndIf
000009:  If $X = 1
000010:   While $X < 2
000011:    If $X = 1
000013:    Loop
 - WARNING: Unterminated If when closing While!
000014:   EndIf
000015:  EndIf
000020: Function: X1
000022:  If $X = 1
000024:  EndIf
000026:  While/Loop
000029:  While $X < 10
000031:  Loop
000033:  While $X < 10
000038: EndFunction X1
 - WARNING: Unterminated While when closing function!

000041: Function: X2
 - WARNING: Open If, While, Do or Select when defining function.
000043:  If/EndIf
000046:  Select
000051:  EndSelect
 - WARNING: Unterminated While when closing Select!
000053:  Select
000067: EndFunction X2
 - WARNING: Unterminated Select when closing function!

000069: Function: x3
 - WARNING: Open If, While, Do or Select when defining function.
000071:  If $X = 1
WARNING: Missing EndFunction!
WARNING: 1 Unterminated If statement(s)!
WARNING: 1 Unterminated While statement(s)!
WARNING: 1 Unterminated Select statement(s)!
 8 warnings generated, 73 lines processed.
This output was from KGen. You can see that it indents the code fragments by 1 space per level, making it easy to catch the mismatched items. The actual line containing the If or While is displayed in the report. Between that and the line number it should be easy to locate in your source file.

Comments? Suggestions?

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#193266 - 2009-03-31 01:57 AM Re: Enhancing my Sanity [Re: Glenn Barnas]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Is it possible to make it a user input [optional] variable? I don't think I have ever gone more than 5 or 6 deep so 40 is more than enough for me.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#193268 - 2009-03-31 04:14 AM Re: Enhancing my Sanity [Re: Gargoyle]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
I've been testing with one of my most complex applications.. 6890 lines in the module I'm testing, and the deepest level of open function pairs is 5. I'm thinking that if you need 40 levels, you're doing something wrong. ;\)

It's interesting, because I actually found a problem in the code I'm using to test Sanity - a missing EndSelect that's been "not there" for at least 3 code revisions, so the updated Sanity certainly works.

I figure another day to polish it and it should be ready to publish. In addition to the usual checks for variable declarations and mismatched quotes and parens, it now checks for balanced If/Endif, While/Loop, Do/Until, and Select/EndSelect, including invalid sequences like If/Select/Endif/EndSelect.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#193286 - 2009-03-31 07:36 PM Re: Enhancing my Sanity [Re: Glenn Barnas]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
it would be COOL, if the code would produce also the line of "error":
 Code:
 - WARNING: Unterminated If when closing While!


would then be:
 Code:
 - WARNING: If on line 11 missing an endif!
_________________________
!

download KiXnet

Top
#193290 - 2009-04-01 01:50 AM Re: Enhancing my Sanity [Re: Lonkero]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
I thought about that.. the problem I ran into was that there could be multiple If's open - I'd have to track them all individually rather than simply tracking the number of open item pairs. It's a non-trivial concept, especially when you're already tracking the interaction of IF, While, Do, Select and Function declarations.

Maybe for the next release.. ;\) Of course, if you have some ideas for tracking that, let me know.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#193299 - 2009-04-01 10:41 AM Re: Enhancing my Sanity [Re: Glenn Barnas]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Aren't you just pushing the open construct onto a LIFO/push-down stack and popping it off when a matching close construct is found?

If so it should be trivial to make the stacked item a compound of the open construct and the source line number.

No issue with nested counts either, other than memory/string limits for the stack size.

From your example:
 Code:
000001: Main
000005:  If $X = 1
000007:  EndIf
000009:  If $X = 1
000010:   While $X < 2
000011:    If $X = 1
000013:    Loop
 - WARNING: Unterminated If when closing While!


If you are stacking the constructs then your stack would look like this at the point that you hit the Loop:
 Code:
[0] 00011:IF
[1] 00010:WHILE
[2] 00009:IF


This makes it easy to track back and issue a message like:
 Code:
000001: Main
000005:  If $X = 1
000007:  EndIf
000009:  If $X = 1
000010:   While $X < 2
000011:    If $X = 1
000013:    Loop
 - WARNING: Loop without While
            Possible unterminated "IF" at line 000011
            Possible match with "WHILE" at line 000010

Top
#193313 - 2009-04-01 06:40 PM Re: Enhancing my Sanity [Re: Richard H.]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
LOL - I'm going to have to call Foul here. I don't think Richard is really a System Admin, I think he is a programmer in sheep's clothing (don't tell Shawn)

You have quite a knowledge of IT there Richard. Thanks for being here and sharing.

Top
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 840 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.043 seconds in which 0.021 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org