Ayende @ Rahien

It's a girl

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.precent
upon 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

Demis
09/04/2008 12:41 AM by
Demis

let preferred_customer = customer.TotalPurchases > 10000 ?

Bob
09/04/2008 03:09 AM by
Bob

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.

Ayende Rahien
09/04/2008 04:47 AM by
Ayende Rahien

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.

Ayende Rahien
09/04/2008 04:49 AM by
Ayende Rahien

Joao,

No, I try no to do things twice.

Ayende Rahien
09/04/2008 04:51 AM by
Ayende Rahien

Demis,

As I mentioned, this is a valuable part, but not by far the most important thing.

firefly
09/04/2008 06:39 AM by
firefly

the unless part? :)

upon bounced_check or refused_credit:

        ask_authorizatin_for_more_credit unless IsNotPrefered

        call_the cops unless Prefered


upon new_order:

        apply_discount 5.precent unless IsNotPrefered


upon order_shipped:

     send_marketing_stuff unless customer.RequestedNoSpam

This way it's more uniform = easier for a parsing engine... or even a gui...

John
09/04/2008 09:37 AM by
John

You saw cockroaches at Starbucks? Did you call the environmental services?

Benny Thomas
09/04/2008 10:51 AM by
Benny Thomas

I'm no DSL man, but this seems a bit odd:

upon order_shipped:

send_marketing_stuff unless customer.RequestedNoSpam

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.

Gilligan
09/04/2008 01:03 PM by
Gilligan

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.

James
09/04/2008 01:19 PM by
James

Maybe having a look at Specifications. by Eric Evans evans@acm.org and Martin Fowler fowler@acm.org

ordershippedspecification

neworderspecification

hascreditspecification

shouldbelockedupspecification

Bunter
09/04/2008 01:27 PM by
Bunter

If number of rules grow, this DSL will be long as the river of amazon.

Joseph Gutierrez
09/04/2008 02:32 PM by
Joseph Gutierrez

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

Ayende Rahien
09/04/2008 02:38 PM by
Ayende Rahien

Joseph,

Your approach is somewhat better, but still suffers from the same fundamental issue

Gilligan
09/04/2008 03:48 PM by
Gilligan

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:

 send_marketing_stuff unless customer.RequestedNoSpam
Ayende Rahien
09/04/2008 03:49 PM by
Ayende Rahien

Gilligan,

I hate the syntax, but you got the right idea

Tobin Harris
09/04/2008 04:32 PM by
Tobin Harris

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:

if customer.TotalPurchases > 10000: # preferred

    ask_authorizatin_for_more_credit

otherwise

    call_the cops

when a new_order arrives:

if customer.TotalPurchases > 10000: # preferred

    apply_discount of 5.precent

when order_shipped happens:

 send_marketing_stuff unless the customer.RequestedNoSpam

Synonyms

else => otherwise

Ignored Words (fluff keywords)

a

arrives

the

of

happens

Ayende Rahien
09/04/2008 04:34 PM by
Ayende Rahien

That is just playing with the syntax, and not really changing the abstraction

Avish
09/04/2008 04:45 PM by
Avish

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.

Ayende Rahien
09/04/2008 05:02 PM by
Ayende Rahien

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

pb
09/05/2008 01:56 AM by
pb

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.

Comments have been closed on this topic.