Thursday, September 22, 2011

Javascript, "The Next Big Mistake" ?

I've heard tons of buzz lately on how Javascript is going to replace every programming language ever written , how server side javascript is going to be the next big thing, how NoSQL is going to forever change the world.

After working in a pure javascript stack for the last 8 months, using MongoDB ( javascript scripted / json document storage ), NodeJS ( server side javascript using the V8 engine ) , and ExtJS ( client side UI library ), I pray they are wrong.

It's not any one of these technologies in itself are bad ( NodeJS is actually blazingly fast, outperforms .NET by factors of 10 on an amazon EC2 instance with  [ seriously ] 256K of memory ), but take a bunch of bleeding edge software technologies and tie them together with possibly the worse scripting language ever written and what you get are headaches.

Take for example a bug I hit recently, where I call from Node ( which uses V8 as it's interpeter ), into Mongo ( which uses Spider Monkey ), calling what is the equivelant of a stored procedure , passing it a callback that executes within Node ( back to V8 ), and my this pointer completely vanishes.  I have lost all   scope besides global.

Node is very proud of it's asynch approach to programming, and at times it feels very nice.  At other times it's a horrible blight on the world.

Most of the problems revolve around asynch functions and recursion.  Or for loops, those are also  a pain. The recursion problem is actually not so bad because there is an agreed upon way of handling it, albeit an ugly one, involving a counter that increments everytime the function is called, and decrementing the counter after the function call, and when the counter returns to 0 you call the callback function.

But the ugliest by far is the for loop with asynchronisity.  Let's say you have to loop over a collection, call an asynch function that modifies the element in the collection, then after all the asynch calls have finished, respond to the client.  As far as I have found there is no agreed upon paradigm, mostly it revolves a forever loop that waits for some condition ( count == finalList.length , where count gets incremented before the asynch call ), and then when the condition is met, call your user passed in callback.

Is Javascript going to take over the world ?  You better hope not.

2 comments:

  1. I'm not a node programmer, but couldn't you pass a "deferred" object to your async calls, which, when they've finished, can call back the deferred object.

    Then you can instance a composite deferred object, which is composed of all deferred objects, and which is "complete" when all deferred objects are.

    And there you are.

    ReplyDelete
  2. Yes thats a good approach, however there is no builtin support for this, you would need to created your own Deferred class , and then hope it catches on with the community.

    I will give that a go though.

    ReplyDelete