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

Blogs and RSS come to Microsoft.com

MS has just introduced their portal and it's pretty comprehensive. Nothing quite like learning that some people use AIM instead of MSN messenger, or that there really may be a need for supporting 4 monitors ( Cyrus Complains ) However, it's really a great sign that MS is serious about supporting the blogging community which seems to have um, exploded in size in the past year. Blogs and RSS come to Microsoft.com

Elevating Project Specifications with Three Insightful ChatGPT Prompts

For developers and testers, ChatGPT, the freely accessible tool from OpenAI, is game-changing. If you want to learn a new programming language, ask for samples or have it convert your existing code. This can be done in Visual Studio Code (using GitHub CoPilot) or directly in the ChatGPT app or web site.  If you’re a tester, ChatGPT can write a test spec or actual test code (if you use Jest or Cypress) based on existing code, copied and pasted into the input area. But ChatGPT can be of huge value for analysts (whether system or business) who need to validate their needs. There’s often a disconnect between developers and analysts. Analysts complain that developers don’t build what they asked for or ask too many questions. Developers complain that analysts haven’t thought of obvious things. In these situations, ChatGPT can be a great intermediary. At its worst, it forces you to think about and then discount obvious issues. At best, it clarifies the needs into documented requirements. ...

Programmers vs. Developers vs. Architects

I received an email this morning from Brandon Savage 's newsletter. Brandon's a PHP guru (works at Mozilla) but his newsletter and books have some great overall perspectives for developers of all languages. However, this last one (What's the difference between developers and architects?) kind of rubs me the wrong way. Either that, or I've just missed the natural inflation of job descriptions. (maybe, it's like the change in terminology between Garbage man and Waste Engineer or Secretary and Office Administrator) So maybe it's just me - but I think there's still a big difference between Programmer, Developer and then of course, architect. The key thing here is that every role has a different perspective and every one of those perspectives has value. The original MSF create roles like Product Manager, Program Manager, Developer, Tester, etc - so every concept may pigeon hole people into different roles. But the statements Brandon makes are often distinction...