24 Feb 2013

The comeback of static typing

I used to loathe static typing. From PHP to JavaScript to Python to Ruby, I was pretty much a dynamic typing fan boy. I was working primarily on web applications with very straightforward business logic using frameworks like CodeIgniter and Django.

These were some of the reasons why I hated statically typed languages then:

  • Verbosity – the presence of a type system meant that I needed to type more
  • Difficult to prototype
  • Compilation – time spent on waiting

Over time, not only have I learned to appreciate languages with good type systems, I actually now prefer them for solving certain classes of problems.

Java and C++ (the languages through which I experienced static typing) are not the best representatives of statically typed languages. It’s only after I started diving into Scala, did I start discerning between good and bad type systems. A good type system should not get in your way and should definitely not make you type more than it’s necessary. I have also been dabbling a bit with Go and Rust, and I’m excited about how productive I feel in both of them.

Type inference goes a long way in saving keystrokes, but a powerful type system can offer much more than that. For example, let’s take Scala’s structural types:

  def speakWith(animal: {def speak: String}) = println(animal.speak)
  class Dog { def speak = "bow!" }
  class Cat { def speak = "meow!" }
  class Man { def walk = "walk!" }
  speakWith(new Dog)  // "bow!"
  speakWith(new Cat)  // "meow!"
  speakWith(new Man)  // compile error

Structural types [1] eliminate the need for an abstract class or an interface/trait. I can simply define a function speakWith which takes an argument (animal) which is of type {def speak: String}, i.e. any object that has a speak method defined in it.

I have heard arguments that such a type system is complex for the average developer to grok. I used to be one of them, but I’m now convinced that most of this talk about complexity comes from forgetting that there is a learning curve for any tool. One is bound to be unproductive during that time.

Contrary to conventional thinking, I actually find statically typed languages to be good for projects that undergo a lot of churn, either because the problem requires an experimental approach or the requirements are hazy (as with the case with a lot of start-ups). Although it’s much easier to start hashing things out in a dynamically typed language, I noticed that as the codebase became larger, it becomes much harder for me to feel confident about the various changes I was making without the presence of proper tests. And, tests are painful to maintain when there is a lot of churn happening. Static typing does not replace tests, but it definitely catches a lot of stupid errors and makes refactoring code really easy. Apart from better IDE support, you also get the confidence that when the code compiles, you can be reasonably sure that atleast syntactically things are alright.

With respect to compilation time, Go is pretty fast even when compiling large projects, but the same can’t be said about Scala (C++ is also notoriously slow). However, using a SSD has helped a lot, atleast in absolute time. Still, even if the compilation phase robs me some time, it’s a trade-off I’m now willing to make if a statically typed language gives me better confidence about the correctness of my programs, eventually leading to less time spent on bug fixing.

A few years back, one big lure of Python or Ruby used to be the functional constructs that they offered like higher order functions. However even Java has now caught up, with Java 8 introducing lambda expressions and an enhanced collections API. Hence, moving forward, I believe I will find very little reasons to hate static typing as much as I used to. It’s really exciting to see how languages like Rust and Scala approach type safety without compromising on productivity. I’m going to continue to use Python, Javascript and Ruby where it makes sense (either because of the community or the libraries), but dynamic typing is no longer my number one reason to use them.

[1] – thanks to Mushtaq for introducing me to Scala’s structural types.

You can follow me on Twitter right here.

Tagged with:

23 people have responded to this post.

Leave a Reply