6

I'm versed in Java, and starting to experiment with Groovy. Because the two are integrated so well, I find myself writing in Java whatever I can because it's so easy. What specific tips can you offer that would speedup my work with Groovy?

Meaning - in what areas do groovy excel over java, and where should I stick to Java?

ripper234
  • 222,824
  • 274
  • 634
  • 905

2 Answers2

3

Some of the main things I like groovy for:

  • testing is probably the biggest win. The ability to change behavior at runtime and mock out methods is one of the greatest things about groovy. Convert your test suites to groovy now!
  • use the builders. Groovy builders have so much more expressiveness than traditional java. In particular, the MarkupBuilder for embedded snippets of XML or HTML is 1000s of times nicer to use than vanilla java
  • GPars if you're doing any sort of concurrent programming or threading

Also see Hidden features of Groovy and Why would one use Groovy over Java (note: was "removed from Stack Overflow for reasons of moderation").

Where I'd stick with java:

  • any place where speed really matters, stick with java. You still pay a high cost in performance for groovy's dynamic nature.
Community
  • 1
  • 1
ataylor
  • 64,891
  • 24
  • 161
  • 189
-2

The problem with Groovy.

Groovy is a write-easy, but maintenance-nightmare. In my opinion, it should not be used in large projects. Inheriting somebody else's (or your own) code can be problematic, because very often you have no clue of the type of a variable, so you have your due diligence to find out, or use assertions to guarantee the incoming type to a method.

Groovy is a weak-typed language. The type of a variable is frequently ignored, or "conveniently" cast automatically, which leads to many bugs and slower performance.

Even the bests IDE's are lacking, because the practically typeless variables of the language. In many cases the compile just can't know what is the type of a variable. Even if you declare the type of a variable (which helps the editor to make suggestions), many programmers forget to define the variables type.

It has interesting ideas that I would love to see in Java, but stay away from it if your Groovy code will require more than a thousand lines.

* The answers in a nutshell * Summarizing, here are the answers to both questions:

What specific tips can you offer that would speedup my work with Groovy?

Use it only for small stuff. Otherwise, you will incur in technical-debt (see the Wikipedia). So far I'm in similar situation. Unit testing, using the console to test pieces of code, etc., are things that will speed up your development, since it's so easy to learn and use. Be sure to master closures, collections, and looping, and to understand what Java features are not available in Groovy.

Don't use pure Groovy for complex or large applications. In the long term the maintenance will slow you down.

Meaning - in what areas do groovy excel over java, and where should I stick to Java?

In a large or critical project you need to keep yourself disciplined and use dependable tools. It's like building a house: I are build a doll house, Groovy is fine :-) ... if it's not perfect, it's no biggie. If you build your own house or something bigger with little tolerance for error, you need to use quality tools and materials of better quality that don't let you oversee potential problems (Java, for example).

In any case, Groovy is like duck tape: some here and there may do no harm.

luiscolorado
  • 1,525
  • 2
  • 16
  • 23