March 1998: The 80/20 Principle
If you've ever planned a vacation for your family, you realize that a lot of
thought and planning must be put into the process. You need to arrange for
transportation, for a place to stay, and for transportation once you get there.
You need to plan for emergencies. What if one of the kids needs stitches, or
someone needs a special diet or medication? What if your reservations fall
through? You need to be prepared for all sorts of contingencies.
It's the same with planning a macro project. If you dive into any sizable
macro project without giving much thought to how the macro will work, you
will probably waste a lot of time recoding your macro later. You need to plan
for possible errors that might occur, goofy things that the users will try to do
with the macro, and for differences in the users' hardware.
The 80/20 Principle
You will write 80 percent of the macro in 20 percent of the time you spend on
it. You'll spend the other 80 percent of your time tweaking, enhancing, working on
error checking, or fixing bugs that make up 20 percent of the macro. But
that 20 percent is often the part that makes or breaks the macro.
You'll find you can cut down on the time you spend tweaking the macro if you
have planned well before you even start coding. Take into account what might
happen if a file the macro needs to open is not where it should be, or if the
user's screen only displays 640 by 480 resoulution instead of 1024 by 768
resolution, or if the user has selected the WPDOS keyboard rather than leaving
the default keyboard selected, or if one user maps network drives differently
than her coworkers.
So many things can go wrong with even the simplest macro. You need to consider
all possible problems before deploying your finished project or risk looking
foolish or incompetent to your boss and co-workers.
Here are a few pointers for planning macro projects:
Hardcode as little information as possible
When you hard code information, such as filenames, paths, or other data
throughout a macro, you risk getting errors and macro failures down the road.
Always store data in variables that are set at the top of the macro, or get
the information from the user, system variables or the Registry.
Use system variables when at all possible
System variables can return lots of information about the user's system and the
current document. You can get information such as default file paths, what the
current character is, what the position of the cursor is on a page, what page
the cursor is on, and so forth. By getting information dynamically with system
variables you minimize possible errors down the road.
Store information in the Registry or INI
If your macro needs data that is particular to a user, such as what workgroup the
person is in, or what their user color is, store this information in the Registry
or an INI file. You can let your users customize settings for the macro, which
you can store in the Registry. There is no need for each user to be stuck with
the same settings.
Check if documents, directories, or data exist
If your macro needs to open a file, check to make sure that file is there before
opening it. There are several commands in PerfectScript that you can use to check
if certain files or directories exist. For example, DoesFileExist,
DoesDirectoryExist,
GetCurrentDirectory, and so on. Become familiar with
these commands
so that you can use them when you need them.
You can also check whether a certain variable exists or contains a valid value.
If it does not, the macro can query the user for the correct information, store
it in the Registry for later use, and move on to the next task.
You can use IF(EXISTS(vVariable)) to check whether
a certain variable exists and
IF(vVariable <> " ") to check
to make sure a variable has a value.
For example,