What is a DSL, anyway? Why should you care?
DSL stands for Domain Specific Language, and it essentially means a purpose built language designed for a specific task: HTML, Unix scripting, and SQL are three you’re almost certainly familiar with. Contrast this with a a general purpose language, like Java, or for that matter, Groovy, where the language is open ended, and intended to deal with almost any task. So far, simple enough, and this is all dealt with very adequately in the Wikipedia article on the topic.
But here’s the thing: When you’re creating a DSL with Groovy (or, for that matter, with many metaprogramming languages), you’re not generally creating a limited language – you’re creating a domain aware extension to the existing general purpose language you’re using as a base – unless, of course, you do a great deal of additional work to disable the existing language features.
So, what to call such a beast? What to call a domain aware general purpose language? I’ve noticed that most people just call it a DSL, so that’s the convention I’ll follow in the following posts. Computer science purists, please forgive me.
Why should you care about DSLs? Why learn how to create one? Well, I can only answer for me, but I can tell you that when I was a still a wee little programmer, the people who created the languages I used were people I really looked up to, and the idea of creating one of my own, or even just knowing how, is something that’s really exciting to me. On a more practical side, if you have this in your toolbox, ways to use it present themselves all the time. Want to create a really usable, readable testing framework around your Selenium tests? A DSL may fit the bill. Need to present a text heavy interface to your users for open-ended data entry? A simple object definition language DSL seems appropriate. And so on. Like all techniques, it can be overused (factories, anyone?), but DSLs have their place. Plus, it’s just so darn cool.
In the following series of posts, I’ll go over the basics of creating a DSL. No Groovy knowledge will be required or assumed – just Java. Follow along – I think you’ll like it.