Sunday, March 25, 2007

What's with type safety, ducks are a type that don't bite

Last week I was at SD West conference in Santa Clara and attended some talks on Ruby. In one of them by Joe O’ Brien someone asked about the comparison of static v/s dynamic typed language. This perhaps is one of the most common debate that occurs when you have a room full of Ruby and Java developers with an even split.

I will not go into the details of what transpired there but as you can imagine it was as much religious as technical.

I have coded in many languages and have been on both sides of the argument at different times. After 9 years of Java I had this thought ingrained in my mind -

Compiler is my friend. Static typing saves the day (and night) and dynamic typing is either for those frivolous javascripty web coders or for the not so serious fringe scriptable use cases

After almost 6 months with Ruby I am a changed man. In fact now that I look back I feel like a child who just feared venturing in dark, imagining ghosts where there were none.

Static typing is so overrated. Dynamic typing opens a whole world of possibilities hitherto locked out by inflexible attitude.

Here is a Java snippet


public class Type {
public static void main(String args[]) {
for(int i=1, x=3; i<30; i++)
x *= 3;
System.out.println(x);
if (x<0)
System.out.println("I'm so busted");
System.exit(-1);
}
}
}
}

And here is some Ruby code -


x = 3
30.times do
puts "#{x} #{@y = x.class if @y != x.class}"
x *= 3
end

Now decide for yourself which of these is "safer".

No comments: