Monday, September 10, 2007

Benefits of optional type declarations

Ola Bini wonders if optional type declarations have a place in Ruby. He notes that Common Lisp programmers can use compiler directives to assist the compiler. This reminds me of a good, old Knuth quote (and not the one about premature optimization):

For some reason we all (especially me) had a mental block about optimization, namely that we always regarded it as a behind-the-scenes activity, to be done in the machine language, which the programmer isn't supposed to know.


The programmer [...] will write his beautifully-structured, but possibly inefficient, program P; then he will interactively specify transformations that make it efficient. Such a system will be much more powerful and reliable than a completely automatic one

The standard approach to performance problems in Ruby seems to be rewriting modules in C. What if we could have some of the performance gains of C in an easier-to-maintain Ruby dialect. I understand Squeak uses a similar approach, with much of the VM written in a Smalltalk subset. The code can then generate a binary image or be run in emulated mode on an existing Squeak instance.

The human side

Humans can also benefit from declaring types. While omitting type declarations is generally touted as an advantage of dynamic languages, some have commented that it can make code harder to read:

One day I found myself trying to follow some well-written Ruby code. I found the lack of type information on parameters made life difficult - I kept saying to myself 'what exactly do I have here?'

--- Martin Fowler, DynamicTyping

Other languages

It's interesting how the line between dynamically and statically typed languages increasingly blurs. Java now has several proposals for type inference. Python 3000 has a PEP for function annotations. It will be interesting to see how different language implementations continue to influence each other.

No comments: