development

Dynamic vs. Weak

November 17, 2004 18:09:35.895

I've been meaning to comment on this post on Cafe Au Lait concerning dynamic typing. The problem (for me), is that I don't know PHP, so I wasn't entirely certain where he was coming from:

For the last couple of days I've been programming in the weakly typed language PHP. For the last few years I've been hearing quite a few people, most notably Bruce Eckel and James Robertson, extol the virtues of weakly typed languages. So far I'm not convinced. I've repeatedly found myself running up against bugs that simply would not have been possible in Java, or that would have been caught almost immediately by the compiler, misspelled variable names for example. I just got hit by another one. I was writing $myarrayindex instead of $myarray[$index]. There goes another ten minutes of debugging that I would not have had to do in Java.

Well, this post from Nicholas Lehuen gave me enough information to go on. As it happens, it looks like PHP is weakly typed (along the lines of C, but with more holes). That's not really the same thing we have in Smalltalk, for instance. I'll let Nicholas explain:

PHP sucks badly. So do any language in which "1"+2==3 or even "1"+2=="12" (plus, there are a dozen other reasons why PHP sucks, see http://nicolas.lehuen.com/index.php/2004/11/12/8-why-php-sucks). This does not happen in Python. In fact, I make a difference between weakly typed languages in which "1"+2==3 (bad languages), and dynamically typed languages, in which "1"+2 raises an exception. The former is guaranteed to make you mad. Implicit casting (over than for numeric types) combined with weakly typed variable is the perfect recipe for a disaster. Any kind of silent failure when the interpreter does not get what you want to do is a crime, and PHP does this a LOT.

Yeah, Smalltalk doesn't have that issue either. '1' + 2 raises an exception - MessageNotUnderstood. At least in Smalltalk, we have strong - but dynamic - typing. In Java, you have Strong - but static - typing. As opposed to C, where it's static and weak. Or PHP, where it looks like it's dynamic and weak. There's a world of difference here. Nicholas explains this very well.

 Share Tweet This
-->