Tuesday, December 11, 2007

Something about Ruby

I know there are some things I don't like about Java and that it's missing some useful features but the Ruby crowd seem to be disproportionately vocal about how amazingly easy and quick (in terms of typing) things are in Ruby. In doing so they make themsevles wide open to criticism. One of the most frequent claims I hear about Ruby is how the syntax reads naturally and makes it obvious what's going on.

Yeah, right.

If I want to create a 'new object', I have to write 'object new' like this:

foo = Foo.new(...params...);

In Java it reads more naturally :

foo = new Foo(...params...);

The gap between the classname and the parentheses that is created by having the 'new' keyword between them detracts from the most important elements of the statement. Having the 'new' keyword at the beginning makes it more obvious what is going on.

Iteration suffers from the same problem. The placement of the keyword after the variable name makes it harder to see that this is in fact a loop:

@names.each do name
puts "Hello #{name}!"
end

It's not until your eye is half-way through the first line that you realise that this is a loop. Compare this with Java:

for (name : names) {
System.out.printf("Hello %1$s!", name);
}

Much clearer.

Ruby does have an apparently neat way of looping using integers like this :

4.times do
puts "Hello"
end

My problem with this is that it introduces a new keyword for iteration. The Java designers considered having 'foreach' instead of the enhanced for loop and rejected it for that precise reason. Why make the language bigger than it needs to be with too many keywords? Couldn't they have used '4.each do' instead?

A strange omission from Ruby which would have made a lot of sense and would be in keeping with its syntax is how you find the square root of a number. It's the same in Java as it is in Ruby:

a = Math.sqrt(9);

Why didn't they implement it like this?:

a = 9.sqrt();

While you can write compact code with Ruby, as you can in a lot of scripting languages, this doesn't mean it's a good thing to do. Sometimes a bit of verbosity is useful, as long as it's not excessive. The syntax of Ruby is a jumbled, inconsistent mess. It seems that is was based on a vaguely similar syntax to Java but with seemingly random changes which the authors thought were improvements. Instead, they've made the language so that a lot of statements read backwards in english. For example, 'object new' instead of 'new object' as I've already pointed out. Blocks, loops and methods have to end with the 'end' keyword (and sometimes start with 'do') but it's far easier to see blocks of code if they are delineated with the symbols { and } instead of having to read a whole word. The brain is quicker at understanding single symbols than reading words.

I could go on and on. While Ruby does have some nice features that I would like to see in Java, on the whole it's not a language that is suitable for large scale developments continuing over a number of years with dozens of different developers being involved over its lifetime. Oh and don't get me started on Ruby on Rails and it's appalling HTML markup syntax which is unneccessarily verbose and difficult to read. As for speed of development, well anyone can write a blog application if someone's already written a script to generate it for you.

Labels: , , , ,