<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ThotSpots &#187; DSL</title>
	<atom:link href="http://www.thotspots.com/tag/dsl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thotspots.com</link>
	<description>Agile Software Development</description>
	<lastBuildDate>Wed, 09 Sep 2009 18:13:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Demystifying Domain-Specific-Languages (DSL)</title>
		<link>http://www.thotspots.com/demystifying-domain-specific-languages-dsl/</link>
		<comments>http://www.thotspots.com/demystifying-domain-specific-languages-dsl/#comments</comments>
		<pubDate>Mon, 04 May 2009 01:45:47 +0000</pubDate>
		<dc:creator>Craig Jones</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>

		<guid isPermaLink="false">http://www.thotspots.com/?p=194</guid>
		<description><![CDATA[Groovy supports creating Domain Specific Languages.  A DSL is simply a mini programming language tailored to a specific situation or domain.  The idea is to hide the characteristics of the underlying programming language as much as possible (in this case, Groovy/Java), and let the vocabulary of the application domain shine through.
DSLs are nothing [...]]]></description>
			<content:encoded><![CDATA[<p>Groovy supports creating Domain Specific Languages.  A DSL is simply a mini programming language tailored to a specific situation or domain.  The idea is to hide the characteristics of the underlying programming language as much as possible (in this case, Groovy/Java), and let the vocabulary of the application domain shine through.</p>
<p>DSLs are nothing new.  Just by encapsulating the concepts of a domain in classes (&#8221;domain objects&#8221;) and by defining the methods on those classes that act on them (&#8221;actions&#8221; and &#8220;messages&#8221;), you create a DSL of sorts.  You see this a lot in unit testing.  As a body of unit tests evolves, common SetUp and TearDown code is often extracted to avoid duplication.  As the extracted classes and methods grow and evolve, a DSL emerges. For an excellent example of this, see <a href="http://www.amazon.com/gp/product/0132350882?ie=UTF8&amp;tag=thotspots-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0132350882">Clean Code: A Handbook of Agile Software Craftsmanship</a><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=thotspots-20&amp;l=as2&amp;o=1&amp;a=0132350882" border="0" alt="" width="1" height="1" /> by Robert Martin.</p>
<p>Groovy accomplishes the creation of DSLs in a number of ways:<br />
<span id="more-194"></span></p>
<ol>
<li><strong>Optional Syntax:</strong> In Groovy, much of the syntax is optional:  semicolons at the end of statements when they are the only statement on the line, parenthesis around arguments in a method call, etc.  Dropping away such &#8220;noise&#8221; is one way that the user can then focus on the domain at hand.</li>
<li><strong>Missing-Method Methods:</strong> In Java, if you call a method that does not exist, you get a compiler error.  In Groovy, it waits until runtime to decide if it&#8217;s a problem, giving the class with the missing method a chance to make up a method on the spot.  This is done with a special &#8220;missing-method method.&#8221;  For example, this is how GORM allows you to make up method calls like findByPersonAndJobTitle().  There&#8217;s a missing-method method that knows how to parse out the name, &#8220;findByPersonAndJobTitle&#8221;  see that it&#8217;s comprised of pieces it understands (a find action and two field names) and thereby construct the logic to perform that action.</li>
<li><strong>Operator overloading:</strong> Groovy allows you to define methods that correspond to the various operators, like plus() for the + sign.  This means your DSL could allow for expression like &#8220;car = chassis + 4 * tires&#8221; and &#8220;truck = (cab + 6 * tires) + (trailer + 4 * tires) + (trolley + 4 * tires) + (trailer + 4 * tires)&#8221;</li>
<li><strong>Named Parameters:</strong> Grails has the ability to emulate named-parameters (which means that having to know the order in which to pass parameters is no longer a concern).  This is done by defining the method to accept a single map as a parameter.  Then, in the call, a map is created on the fly with hard-coded keys that look like parameter names, and expressions that are the corresponding arguments.  Leave out the optional square brackets that normally enclose a map constant, and wallah! Named parameters:</li>
<li><strong>Closures:</strong> Groovy&#8217;s strong support for Closures means it&#8217;s easy to define custom control structures</li>
<li><strong>Categories, MetaClasses, and GroovyObject:</strong> Without going into details, these are some of the core facilities in Groovy that make it a dynamic &#8220;scripting&#8221; language and so suitable for expressing DSLs</li>
</ol>
<p><strong>Builders:</strong><br />
The most common use of DSLs in Grails is the idiomatic &#8220;builder.&#8221;  This is for when there is a distinct hierarchy, or tree-structure to the elements of the domain.  There are pre-defined builders for Swing, XML, HTTP, Hibernate Criteria, HTML, ANT, and the Constraints builder in Grails that drives the validation mechanism, just to name a few.  It&#8217;s easy to create your own builders as well.</p>
<p>With the groovy.xml.MarkupBuilder(), for example, instead of having to write your own XML&#8230;</p>
<pre>&lt;stocks&gt;
  &lt;stock symbol='JAVA'&gt;
    &lt;quote day='Mon' price="53.125" /&gt;
    &lt;quote day='Tue' price="54.5" /&gt;
    &lt;quote day='Wed' price="51.75" /&gt;
  &lt;/stock&gt;
  &lt;stock symbol='MSFT' /&gt;
  &lt;stock symbol='IBM' /&gt;
&lt;/stocks&gt;</pre>
<p>You can express the information like this:</p>
<pre>builder.stocks {
    stock(symbol: 'JAVA') {
        quote(day: 'Mon', price: 53.125)
        quote(day: 'Tue', price: 54.5)
        quote(day: 'Wed', price: 51.75)
    }
    stock(symbol: 'MSFT')
    stock(symbol: 'IBM' )
}</pre>
<p>At first glance, it might seem like you&#8217;re just trading one arbitrary syntax for another: parenthesis, braces, colons and commas, rather than angle-brackets, equal signs, and  matched-up start and end tags.  However, this is merely a dumbed-down example.  The benefits of using a builder are enormous:</p>
<p>For one thing, the Builder syntax is universal.  Once you know who to write XML markup using Builder syntax, you also know how to write HTML markup, Swing forms, Grails criteria, and all of the other pre-defined builders mentioned above, and more .</p>
<p>Also, since Builder syntax is built on top of Groovy, Groovy code can be intermixed with the DSL code.  For example, we can use each-loops to build the XML markup from a &#8220;tickerInfo&#8221; object:</p>
<pre>builder.stocks {
    tickerInfo.each {ticker -&gt;
        stock(symbol: ticker.symbol) {
            ticker.dailyQuotes.each{
                quote(day: it.day, price: it.price)
            }
        }
    }
}</pre>
<p>DSLs are a key reason for the success of Grails.  Taking advantage of Groovy&#8217;s DSL support is a prime example of how much thought has gone into the architecture Grails with it&#8217;s prime directive to be as idiomatic-Grails as possible.</p>
<p>We&#8217;ve only just scratched the surface of what DSLs are and how they are supported in Groovy.  Here is some recommended further reading:</p>
<ul>
<li><a href="http://docs.codehaus.org/display/GROOVY/Writing+Domain-Specific+Languages" target="_blank">docs.codehaus.org/display/GROOVY/Writing+Domain-Specific+Languages</a></li>
<li><a href="http://docs.codehaus.org/display/GROOVY/How+Builders+Work" target="_blank">How Builders Work</a></li>
<li><a href="http://www.amazon.com/gp/product/0132350882?ie=UTF8&amp;tag=thotspots-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0132350882">Clean Code: A Handbook of Agile Software Craftsmanship</a><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=thotspots-20&amp;l=as2&amp;o=1&amp;a=0132350882" border="0" alt="" width="1" height="1" /> by Robert Martin</li>
</ul>
<p><strong>Related articles:</strong></p>
<ul>
<li><a href="http://www.thotspots.com/clean-code-book/" target="_blank">A brief review of <em>Clean Code</em></a></li>
<li><a href="http://www.thotspots.com/everythings-groovy/" target="_blank">Everything&#8217;s Groovy</a></li>
<li><a href="http://www.thotspots.com/ides-for-groovygrails/" target="_blank">IDEs for Groovy/Grails</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.thotspots.com/demystifying-domain-specific-languages-dsl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

