Company logo


Advice, tips, techniques and downloads for Visual Foxpro developers.

Home | Contact

Versión en español de este artículo

Use #DEFINE to navigate your code

A simple tip that will make navigating large blocks of code a littler easier

A client recently asked us to do some maintenance work on a Visual FoxPro application. The program included a single PRG file with over 2,500 lines of code. We were dismayed to see that the original programmer had made no attempt to divide the logic into small modules, but had coded the whole thing as a single monolithic routine.

Now, that style of programming does not lend itself to easy maintenance. Ideally, we would have liked to take the time to restructure the program into separate procedures and functions, but unfortunately that wasn't possible.

We did, however, hit on a simple way of navigating this monstrous piece of code. We realised that the program was made up of around 25 logical sections. Each of these was a small chunk of code that performed a particular task. At the top of each of these sections, we inserted a #DEFINE directive. This included a meaningful identifier which described the purpose of the code which followed, as in these examples:

#DEFINE Validate_Input 
#DEFINE Calculate_Residue 
#DEFINE Cleanup 

The point about these #DEFINE directives is that they show up in the Document View window (see Figure 1). We therefore had an extremely easy way of navigating to any section of code, with just one mouse click.

Figure 1: The Document View window. The entries with the big red # marks are #DEFINEs.

If you haven't used the Document View window before, it is definitely worth taking a moment to get to know it (it is available in VFP 7.0 and above). You can open it from the Standard toolbar or the Tools menu. Its main job is to display the names of all the procedures, functions and methods defined in a program file, form or visual class. When you click on one of these names, VFP will take you straight to the code for the routine in question. (In the case of forms and classes, you might have to click on the design surface in order to initially populate the Document View window.)

Optionally, the window can also show #DEFINEs and other compiler directives. These options (which are enabled by default) can be found on the menu that pops up when you right-click in the window. It was this feature that made it so easy for us to navigate our huge PRG file.

An especially nice thing about this technique was that, once we had completed the maintenance work, we didn't have to remove the #DEFINEs. Like all compiler directives, these have no impact at run-time, and there is no performance penalty in leaving them in the code.

Mike Lewis Consultants Ltd. November 2003

More Visual FoxPro articles | Crystal Reports articles | Recommended books | Visual FoxPro consultancy | Contact us

FoxStuff is maintained by Mike Lewis Consultants Ltd. as a service to the VFP community. Feel free to download and use any code or components, and to pass around copies of the articles (but please do not remove our copyright notices or disclaimers).

The information given on this site has been carefully checked and is believed to be correct, but no legal liability can be accepted for its use. Do not use code, components or techniques unless you are satisfied that they will work correctly in your applications.

© Copyright Mike Lewis Consultants Ltd.