Wednesday, July 11, 2007

In Defense of Static Typing

Just thought it may be relevant to bring this thread up in context of the debate going on in the Artima forum between the camps of static and dynamic programming languages. The languages under discussion have changed, the thought leaders have changed, however, the topics debated have remained the same .. the verbosity of the static type system, benefits (if any) of type inferencing, power of dynamic typing and the defense of the straw man argument.

Here are some snippets :-

From Matthias Felleisen (of The Little Schemer and the rest of the series fame)

Static types are necessary
(1) to establish basic properties of software and
(2) to establish absolute yet cheap abstraction boundaries between modules.

and more of some of the basic values of static typing ..

With types, you can say something like this at a module boundary:

there is a type t, here are some instances of type t, i even give you functions to create t's, and work with t's, but i won't tell you what they are implemented in.

Now you or your partners have no way of corrupting these values. And you know what, they are actually integers, and if you had known, you could have used all kinds of numeric hacks to compute values. But fortunately you didn't, because I, the module writer, can now change t to a hash table and do all kinds of nice things with it.

What's the cost for that? Zero. Yeap, in a statically typed world, there is no run-time cost.

and of course there is Paul Graham on the opposite camp .. regarding static typing ..

It's like the way the military does things. Making everyone fill out forms to do anything certainly prevents mistakes. But it also inhibits innovation terribly.

I'm not saying the military is mistaken here. There are places where restrictions are good-- particularly when you don't trust individual people not to screw up. So requiring type declarations or other assertions (e.g. all the boilerplate that seems to be required in Java) could be a good idea in some big company where the programmers are guys who walked in off the street after Learning Java in 21 Days. But at the other end of the continuum, there is also a role for languages that put as few constraints on the programmer as possible.

Enjoy the complete thread here !

I think I would like to have some militarism in contracts when I am executing a big-bang enterprise project. As Cedric has rightly mentioned,
nothing beats a dynamic language to write a prototype, but if you know that the code will have to be maintained and that it will be in use for several years, the initial cost of using a statically typed language is an investment that is very quickly amortized...

Types are elements promoting Design By Contract at the lowest level and I would not want to lose the enforcements that they bring to my higher level abstractions. And finally, the refactoring support that I get from statically typed languages is way too precious to lose when I am working on a large codebase.


jason said...

Partly prompted by that Artima discussion, I blogged about some of the fallacies re: dynamic typing and productivity yesterday. There are a lot of false arguments in the air at the moment.

Anonymous said...

Your blog is not readable with FireFox.

Anonymous said...

see - i like static typing and i choosed java for my projects. if You do not like static typing (or miss templates, properties, unchecked exceptions, preprocessor, etc), just pick another language and use it happily. why do you need explain me what's wrong with my tool??