For the last couple of years I’ve heard the argument proffered by certain touring programming didacts (and perhaps the occasional narpet) that dynamically-typed languages are the future. For a while now static typing is on the outs in certain segments of programming high society--segments which also, not coincidentally, seem to worship at the feet of all things “unit testing”.
I never quite bought it, but couldn’t articulate why. Well, today I was reading Programming in Scala and found a paragraph that rang true:
The argument goes that since such type systems can only detect simple errors, whereas unit tests provide more extensive coverage, why bother with static types at all? We believe that these arguments miss the point. Although a static type system certainly cannot replace unit testing, it can reduce the number of unit tests needed by taking care of some properties that would otherwise need to be tested. Likewise, unit testing can not replace static typing. After all, as Edsger Dijkstra said, testing can only prove the presence of errors, never their absence. So the guarantees that static typing gives may be simple, but they are real guarantees of a form no amount of testing can deliver.
It makes sense. I’ve had a go at programming a complex system in a dynamic language (Ruby), and while I won’t claim my testing was perfect, I wrestled that the kinds of problems this paragraph is talking about. And I would have wrestled with them with better test coverage too. For instance, I find it very useful to get something out of an object and be able to query it (perhaps with an intelligent editor) for the methods I can call on it. With dynamic typing, once my data structures get complex I start to lose the ability to hold in my brain exactly what properties I can call on the things I get by calling properties on other things I have. This isn’t something testing will solve, because it’s a programming-time failure.
Maybe I’ve become lazy and too reliant on code completion tools. If that’s the case, I make no apologies for it. I want my static types! For this and other reasons, Scala is looking pretty attractive.
No comments:
Post a Comment