Saturday, September 22, 2007

Grokking Domain Specific Languages

A Domain Specific Language (DSL) is a programming language that is designed for a specific task. Although this definition is overly simplistic, it is good place to start.

As a Java developer I use domain specific languages in every non-trivial project that I am involved in. I use Hibernate, which uses an XML configuration file to map Java properties to data in a database. This XML file is a DSL. This also goes for DBUnit dataset files, Spring bean definitions, and even HTML. If might not be natural to think of a "configuration file" as a DSL, but it is.

For a really good discussion on DSL's and related concepts you should watch Language-oriented Programming and Language Workbenches, a presentation given by Neal Ford and Martin Fowler.

In watching the presentation I learned that DSL's don't always mean that you need to create a new language with a new syntax. Let me show you am example so that you can see what I mean.

The code below is a testing "language" of my own concoction that you would use in a JUnit test. It allows you to easily spin up a Spring config, prepopulate the database with DBUnit, then test the output of a Spring controller.


WebRequest req = createRequest()
.forURL("/app/login")
.usingPostMethod()
.withParams("user", "rhanson". "pass", "r@ckin")
.withSessionAttributes("user", null);

executeSpringController(
"loginController",
usingSpringConfig("applicationContext.xml"),
usingDBUnitData("dataset.xml"),
usingRequest(req)
)
.assertThatViewEquals("loginSuccess");
.assertThatSessionAttribute("user", isNotNull());
.assertThatSessionAttribute("user.name", equalTo("rhanson"));


I'll bet that without providing any documentation for the API that you can easily tell what the test is doing. In the presentation I noted, martin Fowler states that readability is one of the cornerstones of a DSL, and I think this one passes the test.

My DSL is written in Java, I didn't create my own programming language for my DSL. This is beneficial because it makes it easy for any Java developer to understand my DSL, and IDE's like Eclipse will be able to provide code completion, making it even easier to use.

DSL's make a lot of sense for simplifying tasks, in turn reducing the cost of a project.

Some references:

Thursday, September 20, 2007

Tail Chasing

I hope that I am not the only one with this problem, but I often find myself trying too many different things. By "things" I of course mean technologies. Things like Wicket, Grails, jQuery, and Silverlight. So many cool things to learn, and only so many hours in the day. It is enough to make my head spin.

I have been trying to come up with a strategy that will allow me to learn more, but with little success. I toyed with the idea of quitting my job to maximize my learning time, but my wife reminded me that we still need to pay the bills. With that plan scrapped I thought that maybe I could get rid of my wife, thus reducing the bills that need to get paid. That wasn't a great idea either for various reasons, including the fact that I can't take care of myself.

So what's a tech junkie to do?

This has led to some deep thinking about the state of our craft. After some thought I have summarized it as "Tail Chasing". Let me see if I can explain.

Let's say that you are a developer, and you have been spending the past year or so really getting to know a given technology. Now you are being told that the technology you are using is inferior to this "other" technology. You take a look and realize that it might be best to switch. A year later you finally have a good understanding of the tool, and use it with great skill. Then someone tells you about this "other" technology.

How many of us built our own MVC frameworks only to move to Struts, then maybe on to Spring MVC. Sure, there are some improvements made in each technological step, but since you are spending most of your time really getting to know a product you often spend little time getting the most out of it. This is compounded by the fact that you often use several of these products at the same time, adding to what you need to learn.

So what is a dog to do? Although you are moving forward, you never quite catch the tail. Should you just stop moving forward, or run faster or slower?