Wednesday, September 19, 2012

Groovy Day 3 : operators overloading

Back in 2009, when I learned C# for a Microsoft Contest (Imagine Cup) I was wondering why Java has no operator overload mechanisms. After discussion and thinking, the problems are the readability and possible deratives. Firstly the readability because it could disturb the newbies when they see a code where an element is added to a list using a +=. It is not always clear, depending on the language, which method is called internally. The second problem comes from the first one in some sense. You could imagine a new way of code obscuration when you implement the addition with a minus symbol or the append on String with a division symbol. Just imagine the chaos that could appear.
Groovy resolves the first problem by giving a limited set of symbol that can be overload. For example, the constructor operator cannot be overloaded. This limited choice reduces the potential mistakes. Then Groovy provides by its JDK (Java development kit) some common operators that follow standards. Additionally  the operators are linked to named methods, this provide a certain security for the new developers. They could easily use the normal methods to do their stuff.
Stop the theory, let's show some examples !
// initialization of a list using Groovy facilities
List list = []
// append 'hello' to the list
list.add('hello')
// without parenthesis this time
list.add 'world'
// using leftShift method
list.leftShift('too')
// using the symbol leftShift
list << 'other'

assert list == ['hello', 'world', 'too', 'other']

Saturday, September 15, 2012

Groovy Day 2 : Differences in term of readability between Java and Groovy

For this article, we will see the differences of readability between an "old" language (Java) and a dynamic language (Groovy). You will see at first the evolution of Java to try to become more readable for the novice (and less tiring for the advance developper). The second part is the Groovy version of the same code and you will see the difference very easily.
I decided to use a simple example of a creation of HashMap and store in it the names of the seasons and the related adjectives.

First code snippet is a Java 1.4 code.
class HashMapJavaShow {
   public static void main(String[] args) {
      // creation of the hashmap
      HashMap<String, String> seasons = new HashMap<String, String>();
      
      // insertion of data, key => value
      // in this example we take the name of the season as key
      // and the related adjective as value
      seasons.put("spring", "vernal");
      seasons.put("summer", "estival");
      seasons.put("automn", "autumnal");
      seasons.put("winter", "hibernal");
      
      // before 1.5 we were forced to use an iterator
      Iterator iter = seasons.iterator();
      // loop until the iterator has not more element
      while(iter.hasNext()){
         // retrieve the current element and display it
         // using its default method toString()
         System.out.println(iter.next());
      }
   }
}

Sunday, August 12, 2012

Groovy Day 1 : Truth (2/2)

The second part of the post on Groovy Truth is about collections and custom conversion to boolean. First the collections, in Groovy, a collection can be tested for nullity and emptiness at the same time.
List list1 = [2,3,5] // way that groovy declare List, in this case, an ArrayList is used
List list2 = [] // empty list
List list3 = null

assert list1 // test is the object is null, if it's not the case, test emptiness on collections
assert !list2 // the list is non-null but empty
assert !list3 // the list is null

Groovy Day 1 : Truth (1/2)

This post is the first of the serie on Groovy discovery. To start easily the learning of Groovy, we need to understand the differences with Java. Groovy is a language that is compiled in bytecode like Java. The bytecode is then read by the same JVM. In other words, Groovy can use Java library (all the .jar that you can find in the internet, quite amazing) and Java can use Groovy library too ! The magic of bytecode in some sense. We could also imagine an interaction with Scala or JRuby etc. all the JVM language are intercompatible. This was for introduction, let's discover the first real part. The Groovy Truth is something very weird for an old Java developper. In Java we have some really particular cases to learn and understand to avoid common mistakes.