<?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: Challenge: Do You Truly Understand This Code?</title>
	<atom:link href="http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/</link>
	<description>Trying to walk that thin line between intelligence and ignorance</description>
	<lastBuildDate>Sat, 13 Mar 2010 21:19:34 +0100</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Using Copy-On-Write In Multithreaded Code To Reduce Locking Overhead &#124; The Inquisitive Coder &#8211; Davy Brion&#39;s Blog</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-31139</link>
		<dc:creator>Using Copy-On-Write In Multithreaded Code To Reduce Locking Overhead &#124; The Inquisitive Coder &#8211; Davy Brion&#39;s Blog</dc:creator>
		<pubDate>Mon, 08 Mar 2010 15:36:44 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-31139</guid>
		<description>[...] leaves the copy-on-write pattern.&#160; I’ve used it before with success (though at the time, i didn’t know it was a known pattern) so this approach has [...]</description>
		<content:encoded><![CDATA[<p>[...] leaves the copy-on-write pattern.&#160; I’ve used it before with success (though at the time, i didn’t know it was a known pattern) so this approach has [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sl</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-9104</link>
		<dc:creator>sl</dc:creator>
		<pubDate>Mon, 23 Feb 2009 23:21:08 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-9104</guid>
		<description>There&#039;s no such thing as self-documenting code.  Sure, you can always roll up your sleeves and study the code line by line. But when you have hundreds or quite commonly thousands of lines of code, it&#039;s impractical to go through all of them. Regardless of how clean your code is written, nothing beats a few lines of plain English sentences describing what your code does at a high-level (unless you&#039;re writing a &quot;hello world&quot; application).</description>
		<content:encoded><![CDATA[<p>There&#8217;s no such thing as self-documenting code.  Sure, you can always roll up your sleeves and study the code line by line. But when you have hundreds or quite commonly thousands of lines of code, it&#8217;s impractical to go through all of them. Regardless of how clean your code is written, nothing beats a few lines of plain English sentences describing what your code does at a high-level (unless you&#8217;re writing a &#8220;hello world&#8221; application).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Davy Brion</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-9092</link>
		<dc:creator>Davy Brion</dc:creator>
		<pubDate>Mon, 23 Feb 2009 20:44:53 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-9092</guid>
		<description>@JM

the importance of comments in certain situations, no matter what the purists might say. did you really read the 3 posts including the (post) comments? i kinda figured it&#039;d be clear (the point)</description>
		<content:encoded><![CDATA[<p>@JM</p>
<p>the importance of comments in certain situations, no matter what the purists might say. did you really read the 3 posts including the (post) comments? i kinda figured it&#8217;d be clear (the point)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JM</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-9090</link>
		<dc:creator>JM</dc:creator>
		<pubDate>Mon, 23 Feb 2009 20:32:25 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-9090</guid>
		<description>This reminds me of a &quot;look how smart I am&quot; blog. Not sure what you were trying to get across in your three blogs on this code. The use of comments? The way foreach works? The I can code neat stuff, can you figure it out (I am in this camp)?</description>
		<content:encoded><![CDATA[<p>This reminds me of a &#8220;look how smart I am&#8221; blog. Not sure what you were trying to get across in your three blogs on this code. The use of comments? The way foreach works? The I can code neat stuff, can you figure it out (I am in this camp)?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: vnaranjo</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-9088</link>
		<dc:creator>vnaranjo</dc:creator>
		<pubDate>Mon, 23 Feb 2009 20:08:30 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-9088</guid>
		<description>Sure. You have a List of Clients, and to control the access to the list, you use a Monitor (dummy object, just to ensure threat Safety).</description>
		<content:encoded><![CDATA[<p>Sure. You have a List of Clients, and to control the access to the list, you use a Monitor (dummy object, just to ensure threat Safety).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Terry</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-9085</link>
		<dc:creator>Terry</dc:creator>
		<pubDate>Mon, 23 Feb 2009 15:17:13 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-9085</guid>
		<description>Without a doubt, I would say that with no &quot;why&quot; style comments the context and the purpose of this code is unclear to say the least. It&#039;s obvious &quot;what&quot; the code does but &quot;why&quot;? Sadly mean code purists don&#039;t understand this fact.</description>
		<content:encoded><![CDATA[<p>Without a doubt, I would say that with no &#8220;why&#8221; style comments the context and the purpose of this code is unclear to say the least. It&#8217;s obvious &#8220;what&#8221; the code does but &#8220;why&#8221;? Sadly mean code purists don&#8217;t understand this fact.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James Hart</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-9079</link>
		<dc:creator>James Hart</dc:creator>
		<pubDate>Mon, 23 Feb 2009 10:41:20 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-9079</guid>
		<description>One thing I would say that is overly obscure is the use of a collection initialiser:

clients = new List(clients) { client };

you&#039;re using the collection initialiser here to shorten the equivalent code:

clients = new List(clients);
clients.Add(client);

In this case, I&#039;d explcitly expand that out. The problem is that idiomatically, collection initialisers are typically used to populate a collection object after it has been constructed -empty-. Using it to append an item to a prepopulated collection is non-idiomatic, and distracts from noticing that the collection is prepopulated at all.

Also, looking at your removal code, you might consider, instead of creating a list then removing an item, switching to:

clients = clients.Where(c =&gt; c != client).ToList();

though that maybe plays down the list creation; perhaps:

clients = new List(clients.Where(c =&gt; c != client));</description>
		<content:encoded><![CDATA[<p>One thing I would say that is overly obscure is the use of a collection initialiser:</p>
<p>clients = new List(clients) { client };</p>
<p>you&#8217;re using the collection initialiser here to shorten the equivalent code:</p>
<p>clients = new List(clients);<br />
clients.Add(client);</p>
<p>In this case, I&#8217;d explcitly expand that out. The problem is that idiomatically, collection initialisers are typically used to populate a collection object after it has been constructed -empty-. Using it to append an item to a prepopulated collection is non-idiomatic, and distracts from noticing that the collection is prepopulated at all.</p>
<p>Also, looking at your removal code, you might consider, instead of creating a list then removing an item, switching to:</p>
<p>clients = clients.Where(c =&gt; c != client).ToList();</p>
<p>though that maybe plays down the list creation; perhaps:</p>
<p>clients = new List(clients.Where(c =&gt; c != client));</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-9076</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Mon, 23 Feb 2009 09:26:39 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-9076</guid>
		<description>I&#039;m not familiar enough with C#, but I would have expected the list variable to be marked as volatile. Otherwise, it can be cached and won&#039;t be seen by all threads until the CPU caches are flushed. At that point, its a standard CopyOnWriteList, and I&#039;d have just made a reusable data structure. This is pretty standard and straight-forward code, imho, and I&#039;ve written similar.</description>
		<content:encoded><![CDATA[<p>I&#8217;m not familiar enough with C#, but I would have expected the list variable to be marked as volatile. Otherwise, it can be cached and won&#8217;t be seen by all threads until the CPU caches are flushed. At that point, its a standard CopyOnWriteList, and I&#8217;d have just made a reusable data structure. This is pretty standard and straight-forward code, imho, and I&#8217;ve written similar.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Davy Brion</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-9074</link>
		<dc:creator>Davy Brion</dc:creator>
		<pubDate>Mon, 23 Feb 2009 07:17:41 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-9074</guid>
		<description>@Bob

i believe comments are at least helpful in this case because the problem isn&#039;t obvious to everyone nor is the solution

and no, i don&#039;t want a pat on the back for this... it&#039;s funny you think of that. How much functionality would a class need before one deserves a pat on the back from you? ;)</description>
		<content:encoded><![CDATA[<p>@Bob</p>
<p>i believe comments are at least helpful in this case because the problem isn&#8217;t obvious to everyone nor is the solution</p>
<p>and no, i don&#8217;t want a pat on the back for this&#8230; it&#8217;s funny you think of that. How much functionality would a class need before one deserves a pat on the back from you? <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bob</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-9071</link>
		<dc:creator>bob</dc:creator>
		<pubDate>Mon, 23 Feb 2009 06:11:12 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-9071</guid>
		<description>Creating a copy on write LIst is OK i guess, if your willing to put up with that kind of overhead.

My confusion rests in your question about comments.  This class does almost nothing.
Why would comments be required?  

You really dont want a pat on the back for writing 50 lines of clear but essentially minimal functionality do you.</description>
		<content:encoded><![CDATA[<p>Creating a copy on write LIst is OK i guess, if your willing to put up with that kind of overhead.</p>
<p>My confusion rests in your question about comments.  This class does almost nothing.<br />
Why would comments be required?  </p>
<p>You really dont want a pat on the back for writing 50 lines of clear but essentially minimal functionality do you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Justin Etheredge</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-9006</link>
		<dc:creator>Justin Etheredge</dc:creator>
		<pubDate>Fri, 20 Feb 2009 19:00:34 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-9006</guid>
		<description>You are treating your Lists as immutable and concatenating the new item or removing the old item from a new list. You are doing this because if you are in the middle of a broadcast when a client is added or removed it will cause the list to be modified which will throw a &quot;collection was modified&quot; exception from the &quot;foreach&quot; loop since you modified the iterator. Wow that was one heck of a run-on sentence.</description>
		<content:encoded><![CDATA[<p>You are treating your Lists as immutable and concatenating the new item or removing the old item from a new list. You are doing this because if you are in the middle of a broadcast when a client is added or removed it will cause the list to be modified which will throw a &#8220;collection was modified&#8221; exception from the &#8220;foreach&#8221; loop since you modified the iterator. Wow that was one heck of a run-on sentence.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian Chamberlain</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-8990</link>
		<dc:creator>Ian Chamberlain</dc:creator>
		<pubDate>Fri, 20 Feb 2009 00:05:06 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-8990</guid>
		<description>I think this is to manage concurrency. You cannot support enumeration (as in Broadcast) with write accesses (Add and Remove) at the same time as List instance members are not thread safe. All you would need to get a problem would be *one* add/remove happening at the same time as a broadcast to generate an exception. You either have to lock the List whilst enumerating or not modify it whilst a broadcast is in progress.
If the issue is thread safety I like to indicate that in the method name as in ThreadSafeAddxxx or ThreadSafeRemovexxx.</description>
		<content:encoded><![CDATA[<p>I think this is to manage concurrency. You cannot support enumeration (as in Broadcast) with write accesses (Add and Remove) at the same time as List instance members are not thread safe. All you would need to get a problem would be *one* add/remove happening at the same time as a broadcast to generate an exception. You either have to lock the List whilst enumerating or not modify it whilst a broadcast is in progress.<br />
If the issue is thread safety I like to indicate that in the method name as in ThreadSafeAddxxx or ThreadSafeRemovexxx.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Davy Brion</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-8988</link>
		<dc:creator>Davy Brion</dc:creator>
		<pubDate>Thu, 19 Feb 2009 22:47:24 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-8988</guid>
		<description>i will post a full explanation of the problem, including the comments i added to the code and the tests that protect against the issue and then you can all decide what is more communicative: the comments or the tests

but seeing as how i&#039;m enjoying leaving you all in suspense (i&#039;m rather evil that way), i will do that on either saturday or sunday depending on when i have the time to write the post :)

in the meantime, feel free to keep guessing :p</description>
		<content:encoded><![CDATA[<p>i will post a full explanation of the problem, including the comments i added to the code and the tests that protect against the issue and then you can all decide what is more communicative: the comments or the tests</p>
<p>but seeing as how i&#8217;m enjoying leaving you all in suspense (i&#8217;m rather evil that way), i will do that on either saturday or sunday depending on when i have the time to write the post <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>in the meantime, feel free to keep guessing :p</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-8986</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Thu, 19 Feb 2009 22:35:44 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-8986</guid>
		<description>To me, the code is very readable

I agree with Laila that seeing the tests fort his code would show more than comments.</description>
		<content:encoded><![CDATA[<p>To me, the code is very readable</p>
<p>I agree with Laila that seeing the tests fort his code would show more than comments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Davy Brion</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-8985</link>
		<dc:creator>Davy Brion</dc:creator>
		<pubDate>Thu, 19 Feb 2009 22:34:18 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-8985</guid>
		<description>OK... one hint: in the worst possible scenario (from a load perspective), i&#039;m expecting maybe a couple of broadcasts _per minute_</description>
		<content:encoded><![CDATA[<p>OK&#8230; one hint: in the worst possible scenario (from a load perspective), i&#8217;m expecting maybe a couple of broadcasts _per minute_</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stijn Guillemyn</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-8984</link>
		<dc:creator>Stijn Guillemyn</dc:creator>
		<pubDate>Thu, 19 Feb 2009 22:04:23 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-8984</guid>
		<description>@Ayende

Indeed. As always, your explanation is more clear, but that&#039;s what I meant with:
&lt;i&gt;probably not something you want giving the fact you want the broadcast to act instantly&lt;/i&gt;

You need a way to ensure your code doesn&#039;t break when broadcasting, due to a changing list.
You might lock every access, or use this approach that gives better broadcast performance (which is the main purpose of your class after all).


To me, comments would be advisable. What&#039;s your take on this?</description>
		<content:encoded><![CDATA[<p>@Ayende</p>
<p>Indeed. As always, your explanation is more clear, but that&#8217;s what I meant with:<br />
<i>probably not something you want giving the fact you want the broadcast to act instantly</i></p>
<p>You need a way to ensure your code doesn&#8217;t break when broadcasting, due to a changing list.<br />
You might lock every access, or use this approach that gives better broadcast performance (which is the main purpose of your class after all).</p>
<p>To me, comments would be advisable. What&#8217;s your take on this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ayende Rahien</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-8983</link>
		<dc:creator>Ayende Rahien</dc:creator>
		<pubDate>Thu, 19 Feb 2009 21:50:28 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-8983</guid>
		<description>I would say that the actual reason is performance.
You aren&#039;t likely to changing the broadcast list all that often.
By having an immutable value, you can avoid locking whenever you need to broadcast.</description>
		<content:encoded><![CDATA[<p>I would say that the actual reason is performance.<br />
You aren&#8217;t likely to changing the broadcast list all that often.<br />
By having an immutable value, you can avoid locking whenever you need to broadcast.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Should you add comments on readable, communicative code? &#171; Stijn Guillemyn</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-8980</link>
		<dc:creator>Should you add comments on readable, communicative code? &#171; Stijn Guillemyn</dc:creator>
		<pubDate>Thu, 19 Feb 2009 21:42:01 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-8980</guid>
		<description>[...] you add comments on readable, communicative&#160;code?  Davy Brion has put some code online to challenge his readers. And guess what&#8230; I&#8217;m a reader and I like challenges!  &#8221;Do you truly [...]</description>
		<content:encoded><![CDATA[<p>[...] you add comments on readable, communicative&nbsp;code?  Davy Brion has put some code online to challenge his readers. And guess what&#8230; I&#8217;m a reader and I like challenges!  &#8221;Do you truly [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Davy Brion</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-8977</link>
		<dc:creator>Davy Brion</dc:creator>
		<pubDate>Thu, 19 Feb 2009 21:27:47 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-8977</guid>
		<description>@Stijn,

i&#039;m having too much fun with this post so i&#039;m not giving anything away just yet :p</description>
		<content:encoded><![CDATA[<p>@Stijn,</p>
<p>i&#8217;m having too much fun with this post so i&#8217;m not giving anything away just yet :p</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stijn Guillemyn</title>
		<link>http://davybrion.com/blog/2009/02/challenge-do-you-truly-understand-this-code/comment-page-1/#comment-8975</link>
		<dc:creator>Stijn Guillemyn</dc:creator>
		<pubDate>Thu, 19 Feb 2009 21:17:44 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=976#comment-8975</guid>
		<description>My best &amp; quick guess would be the fact that &lt;b&gt;for every add and remove, you create a new list, based on the current list&lt;/b&gt; and add or remove the given item. But this looks pretty obvious, given the fact &lt;b&gt;a broadcast uses a foreach&lt;/b&gt;, meaning the &lt;b&gt;collection itself cannot change during this operation&lt;/b&gt;.

Since you don&#039;t lock the broadcast (probably not something you want giving the fact you want the broadcast to act instantly) you should ensure the collection doesn&#039;t change by always taking a copy.

Am I close? Or far off?

I&#039;m just curious now!</description>
		<content:encoded><![CDATA[<p>My best &amp; quick guess would be the fact that <b>for every add and remove, you create a new list, based on the current list</b> and add or remove the given item. But this looks pretty obvious, given the fact <b>a broadcast uses a foreach</b>, meaning the <b>collection itself cannot change during this operation</b>.</p>
<p>Since you don&#8217;t lock the broadcast (probably not something you want giving the fact you want the broadcast to act instantly) you should ensure the collection doesn&#8217;t change by always taking a copy.</p>
<p>Am I close? Or far off?</p>
<p>I&#8217;m just curious now!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
