<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Languages of the real and artificial - Latest Comments in Minimizing Code Paths in Asychronous Code</title><link>http://osteele.disqus.com/</link><description></description><atom:link href="https://osteele.disqus.com/minimizing_code_paths_in_asychronous_code/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Thu, 12 Jun 2008 08:20:38 -0000</lastBuildDate><item><title>Re: Minimizing Code Paths in Asychronous Code</title><link>http://blog.osteele.com/archives/2008/04/minimizing-code-paths-asychronous-code#comment-4881240</link><description>&lt;p&gt;I made a minor addition to the Memoize function to accept a variable number of arguments. I used this function to completely rewrite a series of AJAX calls. The results were quite nice. Thanks for the idea.&lt;/p&gt;&lt;p&gt;&lt;code&gt;&lt;br&gt;// Memoize function&lt;br&gt;// k must have a signature k(data,id,...);&lt;br&gt;//Additional arguments are passed to the program.&lt;br&gt;function asyncMemoize (f) {&lt;br&gt;  var cache = {};&lt;br&gt;  return function (x, k) {&lt;br&gt;    var v = cache[x];&lt;br&gt;    var args = Array.prototype.slice.call(arguments);&lt;br&gt;    args[1] = x;&lt;br&gt;    if (v) {&lt;br&gt;      args[0] = v;&lt;br&gt;      setTimeout(function() { k.apply(null,args); }, 10);&lt;br&gt;    }&lt;br&gt;    else {&lt;br&gt;      f(x, function (v) {&lt;br&gt;         cache[x] = v;&lt;br&gt;         args[0] = v;&lt;br&gt;         k.apply(null,args);&lt;br&gt;      });&lt;br&gt;    }&lt;br&gt;  }&lt;br&gt;}&lt;br&gt;&lt;/code&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Bill Wilson</dc:creator><pubDate>Thu, 12 Jun 2008 08:20:38 -0000</pubDate></item><item><title>Re: Minimizing Code Paths in Asychronous Code</title><link>http://blog.osteele.com/archives/2008/04/minimizing-code-paths-asychronous-code#comment-4881239</link><description>&lt;p&gt;I've spent a lot of time in the last year working in GWT and Silverlight 2,  both of which involve programming in a strictly typed OO language that uses XHR for communications:  so you have the same issues with concurrency that you talk about in Javascript.  I've been writing a series articles of patterns about how to write correct RIA's despite the fact that callbacks can run in a random order:&lt;/p&gt;&lt;p&gt;&lt;a href="http://gen5.info/q/category/asynchronous-communications/" rel="nofollow noopener" target="_blank" title="http://gen5.info/q/category/asynchronous-communications/"&gt;http://gen5.info/q/category/asynchronous-communications/&lt;/a&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Paul Houle</dc:creator><pubDate>Fri, 02 May 2008 13:13:03 -0000</pubDate></item><item><title>Re: Minimizing Code Paths in Asychronous Code</title><link>http://blog.osteele.com/archives/2008/04/minimizing-code-paths-asychronous-code#comment-4881236</link><description>&lt;p&gt;The modified function actually _does_ guarantee the order of execution: that the code that follows the call to @requestProductDetails@ will _always_ execute prior to the invocation of the continuation parameter.&lt;/p&gt;&lt;p&gt;Making each part of program not rely on the order of execution may seem like a good thing, but it increases the number of required test cases exponentially, if nothing else.  Some indeterminacy is inherent in distributed processing; the rest can be determinized.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">oliver</dc:creator><pubDate>Tue, 22 Apr 2008 14:00:53 -0000</pubDate></item><item><title>Re: Minimizing Code Paths in Asychronous Code</title><link>http://blog.osteele.com/archives/2008/04/minimizing-code-paths-asychronous-code#comment-4881237</link><description>&lt;p&gt;while it is a good thing (tm) to make sure that code works the way the programmer expects - that the async callback gets executed after the next statement, one should remember that the reality of async is that you cannot guarentee order of execution, and thus, should instead modify the design of the program to not have to rely on the order of execution. There is no other way to fix this "problem".&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Chii</dc:creator><pubDate>Tue, 22 Apr 2008 10:20:20 -0000</pubDate></item><item><title>Re: Minimizing Code Paths in Asychronous Code</title><link>http://blog.osteele.com/archives/2008/04/minimizing-code-paths-asychronous-code#comment-4881238</link><description>&lt;p&gt;The inside of the function doesn't have to look complex if you abstract out the gunk:&lt;/p&gt;&lt;p&gt;[blockcode]&lt;br&gt;requestProductDetails = asyncMemoize(function (id, k) {&lt;br&gt;  ajax.get('/product/'+id, k);&lt;br&gt;});&lt;br&gt;[/blockcode]&lt;/p&gt;&lt;p&gt;Here's a definition of asyncMemoize.  (My Javascript is rusty, so forgive any bad grammar.)&lt;/p&gt;&lt;p&gt;[blockcode]&lt;br&gt;function asyncMemoize (f) {&lt;br&gt;  var cache = {};&lt;br&gt;  return function (x, k) {&lt;br&gt;    var v = cache[x];&lt;br&gt;    if (v) {&lt;br&gt;      setTimeout(function() { k(v); }, 1);&lt;br&gt;    }&lt;br&gt;    else {&lt;br&gt;      f(x, function (v) {&lt;br&gt;         cache[x] = v;&lt;br&gt;         k(v);&lt;br&gt;      });&lt;br&gt;    }&lt;br&gt;  }&lt;br&gt;}&lt;br&gt;[/blockcode]&lt;/p&gt;&lt;p&gt;(A better definition could use varargs, but whatever.)&lt;/p&gt;&lt;p&gt;I agree that a given function should always or never invoke the callback asynchronously.  Not because I want to minimize code paths, but because that's part of the interface.  Your first version of requestProductDetails essentially has undefined behavior!&lt;/p&gt;&lt;p&gt;I would go further and suggest a naming convention, either as part of the function name or the name of the continuation argument, that indicates that it's asynchronous.  (I've been steeped in Objective-C for the last year, and I'm loving the Smalltalk-style descriptive method/argument naming.)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Bret</dc:creator><pubDate>Mon, 21 Apr 2008 00:03:07 -0000</pubDate></item></channel></rss>