About a month ago, there was a flurry of blogs and articles on what is, and what isn’t, a domain-specific language. I’m not sure who started it, but articles include Martin Fowler’s, Nutrun’s, and Jeremy Voorhis’. At the same time, I was having some interesting conversations about DSLs with Terence Parr, the author of ANTLR. So much for link-dropping.
I think there’s a question that’s more interesting than “what’s the lower bound for a DSL” (essentially the question that Martin is asking), or “is Rails a DSL?” (the question that Jeremy and Nutrun are asking). What makes a working language? Well, there are lots of components, but basically, you have a vocabulary and a grammar. Ruby DSLs are structurally very similar: they define a lot of methods (often via metaprogramming); they typically don’t define any new grammar, but use Ruby’s relatively loose and flexible grammar. So what looks like a nice domain-specific language that requires minimal knowledge of a formal programming language is, in fact, just a bunch of method calls in a formal programming language. The method names are the vocabulary, and the grammar is Ruby. Jeremy calls this a “fluent interface” (and notes that it’s often just good design).
Creating a really good fluent interface is a neat trick: it looks like you’re getting a new language “for free” (almost). You could argue that these fluent interfaces aren’t really DSLs–they’re really just Ruby. But that’s not a very useful or interesting argument. Here’s a more interesting question, which I haven’t seen discussed: what’s the point at which it becomes necessary for a DSL to have a distinct grammar? (I don’t think it was discussed at the DSL session at our FOO conference, which I missed). What’s the point at which the control structures, etc., in Ruby (or whatever the parent language) are no longer sufficient or adequate for the domain? What’s the point where you have to go beyond creating classes and metaprogramming, and generate your own parser with the venerable YACC, or a successor like ANTLR?
After all, it doesn’t matter whether you call it Ruby or Java or Python; most programming languages have fairly similar control structures (if/else, for, while, unless), and in most OO languages, the class system is fairly similar. That works well for computing–but what about other domains? When does the grammar of a traditional programming language, even one as flexible as Ruby, become inadequate?
Whether or not Rails is a DSL, it’s not terribly surprising that it doesn’t require any new grammar. There’s certainly an impedance mismatch between OO languages and relational databases, but it’s not a huge one: Web interfaces, OO languages, and RDBMSs are all things designed by us computer folk, and they’re more similar than different. Could you use a language like Ruby to model business strategies, musical compositions, and other domains where the impedance mismatch is greater? Or would you need a different grammar entirely?