Paul King had a very good presentation (last year’s slides) at JavaZone about why to use Domain-Specific Languages and how to create internal DSLs in Groovy. I’d like to list here few tips that he has mentioned but before we get to that, why would you want to create a DSL? Martin Fowler answers that in his Domain-Specific Languages book (2010). Some of the reasons are to have a higher-level, more focused and conscise representation that also domain experts can read and perhaps even write. You have certainly already used a DSL such as regular expressions, CSS, SQL, Spock‘s BDD tests, build instructions in Gradle – these are rather technical but sometimes DSLs are also created to be used by business users, f.ex. for anti-malaria drug resistance simulation. (Want more DSLs in Groovy?).
Paul mentions one important thing – you can always make your DSL better, i.e. more fail-proof (case insensitive, support plural endings, …) and secure and more like a natural language but it all comes at a cost and you must evaluate when the cost overweights the benefit (beware the 80:20 rule).
Some of the Groovy DSL implementation tips:
Read the rest of this entry »