Statically Compiled Groovy: Give Groovy a Chance
Give Groovy a chance.
Join the DZone community and get the full member experience.Join For Free
One of the biggest complaints I hear about Groovy is that it is a dynamically compiled language — not that any dynamic language or platform could ever become popular or useful (Python, JS/node, Ruby, and more). However, Groovy isn't just a dynamic language; it's an optionally typed language. This means you can mix and match the power of a dynamic language with that of a statically compiled language.
Another complaint I often hear once people realize that Groovy can be statically compiled using an annotation at the class or method level is that it feels bolted on. To address the second concern, I recently released a Gradle plugin called Enterprise Groovy, which can be found on my GitHub page.
Enterprise Groovy gives you static compilation by default, which provides optional enforcement through a configuration. This provides options for disabling dynamic compilation, which can also be done by an annotation, disallowing
def in most places and restricting which compile static extensions can be used. Enterprise Groovy is meant to be used for projects where you want to rein in the dynamic features because you have a big team or junior developers, and you don't want them to be able to write bad code with dynamic features (even though you can write bad code in any language).
Enterprise Groovy also provides you with a task for the Groovy console, which is run in the context of your project. The Groovy console can is a very helpful tool because it allows you to play with Groovy code and run it in context. The Groovy Console also comes with an AST(Abstract Syntax Tree) Browser, which allows you to look at the code at different stages of the compilation process. While that might not seem useful to the normal user, it gives you valuable insight into what the compiler does to your code. For example, if you use Groovy's
@ToString annotation, this is actually an AST Transform, which adds an implementation of a
toString method to your code during compilation. With the AST Browser, you can see what that implementation will be, which takes away the magic of Groovy and just makes it like any other tool.
So, why would you want to use Groovy in the first place?
- Actively being updated (it may be faster than you remember with additional features)
- Very nice and has a helpful community
- Runs on the JVM
- Contains seamless interop with Java
- Holds optional static compilation/type checking
- Less verbose than Java due to Inline Lists/Maps, Triple-quoted strings, etc.
- Functional, with aspects and closures delegates
- Runtime metaprogramming
- DSLs to simplify problems (Spock, Geb, Gradle, Grails, etc.)
- Compile-time metaprogramming (AST transforms/Macros)
- Macro Methods
- Enhanced JDK (Extension Methods)
- Enhancements for scripting
That's not all — Groovy has a lot of other powerful features. I like to use the analogy that it's like a forge; you have the power to forge code and do what you want with dynamic features like metaprogramming, both at runtime or at compile-time, which lends itself to be useful for creating DSLs (Spock, Geb, Gradle, Grails, etc.).
In fact, Enterprise Groovy uses the compile-time metaprogramming with an AST transform to give you the power to control some of Groovy's dynamic features and have Groovy statically compiled by default. Groovy can also be used with many popular frameworks Spring/Spring Boot, Micronaut, Ratpack, Grails, and others.
Groovy has been more or less the silent contender of the JVM languages that gets less attention than some of the other JVM languages like Kotlin or Scala. That's been one of its biggest problems. While the community is nice, it doesn't have much excitement in terms of marketing. Despite this, many people continue to use Groovy to get stuff done, which is apparent, as Groovy was downloaded over 100 million times last year. OCI has been a big contributor to Groovy, taking over where Pivotal left off, helping release Groovy 2.5 and making it work with Java 9+, adding macros/macro methods and some updates to AST transforms. OCI and the community have also been working on Groovy 3, which should come out sometime this year with aditional improvements.
So, with all that being said, I think it's time to give Groovy a chance, if you haven't already. Here is a link to a bunch of other Groovy resources to get you started.
And have a Groovy one!
Opinions expressed by DZone contributors are their own.