Better Boolean Tests: True means go


There are many situations in FileMaker where you will use a Boolean expression. This is an expression that evaluates to either True or False.

In scripting, the most common place for a Boolean expression is in an If statement.

Is it true that...?

If we are testing for an overdue item, we can write:


If [ itemReturnDate < Get ( CurrentDate ) ] 
  Perform Script [ "Run overdue process" ]
Else
  # ignore the item
End If

The Boolean expression in the If statement evaluates to True when the item return date is before (less than) today (current date). In that case, we run the overdue process; otherwise we ignore the item.

Since we are going to do nothing if the item is not overdue, it is not strictly required to include the Else branch. However, it is a characteristic of explicit programming to include Else and a comment to communicate to the reader that you intended for nothing to happen.

In the above, we see an example of a positive Boolean test. The intention of the code is to test whether the overdue process needs to be run. So the Boolean expression is written to return True when the item is overdue.

Compare that with the reverse:


If [ itemReturnDate ≥ Get ( CurrentDate ) ]
  # ignore the item
Else
  Perform Script [ "Run overdue process" ]
End If

Both examples produce the same result, but the second version forces the reader to mentally invert the logic – run the overdue process when the item return date is NOT today or after. The If condition tests for the opposite of what the script is actually interested in, and the real work happens in the Else branch.

As a general rule, write Boolean expressions so that True represents the condition you want to act upon. Doing so makes the code easier to read because the condition and the action naturally align. When someone scans the script, they can immediately see, “If the item is overdue, run the overdue process,” without having to mentally flip the logic.

That may seem like a small difference, but choosing positive Boolean tests consistently makes scripts easier to understand, maintain, and debug.

Does it exist?

A common example is when you need to do something but only if a certain piece of data exists. We will often use the IsEmpty function to test whether there is data.

Suppose you want to send an email but only if there is an email address. You could write:


If [ IsEmpty ( email ) ]
 # do not send email
Else
 Perform Script [ "Send email" ]
End If

This looks like a positive Boolean test. However it is testing for the opposite of what you want to be true to perform the action. So we can reverse the logic with the NOT operator:


If [ not IsEmpty ( email ) ]
 Perform Script [ "Send email" ]
Else
 # do not send email
End If

The important question here is, what is the script trying to determine? If this part of the script exists to send an email then the condition should answer "Is it okay to send the email?" If that is true, then send the email.

It is a pity that there is no native opposite function to IsEmpty. However it is easy to create a custom function such as Exists for this purpose.

Yes means go

Think of Boolean expressions as answering a yes/no question. And then consider what question the script is asking.

For example, if the question is "Is it OK to send an email?" then the Boolean expression needs to confirm all of the conditions under which an email can be sent. This leads to Boolean expressions with multiple tests such as:


not IsEmpty ( email )
 and
IsEmpty ( doNotContact )
 and
PreferredContactMethod = "email" 

This gets a little hard to interpret, as there are necessary double negatives in there. You can make it easier with a Let statement:


Let ([
  hasEmail = not IsEmpty ( email );
  canContact = IsEmpty ( doNotContact );
  prefersEmail = (PreferredContactMethod = "email") 
];
  hasEmail and canContact and prefersEmail
)

The result of the Let is more of a plain English sentence – this person has an email, and can be contacted, and prefers email.

Conclusion

A useful guideline is to ask yourself:

Does the statement test when the code is supposed to run?

If the answer is yes, you’ve probably written a positive Boolean test. If the answer is no, or you have to mentally interpret the Else, it’s worth seeing if the condition can be inverted so that the True branch contains the main work.


Are you coming to Reconnect.Christchurch, 15-16 October?
Get all the details and purchase tickets at Reconnect.Christchurch.

Are you late to the ScriptLogic party? If you have missed out on some of these emails, catch up here: Email Archive

If you know someone else who you think might like this email, you can send them this link to subscribe: Subscribe to ScriptLogic Daily