By Peter Bell

Why Use a Domain Specific Language - IMPORTANT

Enough with the deep theory - what is a DSL and how can it help me to write better programs with less code?

If you want to get anything else about code generation, you’re going to want to re-read this article a couple of times until it makes sense. This is THE core concept from which everything else flows . . .

Firstly, if you’ve ever created a custom tag, utility library or a CFC, you’ve already written a DSL. A domain specific language is a limited language to allow you to solve a subset of problems more concisely than using a general purpose programming language.

So, if you write a UserDAO.cfc with methods for get(), insert(), update() and delete() then you have created a very simple DSL that supports four commands each of which has its own syntax. Now you can just write get(37) and it’ll take care of getting user where UserID=37. Compare that to the syntax to CFquery the db and you’ll see that get(37) is much more concise, expressive and readable. It is also much more limited – you can’t decide what fields to get, inner joins are not allowed, and if you want to get a user based on their email address you’re out of luck.

So, domain specific languages allow us to trade unnecessary flexibility for conciseness and readability. APIs and method calls could be considered as a rudimentary form of in language DSL for languages that don’t support more syntactically elegant self-extension mechanisms (Ruby IS cool in some ways).

But *That* Isn’t a DSL . . .
At their core DSLs are just an abstract grammar that allows you to describe a specific subset of problems more concisely that fully describing them in a general purpose language. Method signatures, config files, custom tag interfaces, UML class diagrams, simple “mini languages” parsed using yacc, meaningful directory structure conventions, database schemas – these are all domain specific languages.

Why Use?
Why do you create a custom tag or an object with methods? Elegant code reuse. Now instead of having to recode the algorithms every time you need them, you just write a phrase in your DSL (usually a custom tag or method call) and you end up with shorter, more easily maintainable programs.

A World of Distinctions
Of course, if you sit down with a proponent of Model Driven Architecture (or as I like to call it, programming in pictures :->) and tell them all they are doing is creating an API, they probably won’t like it. That doesn’t mean it isn’t true. The only difference is that instead of being tied to the concrete syntax of API calls in a given language, they are tied to the concrete syntax of boxes and arrows on a diagram.

There are in language DSLs, external DSLs, DSLs can be interpreted or compiled (through a generation step), but I’m with Eric Evans - the fundamental nature of programming is designing languages that elegantly capture requirements and make your code more concise, comprehensible and maintainable.

Conclusions
Start to look at programming as language design. I’ll be posting a number of pieces over the next few weeks on tips and tricks for dealing with different concrete syntaxes and how to generate code using DSLs. I’ll also be spending some time talking about one of the biggest issues that still needs to be fully solved in the world of application generation – creating generators using an agile approach.

Comments
An analogy:

Java = general purpose programming language
Coldfusion = DSL for web development
# Posted By Sam | 1/18/07 10:25 AM
Hi Sam,

True. Obviously domain specificity is a a continuum more than a binary function. I tend to look at CF as a general purpose scripting language as it has a fairly wide range of imperative constructs whereas the majority of DSLs (not all, but most) can be described declaritively. But I guess compared to a general purpose programming language, you could see CF as a fairly general purpose DSL for web dev.

For me, tighter examples of DSLs would be things like Model Glue and ColdSpring XML files.
# Posted By Peter Bell | 1/18/07 10:32 AM
Well, clearly web development is still a very broad domain. Based on what I read here on Application Generation though, I do get a feeling you're talking about much more well defined domains. I'm thinking something along the lines of application specific. You might have an ecommerce DSL, for example, or a contact manager DSL. They might even overlap.

Of course, I'm just speculating, since I don't recall you ever coming out and actually saying something along those lines.
# Posted By Sam | 1/18/07 11:52 AM
I find most DSLs vertical or horizontal, and even more specific than that.

A vertical DSL might relate to shipping rules or handling pricing.

A horizontal would be workflow, screens, object definitions, validations, transformations, etc.
# Posted By Peter Bell | 1/18/07 12:24 PM
So, even more minute than what I was proposing?
# Posted By Sam | 1/18/07 1:42 PM
Yeah - the more constrained the language, the more elegantly you can describe the domain knowledge. Different domains need different levels of expressiveness, but "as constrained as possible but no more" is the guideline.
# Posted By Peter Bell | 1/18/07 1:49 PM
OK Peter, cheers for that, bit clearer now. The comments above between you and Sam also add a bit more meaning to it all for me. As usual though, you've given me loads more to go and read up on! Sometimes you're a harder task master than my boss! I'll be back... (er sorry for that)

Cheers
# Posted By dc | 1/18/07 2:10 PM
Glad it helped a little. I'll try to keep posting and clarifying!
# Posted By Peter Bell | 1/18/07 2:22 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.