I’ve been watching the hype surrounding domain-specific languages (DSLs) with skepticism. At first I thought, “Why would I learn some custom syntax when I could use good old Java and XML?” And then, gradually, I saw the light.
Dave Thomas was the catalyst behind my change of heart. Over dinner a few years ago, he patiently explained to me why the Ruby community eschewed XML for YAML; that angle brackets and wearisome complexity make XML more pain than it’s worth.
At the time, I sputtered various protests about how XML was a lingua franca, readily-understood, blah blah blah. But it got me thinking — and the next time I edited an Ant build file, my discontent was a bit more pronounced than usual.
Shortly thereafter, James Duncan Davidson came out with his “I shudda used a scripting language” post (couldn’t find a good link, sorry, but this fragment gives you an idea), and that got me thinking some more.
As the Rails machine started dominating the Ruby discussions, Dave’s anti-XML chorus found a whole new host of supporters, this time advocating in addition to YAML the use of the value of domain-specific languages instead of general-purpose configuration files. About the same time, my friend Neal Ford started banging his own domain-specific language (a.k.a. language-oriented programming) drum.
“Hey,” I started thinking, “maybe there’s something to this ‘XML bad, DSL good’ meme.”
The funny thing was that all throughout this time period, though I didn’t realize it, I was already preferring a DSL to XML. RELAX NG (a blissfully simple alternative to W3C XML Schema) comes in both XML and DSL flavors, and I’d been preferring the DSL for months. Have a look, starting with the XML version:
<?xml version="1.0"?> <element name="contents" xmlns="http://relaxng.org/ns/structure/1.0" ns="http://galbraiths.org/myns"> <oneOrMore> <element name="section"> <attribute name="name"/> <element name="content"> <zeroOrMore> <choice> <text /> <element name="p"> <empty /> </element> </choice> </zeroOrMore> </element> </element> </oneOrMore> </element>
That doesn’t look so bad — and since it’s RELAX NG, and not W3C XML Schema, it’s actually somewhat intuitive.
Now, check out the DSL:
default namespace = "http://galbraiths.org/myns" element contents { element section { attribute name { text }, element content { (text | element p { empty })* } }+ }
Of course, the XML version would be more intuitive for those with no previous exposure to RELAX NG, but for those who spend a lot of time working in the domain, my own experience (and anecdotal evidence) shows that the DSL is the clear favorite. By the way, the RELAX NG “DSL” is known as the “Compact Syntax”. Until recently, I never thought of it as a DSL, but, it sure is.
So gosh, I guess I prefer domain-specific languages to general-purpose XML, too.
(I’ll talk a bit about creating and using DSLs in Part 2 of this blog entry, and in Part 3 I’ll give some examples of how Java’s Swing GUI toolkit can benefit from a DSL or two.)
I got all spun up on DSL’s when the JetBrain’s CEO started spewing forth his ideas about how great it would be and how he could make it work. He wanted to create a way to create new languages, I was hoping that it would make putting languages like Ruby into IntelliJ IDEA easy and provide the same user experience that you get with Java. Unfortunately, this hasn’t turned out to be the case and Eclipse is winning this game by simply having a good plugin architecture… oh yeah, we are supposed to be talking about DSL’s…
I don’t know RELAXNG, but I understood the compact syntax quicker than the XML syntax. I wonder if I would have understood the compact syntax as quickly if I had read it first.
I am looking forward to your next two posts…
Umm Ben, when will parts 2 and 3 be published? Now that you’ve resurrected this blog, I’m looking forward to the completion of this series 😉
Sean: I know, I know. Soon, my friend!
Ben, I’m still waiting for the next two posts.