ChallengeDon'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.
More posts in "Challenge" series:
- (01 Jul 2024) Efficient snapshotable state
- (13 Oct 2023) Fastest node selection metastable error state–answer
- (12 Oct 2023) Fastest node selection metastable error state
- (19 Sep 2023) Spot the bug
- (04 Jan 2023) what does this code print?
- (14 Dec 2022) What does this code print?
- (01 Jul 2022) Find the stack smash bug… – answer
- (30 Jun 2022) Find the stack smash bug…
- (03 Jun 2022) Spot the data corruption
- (06 May 2022) Spot the optimization–solution
- (05 May 2022) Spot the optimization
- (06 Apr 2022) Why is this code broken?
- (16 Dec 2021) Find the slow down–answer
- (15 Dec 2021) Find the slow down
- (03 Nov 2021) The code review bug that gives me nightmares–The fix
- (02 Nov 2021) The code review bug that gives me nightmares–the issue
- (01 Nov 2021) The code review bug that gives me nightmares
- (16 Jun 2021) Detecting livelihood in a distributed cluster
- (21 Apr 2020) Generate matching shard id–answer
- (20 Apr 2020) Generate matching shard id
- (02 Jan 2020) Spot the bug in the stream
- (28 Sep 2018) The loop that leaks–Answer
- (27 Sep 2018) The loop that leaks
- (03 Apr 2018) The invisible concurrency bug–Answer
- (02 Apr 2018) The invisible concurrency bug
- (31 Jan 2018) Find the bug in the fix–answer
- (30 Jan 2018) Find the bug in the fix
- (19 Jan 2017) What does this code do?
- (26 Jul 2016) The race condition in the TCP stack, answer
- (25 Jul 2016) The race condition in the TCP stack
- (28 Apr 2015) What is the meaning of this change?
- (26 Sep 2013) Spot the bug
- (27 May 2013) The problem of locking down tasks…
- (17 Oct 2011) Minimum number of round trips
- (23 Aug 2011) Recent Comments with Future Posts
- (02 Aug 2011) Modifying execution approaches
- (29 Apr 2011) Stop the leaks
- (23 Dec 2010) This code should never hit production
- (17 Dec 2010) Your own ThreadLocal
- (03 Dec 2010) Querying relative information with RavenDB
- (29 Jun 2010) Find the bug
- (23 Jun 2010) Dynamically dynamic
- (28 Apr 2010) What killed the application?
- (19 Mar 2010) What does this code do?
- (04 Mar 2010) Robust enumeration over external code
- (16 Feb 2010) Premature optimization, and all of that…
- (12 Feb 2010) Efficient querying
- (10 Feb 2010) Find the resource leak
- (21 Oct 2009) Can you spot the bug?
- (18 Oct 2009) Why is this wrong?
- (17 Oct 2009) Write the check in comment
- (15 Sep 2009) NH Prof Exporting Reports
- (02 Sep 2009) The lazy loaded inheritance many to one association OR/M conundrum
- (01 Sep 2009) Why isn’t select broken?
- (06 Aug 2009) Find the bug fixes
- (26 May 2009) Find the bug
- (14 May 2009) multi threaded test failure
- (11 May 2009) The regex that doesn’t match
- (24 Mar 2009) probability based selection
- (13 Mar 2009) C# Rewriting
- (18 Feb 2009) write a self extracting program
- (04 Sep 2008) Don't stop with the first DSL abstraction
- (02 Aug 2008) What is the problem?
- (28 Jul 2008) What does this code do?
- (26 Jul 2008) Find the bug fix
- (05 Jul 2008) Find the deadlock
- (03 Jul 2008) Find the bug
- (02 Jul 2008) What is wrong with this code
- (05 Jun 2008) why did the tests fail?
- (27 May 2008) Striving for better syntax
- (13 Apr 2008) calling generics without the generic type
- (12 Apr 2008) The directory tree
- (24 Mar 2008) Find the version
- (21 Jan 2008) Strongly typing weakly typed code
- (28 Jun 2007) Windsor Null Object Dependency Facility
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) bounced_check and refused_credit 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
order_shipped_specification
new_order_specification
has_credit_specification
should_be_locked_up_specification
If number of rules grow, this DSL will be long as the river of amazon.
Try this:
ask_authorization_for_more_credit with preferred
upon bounced_check or refused_credit:
call_the_cops without preferred
upon bounced_check or refused_credit:
apply_discount 5.precent with preferred
upon new_order
send_marketing_stuff 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: preferred_customer_status
payment_failed = ask_authorization_for_more_credit
discount = 5.percent
end def
def: regular_customer_status
payment_failed = call_the_cops
discount = 0.percent
end def
Then you would apply the rules as such:
if customer.TotalPurchases > 10000:
customer_status = preferred_customer_status
else:
customer_status = regular_customer_Status
upon bounced_check or refused_credit:
customer_status.payment_failed
upon new_order:
apply_discount customer_status.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 bounced_check arrives or refused_credit:
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.
Comment preview