InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Is Groovy a Better Choice Than Java for Creating Internal DSLs?

Posted by Srini Penchikala on Oct 14, 2008

Sections
Development,
Architecture & Design
Topics
Java ,
Domain Specific Languages ,
Dynamic Languages
Tags
Groovy ,
DSLs

JVM-compatible languages such as Scala, Groovy and JRuby are recently gaining more popularity for developing Domain Specific Languages (DSLs). But are they better suited to creating internal DSLs than the Java programming language? Venkat Subramaniam explains why "Essence over ceremony" and "Metaprogramming" features in a dynamic language like Groovy help in developing internal DSLs.

He suggests that Java language is a better fit for creating external DSLs than internal ones, which are more dependent on the host language syntax. But when it comes to internal DSLs, the power of dynamic methods in Groovy language is a better fit. Venkat recently wrote about the metaprogramming feature in Groovy and how to take the advantage of this technique to writing internal DSLs.

Metaprogramming, which is a common characteristic of newer Java-platform languages like Groovy and JRuby but not of the Java language, helps in building internal DSLs allowing the developers to add and invoke methods dynamically. Groovy maintains a metaclass for each Java class. The developers can easily add methods to classes at runtime. It also makes it easy to synthesize methods at runtime by using methodMissing method.

Venkat explained the steps involved in creating a Groovy-based internal DSL with a sample application which is based on a simple input file. He also compared processing the example DSL from within Groovy code and from within the Java code with a sample application.

Groovy's use in writing DSL's has been well documented and there are several DSL implementations like behavior testing DSL, Groovy based Behavior Driven Development (BDD) tools GSpec and easyb, and Groovy Finite State Machine (FSM) DSL .

InfoQ spoke with Venkat about the current usage of DSL's (external and internal) in enterprise applications and the future role of the dynamic languages in creating DSLs. He said:

DSLs have been in popular use for a long time in enterprise applications. What is new probably is an increased awareness of these and more tools availability. Certainly the momentum behind dynamic languages is helping with the use of internal DSLs.

Metaprogramming in Groovy seems to be a powerful feature. What are the typical use cases for using this feature in developing web applications?

It certainly is. GORM already uses this quite extensively in Grails for Web applications. If you add a field to a table, you can readily use finder methods on the corresponding model class to query for objects based on the values of this newly added field. These methods are synthesized dynamically based on convention instead of you having to create these methods manually. You can also take advantage of Metaprogramming anywhere you need similar dynamic behavior. You can also use it anywhere you'd have considered using AOP techniques.

How do you see the future role of dynamic languages in creating the internal DSL's?

There are certainly some challenges. On one hand, dynamic languages make it insanely easy to parse and process internal DSLs. However, validating the internal DSLs is not easy. I am sure tool support as well as good validators will emerge in the coming years. This is certainly an area of good interest for research and experimentation. Assuming a few of these hurdles are crossed relatively soon, the ability to mix dynamic languages with more traditional languages on the same platform like Java and .NET will make internal DSLs highly preferable.

In a previous article, Venkat explained, using a sample application, how to create external and internal DSLs using Java language. A more detailed discussion of Metaprogramming in Groovy can be found in this book by Venkat. For more information on the role of Java in creating internal DSL, check out this article. More InfoQ coverage on DSLs can be found here.

Srini Penchikala currently works as Security Architect and has 17 yrs of experience in software product management.

Scala Notes by Daniel Spiewak Posted
Groovy for Runtime Errors by Ricky Clarkson Posted
Re: Groovy for Runtime Errors by Srini Penchikala Posted
Re: Groovy for Runtime Errors by Vadim Mikhnevych Posted
Re: Groovy for Runtime Errors by Floyd Marinescu Posted
  1. Back to top

    Scala Notes

    by Daniel Spiewak

    Methods are not dynamic in Scala. It may *look* like they are, but the language is purely static and everything is well-typed. Just because a language provides the facility to add methods to existing classes does not make it dynamic (or non-lexically scoped).

  2. Back to top

    Groovy for Runtime Errors

    by Ricky Clarkson

    Groovy takes Java's existing syntax, dumps the static type checking and adds extra syntax. In Groovy, an explicit type causes a runtime assertion, making explicitly-typed code slower than untyped code.

    In other words, when you use Groovy, you're making a conscious decision to push errors to the runtime, or you don't know what that means. So Groovy is appropriate for programs where runtime errors don't matter much (for this reason I don't mind configuring my text editor in elisp) or for novices.

    It's kind of worrying that infoq allows someone to write an article suggesting that Scala has dynamic methods. It doesn't. Scala's mechanism for adding methods to classes is closer to (but better than) C#'s extension methods than it is to Ruby's open classes.

  3. Back to top

    Re: Groovy for Runtime Errors

    by Srini Penchikala

    Hi Daniel & Ricky, Thanks for pointing out about Scala and dynamic methods. I have updated the post to reflect this. -Srini

  4. Back to top

    Re: Groovy for Runtime Errors

    by Vadim Mikhnevych

    I feel sorry that InfoQ is not moderated and allows posts with links to "arap sikişi", like the one above. :-(

  5. Back to top

    Re: Groovy for Runtime Errors

    by Floyd Marinescu

    I feel sorry that InfoQ is not moderated and allows posts with links to "arap sikişi", like the one above. :-(

    We have removed the spam you mentioned, we are doing our best to deal with these spammers, it's unfortunately a purely manual process to deal with.

Educational Content

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.