views:

153

answers:

2

I have been checking out Groovy a bit and I feel that moving a Java program to Groovy little by little -- grabbing a class and making it a Groovy class, then converting the method guts a bit at a time -- might be a relatively sane way to take advantage of some of the Groovy language features. I would also do new classes in Groovy.

Questions:

  1. Is this a reasonable way to convert?
  2. Can I keep all of my public methods and and fields in Java? Groovy is "just" a superset, right?
  3. What kinds of things would you not do in Groovy, but prefer Java instead?
+3  A: 

From Wikipedia:

Most Java code is also syntactically valid Groovy.

And

Some Groovy code looks like it would in the Java language, but Groovy is not a superset of Java. However, barring a few incompatibilities, one can usually rename a .java file to a .groovy one and it will work. Groovy allows the coder to leave out some elements which are required in Java, so Groovy code can be more compact. This makes the Groovy learning curve for Java developers gradual, since they can start with Java syntax and gradually learn to add Groovy features.

In regard to the rest of your question, I think this seems a reasonable way to progress. I would try to focus on changing related classes to use Groovy together though, so that i'ts easy to know which classes use which language when assigning tasks to developers, who may not know both languages.

John
Interesting, that handles some of the question. Or all of it, perhaps.
Yar
+8  A: 

Is this a reasonable way to convert?

Yes

Can I keep all of my public methods and and fields in Java? Groovy is "just" a superset, right?

Almost a superset, but not exactly. For example, == in Groovy calls the .equals method, but in Java it checks whether 2 references refer to the same object. Another example is that Groovy does not support this syntax for constructing arrays

Object[] objs = new Object[] { 1, 2, 4 }

A final example is that when an overloaded method is called, Groovy uses the runtime type of the parameters when choosing which method to invoke, whereas Java uses the compile-time parameter types. This page has a fairly comprehensive list of differences between the two languages.

What kinds of things would you not do in Groovy, but prefer Java instead?

I write everything in Groovy, because I'm much more productive with Groovy than Java, and I enjoy Groovy programming a lot more. I would only use Java if there was a performance problem with some code AND I could demonstrate that writing it in Java resolved the problem. In practice, this has never actually happened to me.

There may also be socio-political reasons to use Java, e.g. some code needs to be maintained by many people some of which don't know Groovy and don't want to learn it.

Don
Don thanks so much for this answer, especially the last part. Early optimization is probably not-so-good and a big waste of dev time. How do you find the autocomplete is in Groovy (good, bad, don't care)?
Yar
Autocomplete in IntelliJ is good. Last time I checked it was unsatisfactory in Eclipse/Netbeans, but that was about 6 months ago and things may have improved since then.
Don
I checked last week and it was not so good. Especially that autocorrect thing in Eclipse that is so amazing (make a method in Class X?) were not happening in Groovy. The reason I'm so interested in this problem is that I work in Ruby with almost no autocomplete (Netbeans) but I think it may be that Ruby is too hard to get right. Anyway... thanks again. i fired up my FREE intellij today, so I'll check it out.
Yar
FYI, the community edition of IntelliJ includes support for Groovy, but not Grails
Don
@Don, I just went through your answer and it's really quite complete and the link is spot on. Thanks for helping.
Yar
@yar - you're welcome
Don