Type errors in Dynamic Typing
This post has raised quite a conversation. As it happens, there's a similar thread over on cls. Peter Lount has also posted a response. I responded to the classic "what about bad message sends" argument like this:
I see this argument all the time. In practice, I rarely see Smalltalkers having issues that look tlike that. The most common reason for MNU (MessageNotUnderstood) in Smalltalk is sending a message to nil - which happens due to a failure to initialize.
So you can raise this argument all you want, and most Smalltalkers (and I suspect other dynamic language users) will yawn - because it's simply not a problem we see.
Personally, in coding I've done over the last decade in Smalltalk, I've had a problem that static typing would have solved for me... twice. That's right, twice. I recall the number because it's been so uncommon. Static typing does impose costs - in flexibility, in time to market, and in maintenance down the road. To my mind, given the frequency of actual type errors that static typing would have helped me with - that cost is not worth it.
I'll go further than that though - here's one of the more hysterical comments in my post thread:
Dunno what sort of world you're programming in... but lets see... four extra days of programming time versus losing $1,000,000 on that bank transfer... or not notifying the customer of their flight change... or landing that plane 4 metres BELOW ground level hmmmmm... in programming, safer == better. ALWAYS. Because the impact of extra programming time is known and controllable. The impact of an unexpected bug is NOT.
Sure, I can't protect against ALL problems. But I CAN protect against a car driving down the footpath. Any protection is better than no protection at all.
This reminds me of nothing so much as political arguments that rely on "what about the children" for their backing store. It means that you don't actually have an argument, so you've decided to make an emotional case instead. I give such arguments all the time they deserve....


Comments
...
[Ian Bicking] August 13, 2004 12:51:02.162
My retort is sometimes: if it takes you four less days to finish the program, that's four more days to do testing and protect against all the bugs that static typing won't save you from either. And empirically it's clear that static typing doesn't save you from bugs, because we've all spent way too much time working with buggy programs written in statically typed languages.
In the end, it takes work to write high-quality code, no matter what your language. Similarly, it takes work to write fast code in any language. Some languages require more work. Dynamic languages don't take more work to write high-quality code. They do usually take more work to write fast code. But in the end you have a certain set of requirements: basic requirements, extra features, code quality, user experience, etc., and you make tradeoffs based on the resources you can put in. A good language is one where the resulting program -- constrained by resources, judged by the weighted qualities you are looking for -- is a better program. And the better language is the one that lets you make better programs.
absence of evidence
[Isaac Gouy] August 13, 2004 13:54:18.046
"The most common reason for MNU (MessageNotUnderstood) in Smalltalk is sending a message to nil - which happens due to a failure to initialize."
Was that the cause of every VW7.2 MNU fixed in VW7.2.1 - sending to nil?
it's simply not a problem we see
I've seen folk send a message to an object that didn't implement it.
I've seen folk use method args which weren't supported by the method (which always surprises me - you'd think keyword message selectors and anInteger arg names would be enough).
problem that static typing would have solved for me... twice
What are the errors you make, the ones that static checking wouldn't help you with? Do you think they are the same errors made on multi-person projects?
[verbat] August 13, 2004 14:29:29.788
given that sending a message to nil is an error that can be caught at compile time, and that will save you time, why would you avoid it?
If I was going to give you a compiler that compiles all your smalltalk code *except* those that generates MNU (yet allowing it if doesNotUnderstand: is implemented) would you want it?
So why can't DL guys accept the concept that they just want an *expressive enough* type system ?
WOW!
[Isaac Gouy] August 13, 2004 15:55:14.777
Will Java fluent readers be impressed by Peter's advocacy?
(or will they just say "We can do that!")
HashSet aSet = new HashSet(); try { aSet.add( Class.forName("java.lang.Integer") ); aSet.add( Class.forName("Ball") ); aSet.add( Class.forName("Chainage") ); aSet.add( Class.forName("FooBar") ); } catch (Exception e){ System.out.println(e); } if (aSet.contains( aVariable.getClass() )) { } else { }Re: Type errors in Dynamic Typing
[ James Robertson] August 13, 2004 16:42:43.650
Comment on Type errors in Dynamic Typing by James Robertson
verbat,
The problem is that the cost of static typing (in terms of expressiveness and flexibility - is higher than the benefit. Having used the common static languages (Java, C, C ) and Smalltalk, I can tell you that the cost isn't worth it.
Dynamic Languages Permit and Benefit from Multi-Type Variables
[Peter William Lount] August 13, 2004 18:11:31.824
Verbat I answer your question here.
Thanks for the example Issac
[Peter William Lount] August 13, 2004 18:51:20.426
Nice counter example Issac. I respond here.
Re: Type errors in Dynamic Typing
[ Michael Lucas-Smith] August 13, 2004 19:41:01.728
Comment on Type errors in Dynamic Typing by Michael Lucas-Smith
It's been once for me so far. The problem was this: I wanted to erradicate all floats replaced with integers from the complex math I was doing. It's hard to do this in a dynamic language. But later I realised that I didn't need to do it, so I guess you can say 'zero'.
Addendum
[Isaac Gouy] August 13, 2004 21:41:07.076
Peter: No big "WOW" Issac!
The name's Isaac, and the big "WOW" is a quote from your original article - "Done in less than 60 seconds!!! WOW!"
Peter: How long did it take you to write the Java example Issac?
The name's Isaac, and it took about 10 minutes because I'm fluent in Smalltalk not Java, so I had to look the methods up in a book.
Peter: Also, your example is incomplete, what type is your "aVariable" defined as?
The original ST example has no variable declarations for aVariable or aSet (or class definitions for Ball, Chainage, FooBar).
Peter: more advanced techniques such as "double dispatching"
Well, double dispatching is just a manual simulation of multiple dispatch - Java programmers can do that as well.
Or they can use an advanced multiple dispatch language - like Nice, MultiJava, Half'n'Half.
wow
[Peter William Lount] August 13, 2004 23:48:08.557
I stand corrected about the spelling of your name. Fixed.
I've added my comments into the article after the questions and at the end, here.
purely at compile time
[Isaac Gouy] August 14, 2004 16:45:21.939
(apologies for these digressions)
Peter: what type is your "aVariable" defined as?
Any reference type.
Peter: care must be taken in Smalltalk ... to not over constrain the program
And to not under-constrain the program.
Peter: Again Java sticks to "compile time" solutions
"Static" languages stick to "compile time" solutions except when they use "run time" solutions.
class A { String id(){ return "A"; } } class B extends A { String id(){ return "B"; } } public class Test2 { public static void main(String args[]) { B b = new B(); A a = b; System.out.println( a.id() ); } }