Benad's Web Site

Two weeks ago, while trying to recommend a friend what scripting languages for Java he could use (I ended up giving him this link), I started trying them out myself. I was worried, though, that they would either be too drastically different from a C-like or Java-like syntax, or if not, that they would be atrociously slow, given that you're running all of this on top of Java.

An issue I started to find is how much those languages tended to be almost a theoretical replacement of the Java language, as if a few new features they would add on top of it would be enough to justify learning yet another language. I fell like we're back to 1975.

Yet, lots of stuff annoys me with Java. Even simple things like reading lines from a file needs a complex macro. But I don't want a radical replacement of the Java syntax, and I still want OK performance...

And then I saw Groovy. It is not your typical "computer scientist" language, but more a "software hacker" language. In many ways, it is close to Perl:

  • It's ugly but short. The syntax is there to write code faster, not in a more "beautiful" way. Groovy is not as horrible as Perl, but can be a bit difficult to read.
  • It's compiled to bytecode before execution. Actually, groovyc will generate class files for you. It's normal Java code, but you still need the Groovy JAR files. In effect, because Groovy thus taps in into the run-time optimizations of Java, this could make Groovy faster than similar Perl code.
  • It does what you want, not what you write. Simple things like the == operator does what it should have meant for strings, without having to use explicitly .equals(String) (you still can). Like Perl, object type is not required, since everything derives from java.lang.Object. Same thing also for function parameters and its return value. Oh, and you don't have to write that semicolon at the end of the line (yeah, that's a bit weird at first).
  • Code that should be simple is simple. All those things like using a database, reading or writing an XML or HTML file, reading and writing to a file, using Java Beans or using regular expressions is easy. With Groovy you avoid having to rely on a complex or strange framework to write code faster.
  • Scripts can be reused easily. Every groovy script file is actually a class file with its main function and its member functions. So while the scripts look "flat", they are self-contained and reusable. (But, honestly, you better write your high-quality Java library in Java, not Groovy...)

Obviously, Groovy is far from perfect. The language is rather new, so documentation is difficult to find, and you could hit an occasional bug or two. Also, it doesn't have a debugger yet, but it shouldn't be too hard to add one later since the Java bytecode it generates could use the standard interface to Java debuggers that is already used by all those Java IDEs out there. And, performance-wise, it's still slower than Java.

But otherwise, Groovy can be used to write quick-and-dirty scripts or used as a replacement of the ugly mess that is JSP. Yes, like what Perl was meant for, but in and for Java.

Published on November 28, 2005 at 16:42 EST

Older post: plasticfs example

Newer post: Virtualization As A Solution For Package Management