Chiming in on static versus dynamic typing
August 12, 2005
There's a large debate about static versus dynamic typing in the programming and language communities. (There is a classic Bruce Eckel article on this here, for example.)
I spent part of today fixing some 'issues' in DWiki, which is written in Python. This gave me the opportunity to reflect about the sort of bugs I've been finding in it. Python is dynamically typed, and I don't have any sort of formal test system for DWiki. In theory, this should lead to disaster.
In practice, type bugs in DWiki have been rare and found rapidly. Most bugs are like the ones I spent yesterday on: things where what was wrong was not my syntax or my programming, but my logic. I hadn't done something wrong, like passing a variable of the wrong type to a function; I had failed to think everything through correctly, and the code was quite faithfully carrying out my flawed logic, with equally flawed results.
(Indeed, just now I discovered that my revised logic for yesterday's fix was still a little bit wrong.)
This isn't too surprising. In a strongly typed language like Python, a type mistake is likely to cause an immediate error of some sort: either an exception or a clearly and majorly wrong result. If you test a code path at all, even informally by seeing if a given feature works, you're probably going to see those problems immediately.
As for their rarity, I think type errors are rare even in statically typed languages. Most of the time we get the obvious programming stuff right regardless of the language we're using; this includes types just as much as it does language syntax and the number of arguments a function takes. (If we routinely got static types wrong no one would put up with manual type declarations; it would be too much work. Even as it is, automating them is one popular IDE function.)
(I'm not even sure unit tests would have caught this particular issue, because I'm not sure it would have occurred to me to unit test the particular set of circumstances that showed the erroneous logic. I spotted the issue only by being puzzled by something in the request logs for CSpace and then working backwards to how MSN's spider must have obtained the particular URLs it was crawling.)
Sidebar: A brief summary of the positions
To brutally summarize the arguments between the two camps, static typing people believe it finds bugs and are horrified by the thought of latent type incompatibility bugs lurking unfound in programs in dynamically typed languages; how are you sure that all of your routines get the right type of argument every time? Dynamic type people retort that in practice this doesn't happen, that you develop much faster and refactor much easier without static typing getting in the way, and recite 'if you didn't test it, it doesn't work' a lot.
The Bruce Eckel article has a longer discussion of this.
* * *
Atom feeds are available; see the bottom of most pages.