Making Apps Groovy


Programmers are lazy.  We try to write the least amount of code we can, reuse our old work and tend to require a lot of caffeine.  The field of computing has evolved over time to address this.  From punch cards to IBM mainframes to the promises of “The Cloud”, each step has increased how expressive developers can be with their code and how much work that requires.  Developers learned very quickly that the more code they write means more time spent on code maintenance and less on new features.  We hate writing unnecessary code, messing with configuration files, and as a result, programming conventions have evolved to address this.  Open source libraries are standard in most code bases and principles like Don’t Repeat Yourself (DRY) are our motto.  I want all of my functionality to work as easily and simply as possible, and my manager wants most of my time focused on business logic.  Groovy and its associated web framework, Grails put this idea into practice.  By using a dynamic language with simplified configurations, it becomes easy to write clean, maintainable code that facilitates a rapid development cycle.

I come from a Java background.  As one of the current most popular languages, it incorporates many current design philosophies and enjoys support from a wide range of third party libraries.  With the release of Java 8, the language has become more functional, and thus more expressive with the inclusion of Lambda functions and Streams.  These features work great, but at times feels overly verbose when compared to other popular languages.  Java has proven to be effective for creating web applications.  Java has been used by a wide variety of companies for many different use cases by taking advantage of frameworks such as Hibernate and Spring.  While these frameworks are great and I have enjoyed using them in the past, there is a lot of configuration that can be tedious, lead to bugs, and slow down development.  Whether a developer forgot to add the spring annotation to a service, or the xml config had a misspelling, the set up for seemingly simple operations gets annoying.  Why can’t the framework just know the basics about your application?

That brings me to the programming language Groovy and its web framework Grails.  Groovy is a dynamic language built on top of the Java Platform.  The syntax is familiar to Java, yet much more dynamic and simplified.  This means seasoned Java developers can quickly get comfortable to the new language, and take advantage of the expressive features.  With a simplified syntax the code is less bulky making it easier to read, maintain and get new developers up to speed.

The advantages become apparent when developing web applications with Grails.  Using the dynamic nature of Groovy, Grails builds on this with a Convention-over-Configuration approach, utilizes sensible defaults and provides easy to use object relational mapping.   Coding by convention removes unnecessary complexity from an application allowing a developer to focus on business goals.  Instead of registering services and controllers with the framework, Grails recognizes the function of any file the ends with “Controller” or “Service.”  While this is a simple example, similar conventions exist all over the framework that simplify implementation and save time.  All of this is built around Grail’s easy to use domain modeling through GORM.  This allows a web application to model and create relationships between the data in the database.  Under the hood, GORM uses Hibernate so the design patterns will feel similar to Java developers.  However due to the dynamic nature of Groovy, the domain classes take less time to set up and querying is easier to read and understand.  GORM supports multiple ways to query the database depending on the complexity of the query.  A simple search can be done in with a dynamic finder (Company.findByName(“UDig”)), a simplified criteria search or even using HQL.

Grails is a powerful web framework that takes advantage of the dynamic and expressive Groovy programming language.  In my experience, I have been able to produce clean, maintainable code with minimal configuration. I find myself concentrating on the business logic, the real purpose of the app, more than comparable frameworks.  Since it is built on the Java platform, it is familiar enough for Java developers and contains enough value adding features that extend a programmer’s toolbox.  The technology is fun to use, easy to read and applicable for many applications.

About The Author

Tom Duffy is a Lead Consultant on the Software team.