DSL Styles - Imperative vs. Declarative

time to read 2 min | 331 words

I posted yesterday about building a rule engine using a DSL. After I wrote that, I tried to think about what style it was. That made me realize that I had no clear distinction between imperative and declarative DSL. At least not a coherent, internally consistent one.

I asked the Alt.net mailing list, and a lively discussion enthused. Contrary to my opinion, a lot of people disagreed with my opinion that this is a declarative style DSL:

when User.IsPreferred and Order.TotalCost > 1000:
    addDiscountPrecentage  5
     applyFreeShipping
when not User.IsPreferred and Order.TotalCost > 1000:
suggestUpgradeToPreferred
     applyFreeShipping
when User.IsNotPreferred and Order.TotalCost > 500:
     applyFreeShipping

It looks too much like code apperantely. After some debate, we now have the following candidates for declarative DSL:

From Chad:

about User
       when Preferred and OrderTotal > 1000:
               Discount is 0.05
               And Shipping is FREE;
       when not Preferred and OrderTotal > 1000:
               Upgrade Suggested
               And Shipping is FREE;
       when not Preferred and OrderTotal > 500:
               Shipping is FREE;

Based on Berry's ideas:

applyDiscount 5.precent:
    when User.IsPreferred and Order.TotalCost > 1000
suggestPreferred:
     when not User.IsPreferred and Order.TotalCost > 1000
freeShipping:
     when Order.TotalCost > 500

The really interesting thing to me, so far, is that they are all functionality equivalents. From the point of view of the implementer, there isn't much difference at all betwee them.

My own, loosely defined and likely wrong definition was based on what the DSL actually did. If the DSL performed all the work internally, I would call it imperative. If the DSL performed some work that produced an object graph, which another part of the system would tehn consume, I would call it declarative.

Thoughts?