Much of the wisdom of software development is expressed as wry—nay, even cynical—aphorisms. My favourite (attributed to Hamming) is: “Mathematicians stand on each other’s shoulders while computer scientists stand on each other’s toes.” That one is merely descriptive, but the most important ones are prescriptive, like this one from Knuth:
“Premature optimization is the root of all evil”
Programmers obsess over “optimization.” Mostly they mean they want their programs to run faster, because everybody hates waiting. In case you haven’t noticed, business rules ARE little programs, and as my gift to you, I’m going to give you 3 tips that could make your business rules run faster.
IF vs. SELECTCASE
Let’s say you’ve got a rule that has three possible outputs:
1. If brand is “Acme,” output “Free Overnight Shipping”
2. If brand is “X,” output “Free Shipping”
3. Otherwise, output “See Site for Shipping Cost”
I usually advise people to use SELECTCASE when dealing with more than one condition, like this:
SELECTCASE($brand=”Acme”,”Free Overnight Shipping”,$brand=”X”,”Free Shipping”,”See Site for Shipping Cost”)
I think this looks cleaner than the alternative, using nested IFs:
IF($brand=”Acme”,”Free Overnight Shipping”,IF($brand=”X”,”Free Shipping”,”See Site for Shipping Cost”))
These two rules have exactly the same output, and I assumed they took about the same time to run. I was surprised to find out that the 2nd rule is TEN TIMES faster! Here’s the fundamental truth of “optimization:” it’s always a trade-off. In this case you are trading clarity (first rule) for speed (2nd rule). Choose wisely!
IFBLANK vs ISINLIST
Here’s a pattern I’ve seen a few times:
IF(ISINLIST(“Some list”,$brand),LOOKUP(“Some list”,$brand),”Default text”)
This rule checks to see if brand (it could be any attribute, of course) is the left-hand or “name” column of a list. If so, the rule outputs the corresponding data from the right-hand or “value” column. Otherwise, it outputs some default text. This is perfectly fine. However, if ALL the entries in the list have a non-blank “value” column, the rule could be this:
IFBLANK(LOOKUP(“Some list”,$brand),”Default text”)
The second rule is twice as fast! The reason is that the first rule ALWAYS looks up the value in the list TWICE, whereas the second rule only does it once. Now, again, there’s a trade-off: if your list has blank entries in the “value” column, the rules are NOT the same.
Regular Expressions
Ah, who doesn’t love a regex? (By the way, here’s another aphorism “Some people, when confronted with a problem, think ‘I know, I’ll use regular expressions.’ Now they have two problems.’) However many problems you wind up with, you’ll have this one at least: regexes can be SLOW. And given two regexes that do more-or-less the same thing, one could be MUCH slower than the other. For example, here are two rules that remove html tags from a description:
REGEXREPLACE($description,”<.+?>”,””)
REGEXREPLACE($description,”<[^>]+>”,””)
I won’t go into the details of these; instead I will note that the second one is faster by about 50%. Be very careful with your regexes. Most of the tradeoff is in the time it takes you to write the thing; sometimes you have to be pretty clever.
There you go friends: 3 tips for faster rules. With all the time you save, you might be able to watch all 35 bowl games this year.
Blog post by Anthony Alford, (a.k.a. The Feed Doctor), Technical Lead