<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Minimizing Code Paths in Asychronous Code</title>
	<atom:link href="http://osteele.com/archives/2008/04/minimizing-code-paths-asychronous-code/feed" rel="self" type="application/rss+xml" />
	<link>http://osteele.com/archives/2008/04/minimizing-code-paths-asychronous-code</link>
	<description>Languages of the real and artificial.</description>
	<lastBuildDate>Thu, 12 Feb 2009 00:20:28 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Bill Wilson</title>
		<link>http://osteele.com/archives/2008/04/minimizing-code-paths-asychronous-code/comment-page-1#comment-447</link>
		<dc:creator>Bill Wilson</dc:creator>
		<pubDate>Thu, 12 Jun 2008 12:20:38 +0000</pubDate>
		<guid isPermaLink="false">http://osteele.com/2008/04/20/minimizing-code-paths-in-asychronous-code#comment-447</guid>
		<description>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;code&gt;
// Memoize function
// k must have a signature k(data,id,...);
//Additional arguments are passed to the program.
function asyncMemoize (f) {
  var cache = {};
  return function (x, k) {
    var v = cache[x];
    var args = Array.prototype.slice.call(arguments);
    args[1] = x;
    if (v) {
      args[0] = v;
      setTimeout(function() { k.apply(null,args); }, 10);
    }
    else {
      f(x, function (v) {
         cache[x] = v;
         args[0] = v;
         k.apply(null,args);
      });
    }
  }
}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>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 <span class="caps">AJAX </span>calls. The results were quite nice. Thanks for the idea.</p>
<p><code><br />
// Memoize function<br />
// k must have a signature k(data,id,...);<br />
//Additional arguments are passed to the program.<br />
function asyncMemoize (f) {<br />
  var cache = {};<br />
  return function (x, k) {<br />
    var v = cache[x];<br />
    var args = Array.prototype.slice.call(arguments);<br />
    args[1] = x;<br />
    if (v) {<br />
      args[0] = v;<br />
      setTimeout(function() { k.apply(null,args); }, 10);<br />
    }<br />
    else {<br />
      f(x, function (v) {<br />
         cache[x] = v;<br />
         args[0] = v;<br />
         k.apply(null,args);<br />
      });<br />
    }<br />
  }<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul Houle</title>
		<link>http://osteele.com/archives/2008/04/minimizing-code-paths-asychronous-code/comment-page-1#comment-446</link>
		<dc:creator>Paul Houle</dc:creator>
		<pubDate>Fri, 02 May 2008 17:13:03 +0000</pubDate>
		<guid isPermaLink="false">http://osteele.com/2008/04/20/minimizing-code-paths-in-asychronous-code#comment-446</guid>
		<description>I&#039;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&#039;ve been writing a series articles of patterns about how to write correct RIA&#039;s despite the fact that callbacks can run in a random order:

&lt;a href=&quot;http://gen5.info/q/category/asynchronous-communications/&quot;&gt;http://gen5.info/q/category/asynchronous-communications/&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>I&#8217;ve spent a lot of time in the last year working in <span class="caps">GWT </span>and Silverlight 2,  both of which involve programming in a strictly typed OO language that uses <span class="caps">XHR </span>for communications:  so you have the same issues with concurrency that you talk about in Javascript.  I&#8217;ve been writing a series articles of patterns about how to write correct <span class="caps">RIA&#8217;</span>s despite the fact that callbacks can run in a random order:</p>
<p><a href="http://gen5.info/q/category/asynchronous-communications/">http://gen5.info/q/category/asynchronous-communications/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: oliver</title>
		<link>http://osteele.com/archives/2008/04/minimizing-code-paths-asychronous-code/comment-page-1#comment-443</link>
		<dc:creator>oliver</dc:creator>
		<pubDate>Tue, 22 Apr 2008 18:00:53 +0000</pubDate>
		<guid isPermaLink="false">http://osteele.com/2008/04/20/minimizing-code-paths-in-asychronous-code#comment-443</guid>
		<description>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.

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.</description>
		<content:encoded><![CDATA[<p>The modified function actually <em>does</em> guarantee the order of execution: that the code that follows the call to <code>requestProductDetails</code> will <em>always</em> execute prior to the invocation of the continuation parameter.</p>
<p>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.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chii</title>
		<link>http://osteele.com/archives/2008/04/minimizing-code-paths-asychronous-code/comment-page-1#comment-444</link>
		<dc:creator>Chii</dc:creator>
		<pubDate>Tue, 22 Apr 2008 14:20:20 +0000</pubDate>
		<guid isPermaLink="false">http://osteele.com/2008/04/20/minimizing-code-paths-in-asychronous-code#comment-444</guid>
		<description>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 &quot;problem&quot;. 
</description>
		<content:encoded><![CDATA[<p>while it is a good thing &#8482; to make sure that code works the way the programmer expects &#8211; 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 &#8220;problem&#8221;. </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bret</title>
		<link>http://osteele.com/archives/2008/04/minimizing-code-paths-asychronous-code/comment-page-1#comment-445</link>
		<dc:creator>Bret</dc:creator>
		<pubDate>Mon, 21 Apr 2008 04:03:07 +0000</pubDate>
		<guid isPermaLink="false">http://osteele.com/2008/04/20/minimizing-code-paths-in-asychronous-code#comment-445</guid>
		<description>The inside of the function doesn&#039;t have to look complex if you abstract out the gunk:

[blockcode]
requestProductDetails = asyncMemoize(function (id, k) {
  ajax.get(&#039;/product/&#039;+id, k);
});
[/blockcode]

Here&#039;s a definition of asyncMemoize.  (My Javascript is rusty, so forgive any bad grammar.)

[blockcode]
function asyncMemoize (f) {
  var cache = {};
  return function (x, k) {
    var v = cache[x];
    if (v) {
      setTimeout(function() { k(v); }, 1);
    }
    else {
      f(x, function (v) {
         cache[x] = v;
         k(v);
      });
    }
  }
}
[/blockcode]

(A better definition could use varargs, but whatever.)

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&#039;s part of the interface.  Your first version of requestProductDetails essentially has undefined behavior!

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&#039;s asynchronous.  (I&#039;ve been steeped in Objective-C for the last year, and I&#039;m loving the Smalltalk-style descriptive method/argument naming.)

</description>
		<content:encoded><![CDATA[<p>The inside of the function doesn&#8217;t have to look complex if you abstract out the gunk:</p>
<p>[blockcode]<br />
requestProductDetails = asyncMemoize(function (id, k) {<br />
  ajax.get(&#8217;/product/&#8217;+id, k);<br />
});<br />
[/blockcode]</p>
<p>Here&#8217;s a definition of asyncMemoize.  (My Javascript is rusty, so forgive any bad grammar.)</p>
<p>[blockcode]<br />
function asyncMemoize (f) {<br />
  var cache = {};<br />
  return function (x, k) {<br />
    var v = cache[x];<br />
    if (v) {<br />
      setTimeout(function() { k(v); }, 1);<br />
    }<br />
    else {<br />
      f(x, function (v) {<br />
         cache[x] = v;<br />
         k(v);<br />
      });<br />
    }<br />
  }<br />
}<br />
[/blockcode]</p>
<p>(A better definition could use varargs, but whatever.)</p>
<p>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&#8217;s part of the interface.  Your first version of requestProductDetails essentially has undefined behavior!</p>
<p>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&#8217;s asynchronous.  (I&#8217;ve been steeped in Objective-C for the last year, and I&#8217;m loving the Smalltalk-style descriptive method/argument naming.)</p>
]]></content:encoded>
	</item>
</channel>
</rss>
