Tuesday, December 28, 2010

The power of Groovy - notes of "Transforming to Groovy"

Yesterday I watched a video presentation from infoQ:"Transforming to Groovy", presented by Venkat Subramaniam, I read his book: Programming Groovy. The presentation is really great, the author used lots of code examples to explain the power of groovy: first he provided the java code, then refactored it into groovy code, so you will find out how concise the Groovy is compare to Java. The whole presentation is 1.5 hour, but I did not feel it is quite long.  I remembered the author describe the Groovy using the term "less ceremony", Which reminds me the how to describe the power of Groovy: "Accomplish more by doing less". And when he refactored java code to Groovy code, he described it as "reducing the noise".  I like the word "noise" to describe Java code, since Groovy is quite expressive, concise, and its dynamic nature allow developer to focus more on their core business problems ( essential problem), and spend less time to deal with the language issues (accidental problem), which is noise compare to the business problem.

What I learned
AST transformation
   Before I did not aware the feature of AST transformation in Groovy, probably this is the new feature,  because I did not find this topic in the book.
  The author covered @Immutable, @Newify, and @Delegate

@Immutable
@Immutable class Person {
   String firstName;
   String lastName;
   int age;
}
@Immutable annotation applies to class level only, it does not support on the field level.

@Newify
@Newify def createPerson {
    Person.new("John", "Smith", 28)
}

@Newify(Person)
def createPerson {
   Person("John", "Smith", 28)
}

@Delegate
class TirelessWorker {
   public void work() {}
   public void report() {}
}

class Manager {
   @Delegate employee = new TirelessWorker();
   public void schedule() {}
}

Here the Manager class can use all the methods of TirelessWorker class, if you add/modify method of class Tirelessworker, you don't have to modify the Manager class, it follows the OCP principle.

Multimethods
Multimethods is one of the major difference between Groovy and Java. Multimethod means the method call depends on the target and the parameter type, which reminds me the multiple dispatch, Java does not support mutlimethod, it only support single dispatch, I remember when I learn the Visitor pattern, I learned the multiple dispatch term, I am sure the Visitor pattern will be much easier to understand if the language support multiple dispatch, cool.

ArrayList
use spread-dot operator to access ArrayList
*.  is called spread-dot operator, for example:
list*.member  =  list.collect { item -> item?.member }

It means given a list, iterate each item's specific method, form the return value as a collection.
get each item's first name: list*.firstName

Groovy follows the Kanban principle
I think Groovy is a good example of evolutionary and emergent design, since you can begin with purely java code, then migrate with Groovy, and even more you can mix with java and Groovy code in your project. Let's look at the Kanban's 2 foundational principles: Start with what you do now; Agree to pursue incremental, evolutionary change. Pretty similar.

Based on this, I think Groovy is an important language that worth every Java developer to looking at.

No comments:

Post a Comment