Skip to main content

How I Learned to Love Domain-Specific Languages (in Three Parts)

Posted by javaben on November 16, 2005 at 8:14 PM PST

(cross-posted on Married... with children)

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 })*
        }
    }+
}

I think the XML version is clearly 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.)

Related Topics >>