Ravi Mohan's Blog

Thursday, October 09, 2003

Ruby vs Scheme vs Java ... and the winner is ..

Well the loser is java .

Some context

Martin Fowler was kind enough to send me some ruby code he had written to generate his website. Since I didn't know anything about Ruby, I had to learn it fast in order to understand what the code was doing. I learn best by doing. So the moment I understood a little bit of what he was trying to do , I immediately wrote it out in Scheme, language i did know.Later I tried writing it out in java , just to see how much of a difference there really was in code size.

The java code came to about 300 lines , the ruby code had about 70 lines and the scheme code had around 20.The scheme code "cheats" a bit bacause i used a tree walker macro i have been working on for sometime.Since a macro "expands" at runtime, the scheme code when fully expanded would be about 50 odd lines ...Also I am sure the ruby code could be much shorter but i am a novice @ Ruby Programming ! Eclipse was Java's key advantage, but on a code base this small, an ide is not too important. More than "number of lines" both the scheme and ruby code were very clear and the java code had lots of casts,enumerations, iterators etc. which put a barrier between your mind and the code. I didn't really believe it when Paul Graham said "Succinctness is Power" but I am slowly veering around to this view, even though i still don't think macros are the best ways to provide succinctness .

If Ruby or Scheme or Lisp ever get the kind of awesome refactoring and debugging support Java gets from eclipse,software would be much more fun. That reminds me i need to look into how exactly Eclipse plugins work .....

More importantly, I learnt a number of things from Martin's code.

First, keep it simple.Where i would have used XPATH to navigate an xml tree, Martin used simple recursive tree descent.Lesson learned == Don't use a library just because it's there.

Second, when learning a new language,it is much better to modify/otherwise extend an excellent piece of code rather than start with "Hello World". You get to pick up the idioms (and other "good practices" of the language )faster this way For eg : I extended Martin's code to use a templatefile (a series of template files actualy one for the main site layout and one each for each"document type") ,instead of directly rendering to html, mainly because I can't "think in html" ! Lazy coder that i am got my friend Manoj to whip up a template "look and feel" for me and i just point at the template from my code, and i did this keeping a copy of Andy Hunt's ruby book at my side to clear any ruby road blocks.

Third, read read good code. Programmers, even the good ones don't read as much code as they should.I ,for one, have decided to read at least 10-25 lines of code a day, in addition to writing code daily.

This seems to be one of those blindingly amazing (in retrospect) ideas,that hit you over the head so hard you wonder how you never thought of it. Tomorrow, i'll add an RSS feed to the blog.I need to refactor the code a lot too .

No comments: