Saturday, July 31, 2010

My Biggest peeve with event driven programming

is having to build constructs like this.

I need to load a bunch of templates up and then perform the rest of my actions. Event Driven programming frees you from excessively specifying order in code. Overdoing the Sequence bit in Sequence, Selection and Iteration. Except when you really need sequence, the code becomes messy. The sad part is this isn't even a great solution since it loads files one at a time. Ideally you wanna parallelize this.


  1. If the problem is working with templates, then you need a better way of working with templates :D. jQuery, which you are using here, doesn't provide one. You know where I'm going with this.

    On a more serious note: A simple way to solve this problem would be make the second request only after the first one has completed. That way, you'd be guaranteed about the sequence. However, that's not what you are looking for.

    What you need here is not to maintain execution order. What you want is a way to be notified when all your async operations are complete. There are some patterns to do this. For example, an interface for this would be an object to which you can add async operations, which would trigger a callback when all the async operations are complete. This should be trivial to implement in almost any framework, or even in pure JS.

  2. yeah, I know what you mean. A single callback which maintains state and does something when all operations are completed. But then I'd either have to maintain state in some form or run through my template list and see what's loaded. Either way it's state maintenance and verification which is ugly in it's own way.

    Building the abstraction to hide it away is meaningless if its the only example in my code :/.

    re jquery, Im using jqote since its based on John Resig's microtemplating thing. Btw, I was looking for you last week precisely because I wanted to talk about this barrier I was perceiving between adopting jquery or dojo in this little side project. (I'm building a better utorrent extension for chrome, the one that's out isn't very good.)

  3. The reason I complained is that everytime I've done some javascript mini project, I've always been hit by some variation of this problem, but not enough to fix it. But what you've said makes me wonder if I'm missing some library feature in jq that already addresses this. How about dojo, is there something of this sort?

  4. AFAIK, neither libraries solve this problem. Dojo one-ups jQuery by solving the single-event-multiple-handlers problem, which is far more common, but doesn't address your case. Your case is exactly the opposite, in fact - multiple-events-single-handler. Like I said, it shouldn't be hard to fix this with the pattern I provided.

    I didn't understand the bit where you said you'd have to "run through my template list and see what's loaded". I guess I'll catch you over chat for this?

  5. sounds good. How long are you in Delhi?