Static vs. Dynamic Typing
January 25th 2008
Every good programmer probably has to write about their opinion on this subject once in their lifetime, so here’s my take
As part of my consulting gig I work on “Enterprise Java” code. I just had to write this line after receiving two separate exceptions on the matter (types obfuscated to protect the guilty (and they are oh so guilty… I won’t get into that)):
String somenumber = (String) ((TypeAttribute) TypeCache.getTypeFromCache("Thingie") .getAttribute("someNumber")).getValue((Thingie) object);
You’re a freaking computer! Figure it out for me!
Also, xkcd, as always, says it the best.
It’s revealing to be a Java programmer every 2 weeks, and a Ruby and Flex developer the next 2 weeks, with PHP and JavaScript by night. I understand everything about why static typing is good and proper, but today, when computers (and compilers especially) are powerful and intelligent, I think the computer should do what a computer is great for, namely figuring out extremely complex yet orderly relationships between types of things, leaving the programmer to focus on much more important stuff.
When I’m coding in Java, it takes me 50 lines of rudimentary logic and typecasting muck in a new inline comparator class to do something as simple as sorting custom objects.
In Ruby it’s a one-liner. I don’t care how much less efficient that is for the computer (and it’s not), that is worth its weight in gold in programmer time and code elegance.
I enjoy dynamically typed languages, and any good programmer knows that it’s programmers having fun that makes good software, not programmers spending 50% of their time dealing with code that gets in their way. What kills me most is that computers are really good at automatically doing the stuff that’s not fun—that is in fact what they’re designed to do—and there are people who have fun making that stuff fast, so why don’t we just let them?








this is a major issue indeed. ideally, you would have a dynamically typed language that integrates optional static typing as well…i do hope there’s some PL research going into this (cuz it may need a lot of sound, rigorous design to keep things secure and type-safe when necessary)!
the only language i know of that comes close is, perhaps surprisingly, Common LISP. CL is all dynamically typed, but you can put in optional type-declarations for speed (ie. tell LISP that you know FOR SURE that this argument will be an integer coming in, so don’t waste time checking that it is). of course if you’re wrong with these declarations, then LISP will just do random stuff (and NOT give you any run-time type errors). perhaps this is OK, in practice? unfortunately, it’s hard to say for sure…since “LISP in practice” is a de facto oxymoron in high-performance computing.
and, for the case of Java, here’s something you may find helpful (if you haven’t seen it already): http://snobol.cs.berkeley.edu/prospector/index.jsp
Funny you mention optional static typing, as ECMAScript 4 (a.k.a. JavaScript 2) has optional type declarations. It’s used in Adobe Flex (where it’s called ActionScript 3) and I like it a lot so far. Sure the IDE gives you warnings about unknown types (because that’s what the compiler sees) but it will still run completely dynamic like good ol’ JavaScript always did with no problem.
Type declarations are there for all the right reasons and when they make no sense you can just leave them out—especially helpful for generic methods running on objects sharing a set of attributes that aren’t necessarily of the same type, or any number of other dynamic possibilities. Fun stuff.
And I’ve definitely seen prospector—didn’t we use it in 61B or some class? It’s helpful as a way around Java’s complexity when you have to deal with it, but it doesn’t fix it.
ah interesting. i’ll have to check out JS2. just looking at the docs briefly, i already like the idea of “evolutionary programming.” i like the idea of “evolving” a quick and dirty dynamically typed garbage-collected prototype script into a fast, robust statically-typed powerhouse by incrementally adding things like type decl’s and what not. i’ll be keeping an eye on this!
yeah, it’s an awesome language. Takes a lot of the good ideas of most others, like dynamic typing, optional strong typing, first-class functions, prototype inheritance, interfaces, all that good oop stuff… like I said I’m really enjoying working with it so far, and that says enough.
I read an article somewhere that said its only downside was C syntax but that was necessary for widespread adoption. I totally agree, I’d prefer something more stylish and elegant, but C syntax is a small price to pay for all the right language features finally.
http://steve-yegge.blogspot.com/2007/02/next-big-language.html – He’s talking about JS2. Woot.