Comments
Everything you can put in a calculation has some kind of value…unless it's a comment. Comments are chunks of text whose sole purpose is to help guide you through long calculations. Professional database developers, like all good programmers, provide lots of comments for the benefit of people who might work on the computer code months or years later. Once you've a few sets of parentheses or nested function calls, you may have trouble understanding even your own FileMaker calculation when you have to go back and make changes. When it evaluates the calculation, FileMaker ignores all comments completelyit's as if they weren't there. (But you'll be glad they are.)
You can use two different styles for your comments. First, any text that comes after two consecutive slash marks (//) is considered a comment. This kind of comment goes all the way to the end of the line.
// this is a comment 3.14 * Diameter // and so is this
A comment is also any text that comes between the symbols /* and */. This symbol pair comes in handy in two places. It saves typing if you need to type a long comment across multiple lines:
/* this is a comment that runs across multiple lines. To make life easier, you can use the second comment style */
Also, this comment style lets you add comments within a line:
3 /*sprocket size*/ * 10 /*sprocket count*/ * 57 /*tooth count*/
In addition to comments, you canand shoulduse white space to make your calculations easier to read. Calculations don't have to be strung together in one long line, even though that's the way FileMaker does it in the Specify Calculation dialog box. Press the Return key or Space bar to add space anywhere, except in a field name, function name, text constant, or number. Comments and white space can make a world of difference. Here's a long calculation that doesn't make use of either:
Let([NewText = ":" & Path & ":"]; Case(Item < 0; Let( [TextLen = Length(NewText); Pos = Position(NewText; ":"; TextLen; Item - 1) + 1; Len= Position(NewText; ":"; TextLen; Item) - Pos ]; Middle(NewText; Pos; Len)); Let( [Pos = Position(NewText; ":"; 1; Item) + 1; Len= Position(NewText; ":"; 1; Item + 1) - Pos ]; Middle(NewText; Pos; Len))))
Ten points to the first person who can make sense of that mess. Now here's the same calculation, written with more care:
// Parsing is easier if we can be sure we have delimiters // on both ends of the text Let( [NewText = ":" & Path & ":"]; Case( // If the item number is negative, we count from the end Item < 0; Let( [TextLen = Length(NewText); Pos = Position(NewText; ":"; TextLen; Item - 1) + 1; Len= Position(NewText; ":"; TextLen; Item) - Pos ]; Middle(NewText; Pos; Len) ); // If the item number is positive, we count from the // beginning Let( [Pos = Position(NewText; ":"; 1; Item) + 1; Len= Position(NewText; ":"; 1; Item + 1) - Pos ]; Middle(NewText; Pos; Len) ) ) )
It may not be a picnic to run through in your head, but it's a lot easier to follow than before.