Tuesday, December 4, 2007

Converting Ruby to Java

I readily admit that my previous post about the lack of distinguishable efficiency between Ruby and Java was absolutely off-base. Completely.

I'm still working on that same simulation program that I described yesterday. Why? Although I designed the program using Ruby, I need to provide the source code in Java. I always knew this, my thought process was that I would try out the development process using Ruby, and then port the program to Java once I was completed. Which is what I attempted to do last night. Which was another 'eye-opening' experience.

It seems that my current lack of Java exposure has numbed me to the amount of cruft in a typical Java program. I started the port fairly straightforwardly... copy the Ruby code into a Java class, and change the various Ruby-specific keywords to their Java equivalents, and that should be it.

So, I begin to change def to public void/int/boolean, end to }, and removing the @ and : from instance variables and symbols respectively.

It was then I began to realise how much visual clutter Java forces on you. I needed to put the opening brace after every method definition. I needed to add opening and closing parentheses to all my method definitions AND function calls. And don't forget the dear old semi-colon after every line. Nope, we can't use the if/while/unless statement modifiers, so every one of those becomes a full-blown if statement block, complete with all its brace-y goodness.

Just making all the syntax changes was an experience... by the time I was done, my code was noticeably longer, and just seemed much more like code and less readable as a result.

But, the difference between Java and Ruby goes beyond syntax. So, the stage two of the conversion involved changing constructs such as blocks, array sorting, use of hashes etc to Java equivalents. And there was where the true differences between the languages surfaced. You see,he language you use does influence your coding style, by making certain things easier to do. Thus, the natural syntax of hashes make them extremely popular, as Rails can attest to. Of course, the Java equivalent is a huge pile of clunkiness that I don't even want to think about.

The end result is that after the straightforward conversion of standard syntax differences, I actually had to then rewrite logic for the Java version because of its lack of certain Ruby functionality.

That is to be expected, right? I mean, they are different languages after all. Some things will be easier in Ruby than in Java and vice versa, right?

Of course, so far, I've only come across things that are harder in Java than in Ruby. I've yet to find something that Java does better.....

Hmm....


PS. What has me particularly cranky is the fact that in converting some of the Ruby constructs to Java, I broke something, and I'm having a hard time figuring out what. And it's annoying me that I have to go through the same debugging process again in Java that I went through in Ruby....

No comments: