PHP and dynamically typed languages

NOTE: This is a restored version from this archive.

This is a copy of an email I sent to Elliotte Rusty Harold

Hi Elliotte,

First of all, since it’s the first time I mail you, I want to thank you for your two sites, Cafe au Lait and Cafe con Leche, since I read them since forever (well, since 2000, maybe before). I even was Quote of the Day once, which I was very proud of (it was when I thought that participating to xml-dev could actually change something to the way XML evolved).

Anyway, I’m writing you about your post on PHP, the fact that it sucked and how it led you to think that weakly typed language were way less effective than strongly typed ones. Well, the debate has never ended on the weakly vs strongly typed languages subject, so I can’t expect to write anything clever here… But one thing is sure, you must not make any decision on the subject based on your PHP experience.

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 this article). 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.

Conversely, Python is truly a great language, a sound one ; nothing in common with PHP. Dynamic typing means that you don’t have to worry about declaring variable and parameters types, and that you benefit from implicit interfaces (what other call “duck typing”, which means that provided that an object can kwack, I can assume that’s its a duck). But if you do something invalid, bam, you get an exception. Smalltalk works the same way.

Programming in Python is straightforward, predictable, and fast. The runtime is slow, though, slower than Java, and sometimes I miss the (relative) speed of Java. But then I write C or C++ extension (and I’m now learning D now with the hope that I’ll be able to write Python extensions in D). Plus, a lot of work is currently done on performance (including a JIT, named Psyco), so we can expect better performance in the future.

I was a great fan of Java. I began using Java back in 1995, with the 1.0 beta version (writing applets for Hotjava). I really liked this language, until two years ago when I realised that I was using XML more and more to be able to assemble Java components in a scriptable way. I decided that instead of trying to pilot Java code from XML script-like documents (at that point I had built a XML language that could compile to Java code), I should switch to a scripting language.

I tried Judoscript, I tried Jython, I tried other JVM-based scripting language, but I still couldn’t quit the security of a strongly typed environment (plus, the “indentation-as-syntax” gave me the creeps). Then one day, I had to write some data import script, very stupid and very tedious, and I told myself “what the heck, let’s try to do this in plain Python”. I did so, and I was quite satisfied from the experience (the task was tedious enough, and not having to fight with the stupid JDBC typing system was a relief). After a short transition period, I quit writing Java code, and now I’m a Python convert. Really, I needed to switch to a totally different mindset, but now that I did, I’m much more confortable with a (good) dynamically typed language.

Strong typing is just like the little wheels you put on the side of a child’s bike. They’re reassuring, but once you know how to balance the bike, they get in the way and prevent you from gaining speed.

The same goes for strong typing and programmation : after a while, you realise that you can totally picture what kind of object goes where, and that having to type the variable is not needed. Then you save time by not having to write type declarations, wrappers, adapters etc. In any case, if you do a mistake, the runtime promptly reminds you of this. And I’m not even talking about the fact that you don’t need to declare the exceptions that can be thrown by a method (in Java parlance, all exceptions inherit from RuntimeException). It’s an incredible time saver.

The only drawback of dynamic typing, though, is the lack of refactoring possibilites. I miss IntelliJ IDEA and all its refactoring support badly. Then again, a lot of refactoring were caused by the strong typing nature of Java, and finally, I don’t have often to refactor my code in a way that could be automated by a smart IDE like IDEA.

I’m not trying to convince you or anything, but again, don’t judge dynamic typing from the messy crap that PHP is.

As for Web development in Python, well, it’s sad that we have too much choices. Zope/Plone/Python is one choice, but it’s not the easiest way to go. I’ve decided to use it for a project for one of my customers (despite of the warnings I’ve received), and I can tell you that I’m regretting this decision almost daily. Zope is not a Python developement framework, it has its own development model, and the few parts where you can write Python, the language is totally restrained.

We’re now building projects on top of mod_python, and I truly prefer this alternative, even if it means that we have to do a lot of things we could expect from a mature web framework. But then again, there are half a dozen competing frameworks, so the choice is yours… Alas, I fear that all this competition is hindering the acknowledgement of Python as an interesting web development language… Like I wrote in my blog post mentioned above, PHP is indeed a truly crappy language, but at least it defines a relatively stable platform, hence its strong presence everywhere.

Best regards,

Nicolas Lehuen http://nicolas.lehuen.com/

P.S. with you permission, I put this mail on my blog as an open letter ; if it bothers you, tell me and I’ll remove it.