Saturday, October 01, 2005

Where Dynamically Typed Languages Fall Short

In the past, dynamically typed languages were considered to be more productive than statically typed ones, and I used to agree with that view. My past experience has shown me that Python, a dynamically typed language, is more productive than Java (statically typed). Somehow Python code feels more malleable and pliable, and making it do what you want is more hassle-free. For example, if you want a hashtable, you just write
map = ["R": "red", "B": "blue", "G": "green"]
instead of Java's tedious
HashMap map = new HashMap();
map.put("R", "red");
map.put("B", "blue");
map.put("G", "green");
I think the convenience of writing Python code can be attributed in equal amounts both to its elegant syntax, and to its dynamic types.

But now the landscape is changing. First, the introduction of the new Java language features in Java 5.0 makes Java code a lot more elegant. Second, and more important, is the appearance of really smart IDEs such as Eclipse. Zef Hemel has already noted that statically typed languages allow IDEs to offer features such as refactoring and code completion. But it goes even further than that.

When you open a Java source file in Eclipse, what you get is an extensively hyperlinked document. Ctrl+Click on any variable, method or class name brings you directly to its definition. Ctrl+G finds all the uses of the same. You can easily navigate to the superclass' implementation of any overridden method, or to all the subclasses that override it themselves.

These invaluable capabilities are made possible by the fact that the IDE knows everything, or more specifically - the type of every variable in the source code. And this is of course impossible in a dynamically typed language, since it's, well, dynamically typed. Consider the following Python function:
def func(x):
    x.doSomething()
The IDE has absolutely no way of knowing the type of x, and in fact it can be of any type that has a doSomething method. So inevitably, IDEs (and other tools) for statically typed languages can be much more advanced than IDEs for dynamically typed languages.

These advanced features - code completion, automatic refactoring and hyperlinked source code - greatly improve the developer's productivity, thereby closing the gap between dynamically and statically typed languages.

0 Comments:

Post a Comment

<< Home