main banner

Development

Is Groovy groovy?

An introduction to Groovy's concepts.

Let’s start by defining what exactly Groovy is and why you would consider it as an option to build an application.

Groovy is an object-oriented programming language that runs over the Java Virtual Machine; the code is dynamically compiled as Java bytecode allowing it to interact with other Java code.

When Groovy is compiled to bytecode, the following rules are used:

  • If the name is declared with an access modifier (public, private or protected) then a field is generated.
  • A name declared with no access modifier generates a private field with public getter and setter (i.e. a property).
  • If a property is declared final, the private field is created final and no setter is generated.
  • You can declare a property and also declare your own getter or setter.
  • You can declare a property and a field of the same name; the property will use that field then.
  • If you want a private or protected property, you have to provide your own getter and setter, which must be declared private or protected.
  • If you access a property from within the class, the property is defined in at compile time with implicit or explicit this (for example this.foo, or simply foo), Groovy will access the field directly instead of going through the getter and setter.
  • If you access a property that does not exist using the explicit or implicit foo, then Groovy will access the property through the meta class, which may fail at runtime.

Why use Groovy?

Groovy syntax is simple and easy. It saves a lot of code and effort thus increasing the developer’s productivity in case the same thing needed to be done in Java.

Groovy syntax: println "Hello world!"

Java syntax: public static void main(String args[]) { System.out.println(“Hello world!”); }

It allows you to define closures (defined below).

Groovy accepts almost all Java programs as it can use the Java libraries and it even extends some core Java classes as well. System.out is assumed - Groovy automatically imports the packages groovy.lang.*, groovy.util.*, java.lang.*, java.util.*, java.net.*, and java.io.*

There are Groovy Beans, GroovyBeans are JavaBeans but using a much simpler syntax.

class Car {

 int numberOfDoors

 String model

 String brand

 boolean automatic

 double price

 

 String toString() {

     "[Car details => brand: '${brand}', model: '${model}', #doors: '${numberOfDoors}', automatic: '${automatic}', price: '${price}']"

 }

}

Car ford = new Car(brand: 'Ford', model: 'Focus', numberOfDoors: 4, automatic: false, price: 24995)

Car toyota =  new Car(brand: 'Toyota', model: 'Prius')

toyota.automatic = true  // Yes, this invokes setAutomatic.

toyota.setPrice(28919)  // Normal set invocation.

toyota.setNumberOfDoors 5  // In Groovy we can omit the parentheses.

println ford  // Output: [Car details => brand: 'Ford', model: 'Focus', #doors: '4', automatic: 'false', price: '24995.0']

println toyota // Output: [Car details => brand: 'Toyota', model: 'Prius', #doors: '5', automatic: 'true', price: '28919.0']

It is open source and used by a very popular Grails framework.

It is supported by most popular IDEs.

Closures

We can take a sequence of statements that refers to its external context and assign it to a variable, then execute it later. It’s technically called a “closable block”, commonly called a “closure”. A closure is an open, anonymous, block of code that can take arguments, return a value and be assigned to a variable. A closure may reference variables declared in its surrounding scope. In opposition to the formal definition of a closure, a closure in Groovy can also contain free variables which are defined outside of its surrounding scope.

For example:

def checkMovie = {
println it + " is a good movie"
}

list = ["Alien", "Star Wars" , "Saving Private Ryan" ]
list.each(checkMovie)

The above code prints the following

Alien is a good movie
Star Wars is a good movie
Saving Private Ryan is a good movie

Here 'it' represents the each single item in the list that is passed to the Closure 'checkMovie'.

The code inside the braces is closure code acting like a callback for all the elements of the set.

What are Closures used for?

  • Iterators
  • Callbacks
  • High-order functions
  • Specialized Control Structure
  • Builders
  • Resource allocation
  • Threads
  • DSLs
  • Fluent Interfaces

Conclusions

Groovy is indeed not for everyone, but can certainly be considered as a powerful option if you need to build small to medium size applications. For Java developers, the learning curve would be more flat than for those developers that come from a non-object-oriented language.

Groovy scripts are interpreted at runtime; this might cause some performance overhead, which is not all that bad. You can always compile it to Java bytecode to remove that performance penalty.

Groovy is great for scripting and as fast and safe as Java.

Bibliography

Groovy

Subramaniam, Venkat. Programming Groovy 2. Pragmatic Bookshelf, 2008. Print.


Rubén M.

Ruben has been one of Inflection Point’s key members for many years; born in Mexico City, he strongly believes there are quesadillas without cheese. He would describe himself as a movie fan, avid music listener, hardcore F.C. Barcelona fan and keen comic books and science fiction novels reader. He has a B.S. in Computer Science and is a full-stack developer being Java his favorite flavor.

Articles