lördag, februari 16, 2008

Is Groovy like Java?

I usually don't mention the G word in my blog - the results have a tendency to be less than palatable. This time I'm going to make an exception though. The reason is that this meme has been floating around as an argument for Groovy, and I really don't agree with it. In fact, the reason I'm writing about it is because I think the meme in itself generally can be bad for Groovy.

The thing I'm talking about is the saying that Groovy is just like Java. That you can start using Groovy directly without learning anything about Groovy and that basically all Java syntax is valid Groovy.

Now, the last part has never been really true, since there are a few valid Java constructs that are not supported in Groovy (anonymous classes, I'm looking at you). This might be fixed in the 1.5 version. I don't really know, but that's not the point. Since Groovy isn't using the Java parser there will always be a few small corner cases that are not valid in Groovy but Java accepts. This is not a problem, by the way. The only way it can be a problem is when you're using Groovy the wrong way, or you absolutely need to do something that Groovy doesn't support. Since one of the Groovy catch calls is that you shouldn't implement everything in Groovy, this can't really be a concern either. If you're using Groovy correctly, you should just drop down to the Java layer for that particular feature.

Now that that part is out of the way, let's take a look at the other parts of this idea. Namely that you should choose Groovy because it's just like Java, and you can start writing Java code directly in Java. These statements are definitely true, since you can absolutely pick up a language that way. In my mind, though, this misses the whole point of using another language on top of the JVM. If you're building an application and chooses to use Groovy for this instead of Java, isn't the reason that you're doing that the fact that you expect Groovy to give you something Java does not? And if that's the case, doesn't it sound like a disservice to people picking up Groovy to say that they can begin by just programming Java. There is no gain here. Really. In fact, the equivalent program in Groovy and Java will perform much worse on Groovy since there are several layers of dispatching in Groovy that you don't have in Java. The result? An application with exactly the same source code, but running 10 times slower.

Since this message is mostly used in marketing and introduction facilities, the people it's geared at are by definition newbies to the Groovy world. Many of them are totally new to dynamic languages. And the first thing they do is write Java in Groovy, find it slow and obviously, from that point don't understand why you should use Groovy. This is bad for Groovy. Groovy has some extremely powerful features and libraries that can really make your life on the JVM blissful (compared to using Java, for example). Groovy's integration with Java is top notch, which means that you can always fall back to Java when you need Java specific features. If you're using Groovy, I think you should use the full power of the language so you can actually get the full benefit of Groovy.

5 kommentarer:

Robby O'Connor sa...

well said!

Unknown sa...

Amen that.

One of the biggest features of Groovy/JVM languages is not that you can program it like Java, but that you can use/call Java classes with it.

For example, look at these Groovy benchmarks.

One looking at that might say "Groovy performance sucks!"

I say "Why don't you code your slow parts in Java?"

Not to say Java has extremely useful frameworks in its world.

Grails for example, do you think they re-implemented Hibernate and Spring in Groovy? No.

Is Groovy like Java? Yes, but that's only a part why it's a great language.

Michael Easter sa...

I agree that it is incorrect to say that "Groovy is just like Java".

However, I think the concern about newbies is overstated. Newbies are usually intellectually curious. It is hard to believe they would take the time to download Groovy, toy with it, but then abandon it so quickly without asking why.

In fact, the danger may lay with Java developers who are not curious and perpetuate the meme without ever trying Groovy. Unfortunately, it is very difficult to communicate with these people. (e.g. They typically aren't blog readers.)

Graeme Rocher sa...

Nice to see a positive sounding post about Groovy from you Ola ;-)

What I would say is that syntax compatibility is only one small part of why Groovy is "like" Java.

Other important factors are:

- API compatibility, ie the Groovy APIs are the Java APIs with some extensions
- Byte code interoperability, so you can jointly compiler Groovy and Java. They share the same byte code format and a Groovy class maps directly to a Java class
- Debugging and Profiling, Groovy uses the standard Java debugger and works fine with Java profiling tools (you just need to filter out internal packages)
- Support for Annotations/Generic and Java 5 features for example you can write JPA entities in Groovy and with many recent Java specs embracing generics and annotations this is really helpful

Anonym sa...

I am all with Michael here. If you want to do java you do java. If you do groovy you have reasons. Probably you are a curious person and you have come to a point to realize that java doesn't do the trick for you anymore.

So the java like syntax with extensions comes natural and reduces the noise, because you want to learn how to use the cool stuff and not how to declare member variables with at-signs and all the other accidental ruby idiosyncrasies.

I can however see how people might think that having a semicolon after each line is just noise ;-).