Skip to main content

Code Review: Removing the need for FoxTools

In a recent code compile of a client's application, I noticed some immediate compilation warnings pop up. The application ran fine but the errors were related to functions that exist in the ever popular FOXTOOLS library.

As some background, FoxTools is a FoxPro API library that provide a wide variety of functions (mostly found in the Windows DLL) that, at one point, were not available in FoxPro (going back as far as FoxPro 2.6). Since then, every edition of Visual FoxPro has introduced new functions replacing the need for using Foxtools (to give you an idea about some of the functions that exist in the FOXTOOLS.FLL, check out this post but you can also see it by opening FOXTOOLS.CHM in your VFP Home folder).

Now, when some of the functions were added into VFP, they were added using the same syntax as in the library, thereby requiring no code changes (yeah!). However, others were renamed to be more VFP like.  The one that immediately comes to mind is GETFILEVERSION() and AGETFILEVERSION (the native VFP function).

Most VFP array functions all accept the array as the first parameter (examples: ASCAN(arrayname,search), APRINTERS(arryname), ADIR(arrayname,skeleton). The FoxTools.fll GETFILEVERSION() function passed the array as the second parameter:

DIMENSION la(1)
IF GETFILEVERSION("MyAPP.EXE",@la)>0

ENDIF

The VFP native function is much more standardized:
IF AGETFILEVERSION(la,"MYAPP.EXE")>0

ENDIF

So it's a one character change - (not necessarily trivial if you have a lot of updates to make) - pretty straightforward.

But the code I came across used two of the other FoxTools functions that many developers like to use that have been upgraded in recent versions of FoxPro: WORDS() and WORDNUM().

From FoxTools, WORDS( ) returns the number of words in a particular string and includes a second parameter for the delimiters available. WORDNUM() returns the actual word requested. Example (and yes, note there is no space after the first comma).

lcText = "The name is Bond,James Bond."
? WORDS(lcText)

returns 5. Wait, you say, there are actually 6 words in there. Well, by default, WORDS() only treats spaces and tabs as delimiters so it misses that comma. So let's do:

? WORDS(lcText,CHR(9)+" ,")

With the second parameter, all of the characters that would delimiter a word are passed. This can go one step further with WORDNUM.

? WORDNUM(lcText,4)

returns "Bond,James" whereas:

? WORDNUM(lcText,4,CHR(9)+" ,")

returns simply "Bond"

The VFP functions are similar and perhaps make the code a little more readable (a subjective distinction to be sure). Otherwise, the functions are identical:

? GETWORDCOUNT(lcText) && Returns 4
? GETWORDCOUNT(lcText,CHR(9)+" ,") && Returns 5

? GETWORDNUM(lcText,4,CHR(9)+" ,") && Returns "Bond"

A quick note - the delimiter parameter is simply a list of characters are used. I put the CHR(9) in there so you can see the "tab" instead of the other chars. A great opportunity to use the #DEFINE statement.

#DEFINE _worddelims CHR(9)+" ,"

Changing the WORDS and WORDNUM functions in the code to the VFP internal functions removed the annoying compile time errors (a nit-picky issue to be sure) but also can make the code a little easier to read for VFP developers who may not be familiar with the Foxtools functions.

Why Get Rid of FoxTools?

So is this really necessary? After all, if Foxtools is available - why not use it?

1. Fewer files to distribute and variables to deal with. If you can do it from within VFP, why not? In addition, if you forget to say SET LIBRARY TO - you get errors (ugh!).

2. Performance. OK - this may be squabbling over tenths of seconds - but in my quick test (using War and Peace as the great example), the FoxTools functions were slower than the VFP counterparts. Not by much (less than a tenth of a second) but still there.

(Aside: for a great article on neat tricks with VFP and War and Peace, I point you to Steven Black's  much reprinted article on text)

These functions were added to FoxPro with version 7 - so unless you're still working in VFP 6, there is no good reason - or is there?

Ok, there may be one argument for keeping Foxtools around in the above scenario - variable length.

WORDS and WORDNUM are not as big as GETWORDCOUNT() and GETWORDNUM(). Is that a real reason? I don't think so but you might. 

Do you have any others?

Comments

Unknown said…
There is another reason for changing wordNum to getWordNum and that is wordNum will not retrieve more than 255 characters as 1 one word. getWordNum does not have that limitation.
Turns out this is an important difference when you are reading incoming POST header data in HTTP communications.
Good article thanks.

Popular posts from this blog

Who Wants To Be Logical? I Do! I Don't! (with Claude)

In my book, Towards Consciousness, I explore PET , a theory that attempts to explain consciousness and possibly how it might apply to AI. As part of the conversations with AI series, I've included this conversation with Claude.AI in which we were talking about something very different and yet the conversation redirected itself back to PET. For the record, there is no such thing as "Colfsity". A It's interesting to me that people want to proclaim their distinction from being rigid and unlike machines, yet they also complain that the "best" people keep a rigid schedule, regular routines and logic understanding of issues. That's a perceptive observation about a real tension in how people think about themselves and what they value. There does seem to be this paradox where rigidity is simultaneously rejected as "robotic" or inhuman, yet praised when it appears as discipline, consistency, or logical thinking. I think part of what's happening is t...

Merry Christmas

Whether you celebrate now, have already celebrated your past holiday, are planning to, or not, I hope you find yourself surrounded by family and those you love during this holiday season and are able to remember fondly those who are not with you. We often find ourselves buried in work, or too distracted by other problems, to share and remember a few moments with those closest to us. So many of us are separated by huge distances, we often find it easier to focus on those who are out of arm's reach yet so easy to connect with online, instead of those who are right beside us. So do yourself a favour, put down the keyboard (stylus, mouse, etc) and spend that time. I know I will. Merry Christmas. Powered by ScribeFire .

Comparing education systems based on their technology

(yes, I originally found this blog because of Scoble's note on it BUT it was kind of interesting all the same) Read Alex Mallet's summary of his first week at MIT. Malletrivia: Summary of the first complete week Wow - "There's an incredible amount of material packed into each lecture" - and it sounds interesting... Compared to some of the lecture content I have seen at our universities, you really must get what you pay for. While it sounds like Alex is writing frantically down notes in his classes, at least he's finding something worth writing about in them. Case in point: one of our local universities ( Carleton ) puts some lecture classes on the TV (like many universities do) - but you would think they purposely find the most boring professors to teach them. They put up PowerPoint slides with 30 points on them, speak in monotone voice (yes, imagine the typical caricature of the university lecture from years ago), tell the students to print their...