Java and Ruby

[Infragistics] Jess Chadwick / Tuesday, November 13, 2007

Java's a programming language, right? Actually no, not really. Java is more like a platform-neutral environment for coding. The source code syntax we call "Java" is secondary.

Many developers don't realize it, but Java is specifically structured to allow Java development to be done in many different languages. The Java source code we're all accustomed to is really just a set of directions for generating byte-code. So, if you understand the byte codes thoroughly, you can create a compiler to  generate them from any language you care to. Several such compilers already exist. For Tcl programmers there's Jacl. For Python programmers, there's Jython. And for you Ruby programmers, there's JRuby.

Of particular interest with regard to JRuby, is the fact that the JRuby team has been hired by Sun to work on JRuby full time. To understand why, you need to pick up an article on Ruby (e.g. this one from Java World) and see how compact Ruby's syntax is. For example a simple bean, which would be a page or two of code in Java, can be accomplished in three or four lines of simple Ruby code. Since the Ruby engine contains the notion of accessor methods in a way that Java doesn't, you need a lot less code to set up access to your bean properties.

 This compactness carries across all aspects of the language and largely explains the popularity of Rails. I won't go into examples here because I'm sure that working through even a simple Rails example will convince you.

Because Java source is designed to appeal to C programmers, it pursues a a hybridized approach to object orientation, one that is more strict than C++, but less strict than say, Smalltalk. If you are transitioning into object programming from procedural programming, this is a really big help: you have data primitives, and a main method, and it all looks like what you're used to. As you get deeper into it, of course, you become comfortable with objects and the similarities with C become irrelevant.

Eventually, you may begin to wonder why Java syntax is so bulky. "Why do my beans have to be two pages long?" "Why is type-casting such a big deal?" Most of us simply accept it as "the way Java is" and get on with it. But think about it - these problems don't exist in the runtime environment, they exist in the compiler. So with a different compiler (Jacl, Jython, JRuby) you can easily switch your source code development to something that's more friendly to OOAD.

If Java was your first OO language, the thought of switching may well make you shudder. The transition to OO was tough for most of us and its easy to assume that learning another OO language will be as hard as learning that first one was. Its not, though. You've already got all the object concepts and that was the hard part, wasn't it? The syntax is pretty trivial.  

 So why is Sun investing in JRuby development? Well, its possible that now that they've pulled half the programming world into Java (and thereby into objects) they've decided that they want to give us the option of developing Java from a smoother, more elegant source. A source like Ruby, which requires that you understand objects thoroughly, and rewards you with fast,easy development.

 So keep an open mind. There are lots of good OO languages out there, and Sun and other companies are invested in making them work in the Java environment.

 As a final note, I'd like to point out something about Websphere administration scripting. In the early versions of WAS, this was done in Jacl, but a couple versions ago it was moved to Jython. Could JRuby be in the WAS administrator's future? Stay tuned...