Which is better? Groovy VS Java

Agile Coding Dojo #2

Last week, Agile Vietnam organized the second Agile Coding Dojo event. The goal of the Dojo was to practice coding skills in a real-life work situations: write unit test, refactor bad code, etc.

For this second event, Agile Vietnam chose to code in Groovy. Groovy, just like Java runs on the JVM. They invited me to present the simplicity of Groovy syntax over Java syntax. See the slides here.

At the end of the presentation, Hiep Le (the event organizer) asked me if there were situations where Java would be better than Groovy. Below is a my detailed comparison between the two languages.

Java VS Groovy

Strengths of Java

  1. Faster: That’s because Groovy does a lot of runtime change. This impacts performance of code. In fact, Groovy has been shown to better 2x-8x slower than Java.
  2. Better IDE and Tool support. This is important for two reasons:
    1. Better code completion
    2. Better debugger
    3. Better code analysis
  3. More developers: it’s harder to hire experienced Groovy programmers. In Vietnam, I have yet to meet someone who is coding in Groovy. Still, Groovy is the second most popular JVM language.
  4. Strict language: Java is more “friendly” for inexperienced developers. Groovy is dynamic and flexible: you can write the same code in many different ways. Of course, you can write Java looking code in Groovy; in a team however, it can be challenge to enforce consistent code.
    1. Java uses strong typing, Groovy uses duck typing. I personally think duck typing makes code less understandable.
    2. Java forces catching of almost exceptions, Groovy doesn’t force catching. Sometimes it’s ok not to catch exceptions. Most of the time it’s not. With Java, a young programmer is forced to deal with errors from the program. With Groovy, he doesn’t: this will eventually lead to problems.
  5. Easier to debug: Again, because Groovy does a runtime code changes, it’s hard for it to trace back runtime errors.

Strengths of Groovy

  1. Rapid development: Groovy code is easier to write. The Grails framework (Rails for Groovy) makes rapid development a breeze.
  2. Better maintainability: Groovy code is easier to read. From personal experience, converting Java code to Groovy saves 30-50% lines of code. Everyone loves savings.
  3. Great support for XML and JSON.
  4. Flexible syntax: you can adjust the complexity of the syntax with your needs. You can write simple Groovy syntax for simple scripts or testing. You can write Java looking code in Groovy for other situations.

It’s all about the situation

In general, between technology A and B, neither is better. Technology A is stronger in some situations, B is stronger in other situations. It’s important to make the right technology choice for the right situation.

So where would Java be best?

The answer is: I don’t know. I have worked on tech startups products for most of my career. Small team with very talented developers.The biggest strengths of Groovy is code maintainability and speed of development. Those strengths are great benefits in early stage product startups. The biggest weakness of Groovy is runtime performance. For apps that connect to the internet however, network performance will more of a priority.

One guess is that Java is probably better in complex desktop enterprise applications at a big company. But I haven’t worked on one of those for a long time.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s