The broken promise of static typing
The more advanced the type system, the less bugs that we should have.
This article originally appeared on IG’s blog
I was quite surprised at a recent blog post by Uncle Bob Martin, titled: “Type Wars”, in which he writes: “Therefore, I predict, that as Test Driven Development becomes ever more accepted as a necessary professional discipline, dynamic languages will become the preferred languages. The Smalltalkers will, eventually, win.”
This statement didn’t sit well with some people in the static typing community, who argued that in a sufficiently advanced statically typed language, types are proofs and they make unit tests mostly redundant. Haskell even claims that “once your code compiles it usually works”!
Be it safer refactoring, better documentation, more accurate IDE support or easier to understand, for me all these claims translate to a simple promise: less bugs.
And I really hate bugs. I find them to be one of the worst wastes of time and energy for a project, and there is nothing that annoys me more than getting to the end of iteration demo and the team being somehow proud of saying, “We did X story points and we fixed 20 bugs! Hurray!”
To me it sounds like, “In the last iteration we wrote more than 20 bugs, but our clients were able to find just 20! And we were paid for both writing and fixing them! Hurray!”
With that in mind, I tried to find some empirical evidence that static types do actually help avoid bugs. Unfortunately the best source that I found suggests that I am out of luck, so I had to settle for a more naïve approach: searching Github.
The following are some charts that compare the “bug density” for different languages. By bug density I mean the average number of issues labelled “bug” per repository in GitHub. I also tried removing some noise by just using repositories with some stars, on the assumption that repositories with no stars means that nobody is using them, so nobody will report bugs against them.
Round 1. Languages sorted by bug density. All repos
Round 2. Languages sorted by bug density. More than 10 stars repos
Round 3. Languages sorted by bug density. More than 100 stars repos
Whilst not conclusive, the lack of evidence in the charts that more advanced type languages are going to save us from writing bugs is very disturbing.
Static vs Dynamic is not the issue
The charts show no evidence of static/dynamic typing making any difference, but they do show, at least in my humble opinion, a gap between languages that focus on simplicity versus ones that don’t.
Both Rob Pike (Go creator) and Rich Hickey (Clojure creator) have very good talks about simplicity being a core part of their languages.
And that simplicity means that your application is going to be easier to understand, easier to change, easier to maintain, and more flexible. All of which means that you are going to write less bugs.
What characterizes a simple language? Listing the things in common between Go, Erlang and Clojure, we get:
- No manual memory management
- No mutex-based concurrency
- No classes
- No inheritance
- No complex type system
- No multiparadigm
- Not a lot of syntax
- Not academic
Maybe all those shiny things that we get in our languages are actually the sharp tools that we end up hurting ourselves with - creating bugs and wasting our time - and that all they do is bring a lot of additional complexity, when what we really need is a simpler language.
As Tony Hoare said:
There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.