Saturday, February 9, 2008

Gateway JUG - Feb 2008: Buildling DSLs

Despite the crazy weather and torrential downpour of rain, I was able to make it to the Gateway JUG meeting on Monday night. The talk wasn't what I was expecting and was very interesting. I knew the talk was on DSLs (Domain Specific Languages) but I was expecting the talk to be about how a developer can write applications that present DSLs to the end user, allowing them to describe business processes and what they want in a manor much more akin to their business training and actual representations. Neal, instead, focused on approaches developers can use to model DSLs and domain specific information in static and dynamic general purpose languages where the developer is the primary beneficiary. I liked his focus better.

The presentation outlined how we as developers can design our APIs and usage patterns in such a way that the code shifts from being a hodgepodge of declarative and imperative code, to primarily declarative. A coworker of mine pointed out that this can be done with good refactoring and encapsulation, which is true, but Neal's presentation took this even further. His examples started with business rules/processes that we needed to model in code, wrote the code in the way we wanted to express it (target syntax), and then modeled the API around that. The difference sounds subtle but resulted in code that expressed the true desire of the business rule with a minimum of extra syntax noise.

In doing so covered some good design patterns which he said were named after those that Martin Fowler is going to be using in an upcoming book he is working on all about DSL patterns. Also, he demonstrated how some of the syntactical and dynamic features of languages like Groovy and Rube allow DSLs to be composed much more fluently and expressively. The fact that you can boil down your code to something that looks like this in Groove/Ruby is pretty nice:

def recipe = new Recipe("Smoky Flour")
recipe.add 1.pound.of("Flour")
recipe.add 2.grams.of("Nutmeg")
assertThat 2, is(recipe.ingredients.size)
[chunk of code taken from Neal Ford's presentation, written in Groovy]
All in all I thought it was a great talk and gave me some ideas on how I can remodel my code to be much more exacting which will allow me to write the code to match the requirements a ton easier and allow me to test specific business rules without a ton of extra junk to get in the way of what I am really doing.

If none of that made sense, it may not have, check out the presentation here. The PDF handout for his presentation at CodeMash is the same as far as I can tell

No comments: