Wednesday, April 14, 2010

Static Typing is the Root of All Evil

I can't take credit for this statement, since I am only drawing the logical conclusion from a statement said by a man far smarter than I.

Donald Knuth wrote in his famous paper Structured Programming with go to Statements (PDF):

We should forget about small efficiencies, say about 97% of the time: pre-mature optimization is the root of all evil

Since compilation is premature optimization, it is therefore, the root of all evil. Simple!

Static typing, weak typing, dynamic typing, strong typing, WTF?

There are a number of misconceptions concerning typing.

The meaning of weak and strong typing, is not clearly defined but is mostly interpreted like this.

In a weakly typed language, such as C, the types of the variables can be automatically converted from one representation to another, while in a strongly typed language an explict conversion is required.

This is not what I am writing about here. I am referring to static vs. dynamic typing and the definition I am using is:

  • Static typing, the type checking is performed at compile-time.
  • Dynamic typing, the type ckecking is performed at run-time.

Development Mode

Every time I build my code, my entire code base is type-checked. EVERY TIME! When I am working in development mode, I only care about the method and class that I am currently working on. As long as my tests pass I am happy, type-safe or not.

In development mode it is actually more accurate to call static typing pre-mature de-optimization than pre-mature optimization.

Production Mode

The only reason, to ever use static typing, is because the code may be optimized better, to give better performance.

So, the strategy should be:

  • Use a dynamically typed language during development. It gives you faster feedback, turn-around time, and development speed.
  • If you ever get lucky enough to have performance problems, which cannot be solved with caching or algorithmic optimization, rewrite the problematic code in a statically typed language.

This is what Twitter did, and it has worked well for them.

Other Misconceptions

Proponents of Java often have the misconception that dynamically typed languages are unsafe, yet they use frameworks, like Spring, and Qi4J, that are littered with reflective code, and without the so-called safety net that static typing is believed to give. The reason these frameworks are popular is because they allow us to be more productive. The reason they are more productive is because they use dynamic programming techniques.

Anyone, who has worked seriously with a modern dynamically typed language like Ruby or Smalltalk, know that they are more productive. Working with waterfall languages after working with agile languages is just painful. (Thanks to Andreas Ronge for coining the term Waterfall Language.)

5 comments:

Unknown said...

It's way too heavy statement to say that "all evil" comes from static typing. Of course a post like this is an exaggeration but I'd like to give you a few points that you seemed to miss.

1) Compilation is not only optimization
It's mostly type checking! If your build fails, you have something wrong. If you use dynamic typing, you have to run the actual software to know whether it's wrong or not.

2) In decent development environments when you are compiling you the entire code base don't have to be type checked. Only the code that has changed after latest build.

3) Static typing does not enforce development-time compilation process
For example, in ASP.NET (and even before version 4.0 which allows you to use real dynamic typing) you can write code into .ascx/.aspx files which are compiled only at runtime.

Here is an interesting discussion about what The Strategy should be: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.69.5966&rep=rep1&type=pdf

Anders Janmyr said...

@Sami
1. I know, but my point is that it is done to early and to often. In the development projects I'm working in, requirements change very often and I am far more productive without the compilation phase.

2. Yes, but it still takes time, and I think it is wasted time.

3. I wish "edit and continue" worked better, then it would be less of an annoyance to me.

I have read Eric Meijer's article before, and it is one of my favorites. Thanks for pointing it out to me again. It is well worth another read.

Mark E. said...

@Sami, @Anders Janmyr,

1. Recently I've been working on an an existing ASP.NET web application that is quite large. I'm not kidding when I say that sometimes, when I make a change and save it, I have to wait minutes for it to dynamically compile the project so I can see the changes to the one page I'm working on. At that point I get bored and task switch forgetting about it for awhile before coming back. A major productivity killer.

Also, with #1, compilation does not mean it works correctly! How many times has code been checked in that is broken but, "It compiled!" is the defense. The very fact you point out here about discovering errors through the compiler (while only partially helpful) is why dynamic languages like Ruby and Rails (the web framework) do so much better. The Rails framework strongly encourages you to create tests. Rails makes it easy to test. When I make a change and I want to test for unintended consequences, I run my tests.

I have not found this to be easy or straight forward in ASP.NET. Since TDD and the tests aren't as easy to write in ASP.NET (not a standard default either), I'm left using custom testing tools and/or relying on a QA department and/or spending my own time visiting all the pages that could have conceivably been affected by a change.

Massive productivity killers.

Dynamic typing makes more sense to me too.

Anders Janmyr said...

Mark: I'm glad that you agree, this post was a bit controversial, but my point was, like you wrote, that compilation and everything else that comes with static typing, are massive productivity killers.

Anders Janmyr said...

@Anonymous, It's the second time my writing has been called moronic today, I guess you must be on to something ;)

I agree that the post is provocative, but I would hardly call it moronic. But, my hat is off to you anyway for reading the whole post even though you disagree with it, the other guy stopped reading after a few paragraphs.

If you would like to read a similar post, but less provocative I can recommend Dynamic for the Win and, if you like ASP.MVC, I can recommend ASP.NET MVC vs. Rails3