The broken promise of static typing
Tags: bugs, types
Published: 2017-06-05 17:34:43

---
title: The broken promise of static typing
published: true
description: The more advanced the type system, the less bugs that we should have.
cover_image: https://thepracticaldev.s3.amazonaws.com/i/l791grlhrpw4kd74l5sy.png
canonical_url: https://labs.ig.com/static-typing-promise
tags: bugs,types
---

*This article originally appeared on [IG's blog](https://labs.ig.com/static-typing-promise)*

I was quite surprised at a recent blog [post](http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html) 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](https://twitter.com/unclebobmartin/status/726800028598190082) ​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](https://wiki.haskell.org/Why_Haskell_just_works) 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!"

## Chartin​​​g bugs​​

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](http://danluu.com/empirical-pl/) 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.

In green, in the "advanced" static typed languages corner: Haskell, Scala and F#. 
In orange, in the "old and boring" static typed languages corner: Java, C++ and Go. 
In red, in the dynamic typed language corner: JavaScript, Ruby, Python, Clojure and Erlang.

**Round 1.** Lang​​​​uages sorted by bug density. All repos

![round1](https://labs.ig.com/sites/default/files/bug-density-no-filter.PNG)

**Round 2.** Language​s​​ sorted by bug density. More than 10 stars repos

![round2](https://labs.ig.com/sites/default/files/bug-density-5-stars.PNG)

**Round 3.** Languages sorte​d by bug density. More than 100 stars repos

![round3](https://labs.ig.com/sites/default/files/bug-density-100-stars.PNG)

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 th​​e 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](https://www.youtube.com/watch?v=rFejpH_tAHM) (Go creator) and [Rich Hickey](http://www.infoq.com/presentations/Simple-Made-Easy) (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](http://zoo.cs.yale.edu/classes/cs422/2011/bib/hoare81emperor.pdf):

> 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.