Challenge: Don't stop with the first DSL abstraction
I was having a discussion today about the way business rules are implemented. And large part of the discussion was focused on trying to get a specific behavior in a specific circumstance. As usual, I am going to use a totally different example, which might not be as brutal in its focus as the real one.
We have a set of business rules that relate to what is going to happen to a customer in certain situations. For example, we might have the following:
upon bounced_check or refused_credit: if customer.TotalPurchases > 10000: # preferred ask_authorizatin_for_more_credit else: call_the cops upon new_order: if customer.TotalPurchases > 10000: # preferred apply_discount 5.precentupon order_shipped:
send_marketing_stuff unless customer.RequestedNoSpam
What is this code crying for? Here is a hint, it is not the introduction of IsPreferred, although that would be welcome.
I am interested in hearing what you will have to say in this matter.
And as a total non sequitur, cockroaches at Starbucks, yuck.
Comments
let preferred_customer = customer.TotalPurchases > 10000 ?
Is there anyway to get your pre-release copy of your "Building DSL in Boo"? I'm completely lost in DSL world, and it extremely lacks of resources out there.
Is it crying for the 'why'?
Bob,
If you click on the book link on the side bar, you will go to a site where you can buy a PDF copy of the pre release version.
Joao,
No, I try no to do things twice.
Demis,
As I mentioned, this is a valuable part, but not by far the most important thing.
the unless part? :)
This way it's more uniform = easier for a parsing engine... or even a gui...
You saw cockroaches at Starbucks? Did you call the environmental services?
I'm no DSL man, but this seems a bit odd:
upon order_shipped:
Every other place you are using if, and suddenly you are using unless. Inkonsistence in my world.
But i miss order handling stuff in your DSL, everything is about a customer, but nothing about the order.
I have no experience with DSLs, but I sense something there I cannot quite wrap my brain around. Some things that I would toy around with:
1) bouncedcheck and refusedcredit could probably be combined into some sort of payment_failed object or state on the order.
2) I would really like to keep things focused on the order instead of both the customer and the order, like Benny mentioned. Perhaps setting a "doNotSendMarketingStuff" property on the order (this would also allow you to track the history of a customer's "nospam" setting, to a point).
Actually, I do not even like my second point. I am back to square one.
Maybe having a look at Specifications. by Eric Evans evans@acm.org and Martin Fowler fowler@acm.org
ordershippedspecification
neworderspecification
hascreditspecification
shouldbelockedupspecification
If number of rules grow, this DSL will be long as the river of amazon.
Try this:
askauthorizationformorecredit with preferred
upon bouncedcheck or refusedcredit:
callthecops without preferred
upon bouncedcheck or refusedcredit:
apply_discount 5.precent with preferred
upon new_order
sendmarketingstuff unless customer.RequestedNoSpam
upon order_shipped
Joseph,
Your approach is somewhat better, but still suffers from the same fundamental issue
Perhaps something roughly like this (excuse any bad syntax):
def: preferredcustomerstatus
paymentfailed = askauthorization_formorecredit
discount = 5.percent
end def
def: regularcustomerstatus
paymentfailed = callthe_cops
discount = 0.percent
end def
Then you would apply the rules as such:
if customer.TotalPurchases > 10000:
customerstatus = preferredcustomer_status
else:
customerstatus = regularcustomer_Status
upon bouncedcheck or refusedcredit:
customerstatus.paymentfailed
upon new_order:
applydiscount customerstatus.discount
upon order_shipped:
Gilligan,
I hate the syntax, but you got the right idea
How about the ability to vary the language to make it read more naturally?
This might mean adding synonyms.
Also, you could have "fluff" keywords that make it read better but which are ignored by the interpreter.
This could be a totally terrible idea, I'm just thinking out loud :)
Example:
when a bouncedcheck arrives or refusedcredit:
when a new_order arrives:
when order_shipped happens:
Synonyms
else => otherwise
Ignored Words (fluff keywords)
a
arrives
the
of
happens
That is just playing with the syntax, and not really changing the abstraction
From your reply to Gilligan, I reckon that you think this snippet should convey chunked concepts like "preferred customers get 5 percent discount and extra credit" and "regular customers don't get no discount and we set feds after 'em soon as they try to f*ck with us".
I'm not sure the users of this DSL will be able to grok the non-trivial concept of introducing new "thingies" into the DSL, and using them later. That's a very code-ish concept: declaration and usage.
See the next post to see how I think we can introduce it.
A scenario with actions based on this scenario is actually very easy to explain
C#: The ultimate DSL rules engine
It seems somewhat simple to split things up to make a fluent interface in C# that makes sense. But this is much more work.