Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am looking on a new language to pick up and found out about Groovy. According to the website, the language is 'agile dynamic' language.

  1. How is it Agile?
  2. How shorter are the syntax compared to Java's?
  3. Can I use existing Java libs out there?
  4. Lastly, can anyone share their experience on this lingo? What you love and hate
share|improve this question

migrated from superuser.com Jan 27 '11 at 6:51

This question came from our site for computer enthusiasts and power users.

    
This question would really be better suited to Stack Overflow –  Crippledsmurf Jan 27 '11 at 4:38
1  
My understanding is that Groovy is essentially Java with less template syntax and duck typing (strong typing, but determined at runtime instead of compile time). It's been a looong time since I looked at it, so I don't know if they added any other features. Documentation is where I get hung up. –  Berin Loritsch Jan 27 '11 at 13:48
add comment

4 Answers

  1. I don't know either exactly what makes a language "agile", but Groovy let's you put something together quickly.

  2. Depending on your style, quite a lot. You can leave out a lot of Java's boilerplate code, there are no checked exceptions etc.

  3. Yes, without problems.

  4. Mostly I love it, because I like to get things done quickly; I also like that it feels a lot like JavaScript (e.g. Groovy's closures and Javascript's anonymous functions are relatively similar), which is a good thing in a project that uses both; on the other side, many typos are not found by the IDE or the compiler, but only during runtime. Likewise, Intellisense support in the IDE (Netbeans in my case) is by far not as good as in Java. For that reason, I find myself googling the API more often than I would in Java.

share|improve this answer
add comment
  1. A language can not be agile anymore than a car can be liberal . . . though certain cars may appeal to liberals . . .
  2. The syntax is shorter and more concise in several ways.

    List<String> myList = new ArrayList<String>();
    myList.add("one");
    myList.add("two");
    myList.add("three");
    

can be shortened to

    def myList = ["one", "two", "three"]

Another way that you write less code is that you needn't catch or explicitly throw any exceptions. Obviously, it's up to you to make sure that your application recovers gracefully from errors, but this is really nice when you are writing a script that does some file I/O.

One of the most important resources to understand how your code can be shorter is the Groovy JDK which lists the methods that Groovy adds to your familiar Java objects.

3.Yes, this is the biggest advantage of Groovy over Python or Ruby.

4.I've enjoyed it a lot, particularly in connection with Grails. But if I find myself working on a Java team, I expect to use Groovy for scripts, and for test code, when possible. The biggest disadvantage is the tool support, Eclipse and NetBeans are not ready for prime-time with Groovy. IntelliJ makes it tolerable. Ultimately, Groovy successfully removed most of the boring and awkward parts of Java.

Some good resources: Groovy Goodness Practically Groovy

share|improve this answer
    
Your List example is not fair to Java, since List myList = new ArrayList(); works just as well, and is closer to its Groovy counterpart. –  user281377 Jan 27 '11 at 12:24
    
Hoping my edit makes the advantage clearer. –  Eric Wilson Jan 27 '11 at 13:41
    
So is List myList = new ArrayList(new String[] {"one", "two", "three"}); But yes, there is a bit less template code. Oh, and regarding point 3, you can use Java libs when you are working with JRuby. The code won't be portable to C based ruby install, though. Same with IronRuby and .NET code. I don't know if there is a JRuby equivalent for Python, but for .NET there is IronPython as well. –  Berin Loritsch Jan 27 '11 at 13:46
    
Maybe I should have gone with def map = [one:1, two:2, three:3]? –  Eric Wilson Jan 27 '11 at 13:49
    
There is no equivalent for that in one line in Java for sure. But the practical differences really aren't so dramatic. –  Berin Loritsch Jan 27 '11 at 13:52
show 2 more comments

Here is a nice example that applies to #2. This is a method I wrote recently that will return a subset from the list of alerts (which is a list of maps) where the passed property matches the passed value...essentially a search. I don't know of any way in Java to do anything remotely as simple as this with the same outcome.

def byProperty(propertyName, propertyValue) {
    alerts.findAll { alert -> alert[propertyName] == propertyValue }
}
share|improve this answer
add comment
  1. How is it Agile?

This should cover quite a few features of groovy. One of the agile feature that I like is that the properties and methods are dynamic, obj.get('propertyName'), obj.propertyName and obj.getPropertyName() are doing the same thing (if not messed up). It greatly helped in projects where there is a common property on all objects without a proper base type (or when the type cannot be imported)

  1. How shorter are the syntax compared to Java's?

Yeah, the collection methods (each, findAll, collect, etc) are good way to shorten the code. Groovy builder also offers better syntax than Java builder. Another example:

myobj?.propA?.propB?.propC?.propD

will return null if any of the property in the chain is null, saving a lot of null check code.

  1. Can I use existing Java libs out there?

Yes, and both Java and Groovy compiles to the same kind of .class file.

  1. Lastly, can anyone share their experience on this lingo? What you love and hate

It is not bad and pretty convenient in many ways, just need to have some discipline as the code can become too tricky to read/debug (or I am sure you will hate it :) ). In serious project, sometimes you need to do experiments to find out what the shortcuts really do/mean.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.