Static typing saves developer time. Dynamic typing often wastes it.
Posted on Thursday, January 22, 2009 at 12:53 AM.I just finished reading this account of a developer who spent over an hour trying to track down an unexpected application crash that was eventually determined to be due to a tuple being passed to a function that was expecting a list. This incident happened while using Erlang. For certain types of systems, Erlang is a fantastic language and platform to use. In the coming years, it is likely that its model for handling concurrency will become familiar to most developers. Unfortunately, for all of its benefits, Erlang does have some drawbacks. One of the most significant is that it is a dynamically typed language.
The main problem with dynamic typing is that it allows incidents like the one described by Travis. A simple typo, in his case involving only two characters, can quickly become an hour or more of wasted time. Were static typing, such as that offered by OCaml or Haskell, being used, the problem would have been detected immediately.
Using OCaml (via its interpreter) as an example, we'll define a function that takes a list of strings, and prints each one to stdout:
$ ocaml
Objective Caml version 3.11.0
# let f = List.iter (Printf.printf "%s\n");;
val f : string list -> unit = <fun>
Next, let's see what happens when we accidentally call it with a tuple of strings:
# f ("test", "test", "test");;
Error: This expression has type string * string * string
but is here used with type string list
It clearly has detected the mistake, and lets us know right away that there's a problem. Better yet, it tells us exactly what the problem is: it expects the argument to that function to be a list of strings, rather than the tuple of three strings that we passed it. Now that we have so quickly identified our mistake, we can go ahead and fix it. Passing in a list of strings has the expected result:
# f ["test"; "test"; "test"];;
test
test
test
- : unit = ()
#
We would have gotten similar results were we using the OCaml compiler rather than the interpreter.
Using a language that offers static typing is clearly the better route to take when we care about saving developer time, writing quality software, and detecting potential bugs and problems as soon as possible. It's unfortunate that Erlang is a dynamically typed language, as static typing would help increase the quality of code written using it even further. And it would've caught a minor mistake that has caught at least one developer over an hour of time.








