The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Minimizing Code Paths in Asychronous Code

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Oliver Steele

Posts: 112
Nickname: ows
Registered: Aug, 2003

Oliver Steele is Chief Software Architect at Laszlo Systems, Inc.
Minimizing Code Paths in Asychronous Code Posted: Apr 20, 2008 9:19 PM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by Oliver Steele.
Original Post: Minimizing Code Paths in Asychronous Code
Feed Title: Oliver Steele on Software
Feed URL: http://feeds.feedburner.com/osteele
Feed Description: Languages of the real and artificial.
Latest Agile Buzz Posts
Latest Agile Buzz Posts by Oliver Steele
Latest Posts From Oliver Steele on Software

Advertisement

Have you ever written a function that looks like this?

function requestProductDetails(id, k) {
  var value = gProductDetailsCache[id];
  if (value)
    k(value)
  else
    ajax.get(‘/product/’+id, function(data) {
      gProductDetailsCache[id] = data;
      k(data);
    });
}

requestProductDetails calls its callback with the product details, which are stored in a cache. Since it might need to request this information from the server, it has to “return” it by passing it to a callback; in order to present a uniform API whether or not the product is cached, it “returns” the data this way whether it came from the cache or not.

requestProductDetails is intended to be used this way:

requestProductDetails(id, function(details) {
  infoPanel.setDetails(id, details);
});
infoPanel.setName(id, gProductNames[id]);

(I gave infoPanel a somewhat silly API in order to demonstrate a point. The general pattern is that there’s some computation in the callback, and some other computation after the call.)

There’s a subtle problem in this code, which is that there’s two different code paths through it. In the cached case, infoPanel.setDetails is called infoPanel.setName. In the uncached case (the first time through), it’s the other way around. If there’s a bug that causes setDetails to work only after setName has been called, you may well miss it during casual testing, because it will only trigger the second time you trigger the code — and once it does trigger, it will appear intermittently (especially if you have a more sophisticated cache), and be darned difficult to find.

I recommend this implementation of requestProductDetails instead. It makes the functional itself slightly more complex, and the setTimeout looks gratuitous — but it makes its requestProductDetailss callers much easier to debug.

function requestProductDetails(id, k) {
  var value = gProductDetailsCache[id];
  if (value)
    setTimeout(function() { k(value) }, 10);
  else
    ajax.get(‘/product/’+id, function(data) {
      gProductDetailsCache[id] = data;
      k(data);
    });
}

The general principle here is if a function sometimes has to call its callback asynchronously, always call it asynchronously, to minimize the number of possible code paths through the application.

Read: Minimizing Code Paths in Asychronous Code

Topic: Seaside in Hamburg Previous Topic   Next Topic Topic: More Meeting Photos

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use