Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there compelling reasons not to use Groovy?

Tags:

java

jvm

groovy

I'm developing a LoB application in Java after a long absence from the platform (having spent the last 8 years or so entrenched in Fortran, C, a smidgin of C++ and latterly .Net).

Java, the language, is not much changed from how I remember it. I like it's strengths and I can work around its weaknesses - the platform has grown and deciding upon the myriad of different frameworks which appear to do much the same thing as one another is a different story; but that can wait for another day - all-in-all I'm comfortable with Java. However, over the last couple of weeks I've become enamoured with Groovy, and purely from a selfish point of view: but not just because it makes development against the JVM a more succinct and entertaining (and, well, "groovy") proposition than Java (the language).

What strikes me most about Groovy is its inherent maintainability. We all (I hope!) strive to write well documented, easy to understand code. However, sometimes the languages we use themselves defeat us. An example: in 2001 I wrote a library in C to translate EDIFACT EDI messages into ANSI X12 messages. This is not a particularly complicated process, if slightly involved, and I thought at the time I had documented the code properly - and I probably had - but some six years later when I revisited the project (and after becoming acclimatised to C#) I found myself lost in so much C boilerplate (mallocs, pointers, etc. etc.) that it took three days of thoughtful analysis before I finally understood what I'd been doing six years previously.

This evening I've written about 2000 lines of Java (it is the day of rest, after all!). I've documented as best as I know how, but, but, of those 2000 lines of Java a significant proportion is Java boiler plate.

This is where I see Groovy and other dynamic languages winning through - maintainability and later comprehension. Groovy lets you concentrate on your intent without getting bogged down on the platform specific implementation; it's almost, but not quite, self documenting. I see this as being a huge boon to me when I revisit my current project (which I'll port to Groovy asap) in several years time and to my successors who will inherit it and carry on the good work.

So, are there any reasons not to use Groovy?

like image 236
Leonard H. Martin Avatar asked Jan 18 '09 23:01

Leonard H. Martin


People also ask

Is Groovy still relevant?

Groovy, a programming language on the JVM, has found its way back into the Top 20 in this month's Tiobe Index of language popularity after a two-year absence, coming in at No. 19.

Is Groovy any good?

Groovy is a powerful language for the Java platform, it integrates smoothly with any Java program. It's also a great scripting language with its powerful and easy to learn syntax.

Why does Groovy exist?

InfoWorld: Why does Groovy exist? Laforge: Initially, Groovy was created as a companion to Java, rather than as a replacement. The idea was to be able to simplify certain aspects of the Java language to make Java developers more productive.

Is it worth to learn Groovy?

Just because groovy is the choice of a web framework (grails) doesn't mean that you can't use it for desktop apps. Indeed, it is great for desktop apps too. It's more a matter of dynamic or static languages... In my opinion, it is quite good to have for each task the right language at hand.


2 Answers

There are two reasons I can think of not to use Groovy (or Jython, or JRuby):

  • If you really, truly need performance
  • If you will miss static type checking

Those are both big ifs. Performance is probably less of a factor in most apps than people think, and static type checking is a religious issue. That said, one strength of all of these languages is their ability to mix and match with native Java code. Best of both worlds and all that.

Since I'm not responsible for your business, I say "Go for it".

like image 119
Dave Ray Avatar answered Oct 15 '22 07:10

Dave Ray


If you use Groovy, you're basically throwing away useful information about types. This leaves your code "groovy": nice and concise.

Bird b  

becomes

def b 

Plus you get to play with all the meta-class stuff and dynamic method calls which are a torture in Java.

However -- and yes I have tried IntelliJ, Netbeans and Eclipse extensively -- serious automatic refactoring is not possible in Groovy. It's not IntelliJ's fault: the type information just isn't there. Developers will say, "but if you have unit tests for every single code path (hmmmm), then you can refactor more easily." But don't believe the hype: adding more code (unit tests) will add to the safety of massive refactoring, but they don't make the work easier. Now you have to hand fix the original code and the unit tests.

So this means that you don't refactor as often in Groovy, especially when a project is mature. While your code will be concise and easy to read, it will not be as brilliant as code that has been automatically refactored daily, hourly and weekly.

When you realize that a concept represented by a class in Java is no longer necessary, you can just delete it. In Eclipse or Netbeans or whatever, your project hierarchy lights up like a Christmas tree, telling you exactly what you've screwed up with this change. def thing tells the compiler (and therefore your IDE) nothing about how a variable will be used, whether the method exists, etc. And IDEs can only do so much guessing.

In the end, Java code is filled with "boilerplate," but it's been kneaded into its final form after many refactorings. And to me, that's the only way to get high-quality, readable code for future programmers, including when those future programmers are you-in-the-future.

like image 33
Dan Rosenstark Avatar answered Oct 15 '22 07:10

Dan Rosenstark