| < Day Day Up > |
Staying Out of Trouble
Whether you're writing applications that only you will use, that your coworkers will use, or that a client has
Understand Software Requirements
On the one hand, the need to understand requirements might seem obvious, and on another it might seem a bit far afield from this chapter's topic, but as far as preventing software error, understanding requirements is at the top of the food chain. If you don't know what the software you're writing is supposed to do, it's very
So
know what the program is supposed to do
. "
Sometimes it's not possible or advisable to try to scope out
all
the requirements of a project before getting started. If you determine that your project is of this type, you need a different approach. In this case we recommend you explore and familiarize yourself with some of the latest thinking on what software
The basic theory of Extreme Programming (or XP) is to break the work up into small pieces that meet two criteria. In the first place, each piece needs to be complete; that is, it needs to be large enough and inclusive enough that it
The second requirement for such a unit is, of course, that it be small enough that it
is
possible to nail down all the requirements! If you can break a project up into pieces that are each complete, comprehensible, and as
This book isn't intended to be a primer on software development methodologies, so for further information, refer to the
Avoid Unclear CodeAs a programmer or software developer, relatively little of your time will be spent in writing new programs. Most of your time will be spent maintaining and extending old ones. This being the case, it's imperative that the programs you write be easy to extend and maintain. As much as possible, your programs should be both readable and modular . Have you ever returned to a script you wrote six months ago, and find you have no idea what it does or how it does it? Of course you have; so have we all. And when we do this, we're in immediate violation of the "know what you're doing" rule. You don't know what you're doing, and you have to relearn it. You'd better have a good memory!
Much better is to write your program in a way that makes its functioning clear to whomever reads it, be that you or someone else. Two things in particular are important: giving descriptive
Choosing Good NamesAs much as possible, the names you choose should be descriptive and follow clear conventions where possible. We'll offer some suggestions, but they should be taken as just that: suggestions. Think of them more as examples on which you could base your own naming conventions. The most important thing here is consistency: Try to adopt clear rules for naming things, and do your best to stick to them. Databases and Tables
Each database file (a collection of tables) should be named for its overall function. If one file contains all the tables for an invoicing module, call the database
For tables, we recommend that you name the table according to the type of thing that it stores. For intermediate "join" tables, you should give thought to the function of the table and then decide what "thing" it represents. So a join table between Student and Class could be called StudentClass, but is better called Registration. A join table between Magazine and Customer is called Subscription, a join table between Book and Library is Holding, and so forth.
TIP Additionally, we like our table names to be in the singular. So a table of customers is called Customer, a table of pets is called Pet, and so forth. Some join tables don't really evoke a natural function, in which case you may need to fall back on the less descriptive name that just incorporates the names of each file: ProjectEmployee, for example, or OrderPayment.
Some tables are naturally "line item" files. The children of other files, which are
When it comes to field naming conventions, the debates among FileMaker developers often assume the character of holy wars (much like the arguments about bracing style among C programmers). We're not going to inject
Fields
One of the main issues with "fields" in FileMaker is that they're a superset of what we normally think of as database fields. FileMaker fields include, of course, the classic "fields," which are those that store static data, generally entered by users. But they also include fields with global storage, which are not data fields at all, but programming
You, as a developer, need to decide what things you need to be able to distinguish quickly in this thicket of "fields," and
Distinguishing
Making a broad distinction between user fields and developer fields is harder. Those that try to do this generally adopt some kind of overall field name prefix. It's not uncommon to see a scheme where all developer fields are prefixed with an additional z . This puts them all together at the end of the field list, and uses an uncommon letter that's unlikely to overlap with the first letter of a user field (well, except ZIP code, which is common, but you can fudge this by calling the ZIP a "postal code" instead). In a z-based scheme, globals might be prefixed with zg and keys with zkp or zkf .
There are those that swear by such naming schemes, and an equal number that swear
at
them. Even the authors of this book are somewhat divided! I
LayoutsWith naming layouts, again, we'd advocate that you have some clear naming scheme to distinguish between layouts your users interact with directly and those that you build for behind-the-scenes use. One general rule is to prefix the names of all "developer" layouts with Dev_ or a similar tag.
Scripts
With scripts, it's hard to provide any clear guidelines,
Other ElementsThere are, of course, still other areas where improper names can sow confusion, such as the naming of value lists, extended privileges, and custom functions. Function and parameter naming are especially important, so we'll touch on that area as well. It pays to take care when naming custom functions, custom function parameters, and also the temporary "local" variables you create in a Let statement. A few simple choices here can greatly add to the clarity of your code or greatly detract from it. Suppose you have a custom function intended to compute a sales commission, with a single parameter, intended to represent a salesperson's gross sales for the month. To be fully descriptive, you should call this parameter something like grossMonthlySales . That might seem like a lot to type, but if you call it something short and efficient like "gms" you'll be scratching your head over it in a few months' time. The longer name will stay descriptive.
NOTE Notice the capitalization. For custom function parameters and Let variables, we like to use a style called "camel case," popular among Java programmers, in which the first letter of the first word is lowercase and all other words in the name begin with uppercase. We don't use this convention for field names by the way ”there we prefer title case.
Using Comments WiselyA comment is a note that you, the programmer, insert into the logic of your program to clarify the intent or meaning of some piece of it. You can use comments many different ways, but we strongly suggest you find ways that work for you, and use them.
To add a comment to a field, just type your note into the Comment box in the field definition dialog. To view comments, you need to toggle the Comments/Options column of the field list ”the list can display comments or options, but not both at once. Comments can be useful for almost any field. They can be used to clarify the business significance of user data fields, or to add clarity to the use of global and summary fields. Figure 17.1 shows a field list with comments liberally applied.
Figure 17.1. Adding comments to your field definitions can provide useful
|
|
|
Also new in FileMaker 7 is the capability to insert comments into the text of calculations and custom functions. We recommend you make use of this new feature to clarify complex calculations. |
For more information on how to insert comments into the text of calculations and custom functions,
see
"Using Comments Effectively,"
p 1025
.
Finally, FileMaker enables you to add comments to your scripts. Some developers have elaborate script commenting disciplines. They may create an entire "header" of comments with space for the names of everyone who's worked on it, the creation date, and even a full modification history. Figure 17.2 shows an example of such a commenting style.
Other developers use script comments more sparingly, reserving them for places where the flow of the script is less than self-explanatory, or for guiding the reader through the different cases of a complex logic flow. Short, pointed comments throughout a lengthy script can add a great deal to its clarity.
Some developers scorn comments altogether,
TIP
Commenting
For more information on how to use comments,
see
"Using Comments Effectively,"
p. 1025
.
"Modularity" is one of those popular
As a simple example, let's say you're working with a system that prints a lot of reports. Many of the reports need to be printed to a special printer, on legal-
A better practice would be to write a single script called "Print Setup Report Legal" or something to that effect. All the script does is set the print options ”nothing more. When you need to call up that set of print options, call the script. When you need to change the options, change the script. This way you have to write the logic once, and you only ever need to change it in one place.
|
|
FileMaker 7 offers several powerful new features that can greatly increase the modularity of your code if used with discipline. Two of the most important are custom functions and script parameters. These topics have been covered thoroughly in their respective chapters, but it's worthwhile to bring them up here again. You should thoroughly understand the mechanics and uses of custom functions and script parameters, and use them aggressively to make your code more general and extendable. (Bear in mind that custom functions can be created only with FileMaker Developer 7, not with the regular FileMaker Pro 7 product.) |
{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}
For more on custom functions,
see
"Creating Custom Functions,"
p. 411
.
For more on script parameters,
see
"Script Parameters,"
p. 422
.
Like other areas in FileMaker, even seasoned developers can
Let's say we're dealing with a simple problem: We have a system that deals with billing of some kind, and our client has asked that bills that are more than 30 days old appear with their date written in a red
FileMaker's new text formatting calculations make that a snap. Let's say the table has a field called DueDate and another called DueDateDisplay . If the DueDate is less than 30 days ago, the DueDateDisplay field contains the date in a black color; otherwise it contains the date in a red color. It's easy enough to write a calculation for DueDateDisplay that does all these things:
Case( Get(CurrentDate) - DueDate <= 30; GetAsText(DueDate); TextColor (GetAsText(DueDate);RGB ( 255 ; 0 ; 0 ) ))
Well, that works, but there are things that could be better about it. For one thing, it hard-codes quite a number of different elements: What if the rules change so that "Past Due" happens at 45 days? What if they want the color to be a different
Let's tackle these issues one at a time, using custom functions. Chapter 14, "Specialized Calculation Functions," discusses the possibility of using custom functions for "system constants" to abstract out hard-coded values. That's a good place to start. Define one custom function, called getOverDueLimit . That function takes no parameters, and always returns 30. The next thing is to define another custom calc called getOverdueHighlight , and define it to always return RGB(255;0;0) .
So far, so good. This abstracts out some of the hard-coded values. Next, you need a generic way to determine whether a record is overdue. You could write a custom function called
isOverdue(dateValue)
, defined as
Case( dateValue + getOverdueLimit < Get(CurrentDate); 0; 1)
This function takes a
dateValue
,
The next function is called colorIfOverdue( dateValue, textValue, rgbColor) . This function's job is to take a date, a piece of text, and a color, and apply the color to the text if the date is overdue. The definition looks like this:
Case( not IsOverdue ( dateValue ); textValue; TextColor ( textValue ; rgbColor) ) // last parameter needs to be an RGB color value, i.e. the result of RGB(x,y,z)
(Notice, by the way, that this function uses a comment to clarify the data type of the third parameter.) This function draws on the previously created isOverdue function to do its work. Notice also that this function doesn't assume anything about the color to apply to the text, or even what the text is ”it just applies a color to a text string based on a date.
With these custom functions created, it's finally time to do some work based on actual fields. So the DueDateDisplay field can now be redefined very simply as
colorIfOverdue ( GetAsText(DueDate); DueDate, getOverdueHighlight() )
It might seem like that was a lot of work, but look at the advantages. If you decide you want a different color for all your overdue highlights, you need to change only the definition of
getOverdueHighlight
. If you decide to let bills go 45 days instead of 30, you need to change only the definition of
isOverdue()
. And if you need additional fields to highlight according to whether a record is overdue, you don't need to replicate any of the
That's an example of what we'd call
| < Day Day Up > |