Friday, February 01, 2008

Why dynamic languages suck

I had a little bit of JScript that wasn't working as expected. I stared and stared at it... For a very long time... Here it is.

var splitDetails = details.split("\t");
for (var i=0; i<splitDetails.Length; i++)
  // do something

The problem was that the //do something was never getting hit. Life would have been easier with a debugger but this was server-side JScript in Metastorm which doesn't support debugging so I was forced to just stare at it to work out what was wrong.

No doubt you're now shouting at your computer "you're a fecking idiot! The problem is obvious!" and perhaps you'd be right. Perhaps I was just having a senior moment, which I think I'm entitled to have at my age. For those of you who haven't spotted the error I'd typed length with an upper-case L.

So what happens here? JScript doesn't throw an error because I've tried to access a property that doesn't exist, instead it returns null. Then I presume it converts that to 0 for the loop test so never drops into the code inside the loop.

This kind of thing is all too easy in a dynamic language. If I was using a compiled statically-typed language the compiler would have picked up the problem immediately. Perhaps the advantages of dynamic languages outweigh these kind of problems but personally I'd rather have the crutch of a compiler.


Thomas said...

In php you can set an error level on your dev machine, to something like warnings+errors (where you would only set it to errors on the production machine). That would have given your an error, in a case like this...

Not sure if something like that is possible with jscript...

Eduardo said...

Personally, I dislike dynamic languages too, but for totally different reasons. I hate how SLOOOW programs written in dynamic languages are. My programs are supposed to be run by a computer, not by me. And I would like them to be as efficient as possible (in both time and space) when they are executing. In my humble opinion, arguments in defense of dynamic languages based on "the productivity gain outweighs the performance loss" are of bad taste.