I
like static (manifest) typing. This may come as a shock to those who
have read other posts of mine, but it is true. I certainly am more
comfortable with having a
MPWType1FontInterper *interpreter
rather than
id interpreter. Much more comfortable, in fact, and
this feeling extends to
Xcode saying "0 warnings" and the clang static analyzer agreeing.
Safety
The question though is: are those feelings actually justified? The rhetoric
on the subject is certainly strong, and very rigid/absolute. I recently had a Professor
of Computer Science state unequivocally that anyone who doesn't use static typing should
have their degree revoked. In a room full of
Squeakers. And that's not an
extreme or isolated case. Just about any discussion on the subject seems to
quickly devolve into proponents of static typing claiming absolutely that
dynamic typing invariably leads to programs that are steaming piles of bugs and crash left
and right in production, whereas statically typed programs have their bugs
caught by the compiler and are therefore safe and sound. In fact, Milner has supposedly made the claim that "well typed programs cannot go wrong". Hmmm...
That the compiler is capable of catching (some) bugs using static type checks
is undeniably true. However, what is also obviously true is that not all bugs are type
errors (for example, most of the 25 top software errors don't look like type errors
to me, and neither goto fail; nor Heartbleed look like type errors either, and neither
do the top errors in my different projects),
so having the type-checker give our programs a clean bill of health does not
make them bug free, it eliminates a certain type or class of bugs.
With that, we can take the question from the realm of religious zealotry to the
realm of reasoned inquiry: how many bugs does static type checking catch?
Alas, this is not an easy question to answer, because we are looking for something
that is not there. However, we can invert the question: what is the incidence
of type-errors in dynamically typed programs, ones that do not benefit from the
bug-removal that the static type system gives us and should therefore be steaming
piles of those type errors?
With the advent of public source repositories, we now have a way of answering that
question, and Robert Smallshire did the grunt work to come up with an answer: 2%.
The 2%
He talks about this some more in the talk titled
The Unreasonable Effectiveness of Dynamic Typing, which I heartily recommend. However,
this isn't the only source, for example there was a study with the following title:
An experiment about static and dynamic type systems: doubts about the positive impact of static type systems on development time (
pdf), which found the following to be true in experiments: not only were development times significantly shorter on average with dynamically typed languages, so were debug times.
So all those nasty type errors were actually not
having any negative impact on debug times, in fact the reverse was true. Which of
course makes sense if the incidence of type errors is even near 2%, because then other factors
are almost certain to dominate. Completely.
There are more studies, for example on generics: Do developers benefit from generic types?: an empirical comparison of generic and raw types in java. The authors found a documentation benefit, no error-fixing
benefits and a negative impact on extensibility.
Others have said it more eloquently than I can:
Some people are completely religious about type systems and as a mathematician I love the idea of type systems, but nobody has ever come up with one that has enough scope. If you combine Simula and Lisp—Lisp didn’t have data structures, it had instances of objects—you would have a dynamic type system that would give you the range of expression you need.
Even stringent advocates of strong typing such as Uncle Bob Martin, with whom I sparred
many a time on that and other subjects in comp.lang.object have now come around to this
point of view: yeah, it's nice, maybe, but just not that important, and in fact he
has actually reversed his position, as seen in this video of him
debating static typing with Chad Fowler.
Truthiness and Safyness
What I find interesting is not so much whether one or the other is right/wronger/better/whatever, but rather
the disparity between the vehemence of the rhetoric, at least on one side of the
debate ("revoke degrees!", "can't go wrong!") and both the complete lack of empirical evidence
for (there is
some against) and the lack of magnitude
of the effect.
Stephen Colbert coined the term "truthiness" for "a "truth" that a person making an argument or assertion claims to know intuitively 'from the gut' or because it 'feels right' without regard to evidence, logic, intellectual examination, or facts." [Wikipedia]
To me it looks like a similar effect is at play here: as I notice myself, it just feels
so much safer if the computer tells you that there are no type errors. Especially if it
is quite a bit of effort to get to that state, which it is. As I wrote, I notice that
effect myself, despite the fact that I actually know the evidence is not there,
and have been a long-time friendly skeptic.
So it looks like static typing is "safy": people just know intuitively that it
must be safe, without regard to evidence. And that makes the debate both so
heated and so impossible to decide rationally, just like the political debate on
"truth" subjects.
Discuss on Hacker News.