﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Concurrent Max</title><description>&lt;p&gt;Can you think of a better way to implement this code?&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;volatile&lt;/span&gt; Guid lastEtag;
&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; lastEtagLocker = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt;();&lt;/pre&gt;&lt;pre class="csharpcode"&gt;
&lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; UpdateLastWrittenEtag(Guid? etag)
{
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (etag == &lt;span class="kwrd"&gt;null&lt;/span&gt;)
        &lt;span class="kwrd"&gt;return&lt;/span&gt;;

    var newEtag = etag.Value.ToByteArray();

    &lt;span class="rem"&gt;// not the most recent etag&lt;/span&gt;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (Buffers.Compare(lastEtag.ToByteArray(), newEtag) &amp;lt;= 0)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt;;
    }

    &lt;span class="kwrd"&gt;lock&lt;/span&gt; (lastEtagLocker)
    {
        &lt;span class="rem"&gt;// not the most recent etag&lt;/span&gt;
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (Buffers.Compare(lastEtag.ToByteArray(), newEtag) &amp;lt;= 0)
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt;;
        }

        lastEtag = etag.Value;
    }
}
&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;We have multiple threads calling this function, and we need to ensure that lastEtag value is always the maximum value. This has the potential to be called often, so I want to make sure that I chose the best way to do this. Ideas?&lt;/p&gt;</description><link>http://ayende.com/14337/concurrent-max?Key=2c3cb183-cf99-497f-9386-c91ff1b8dc39</link><guid>http://ayende.com/14337/concurrent-max?Key=2c3cb183-cf99-497f-9386-c91ff1b8dc39</guid><pubDate>Sun, 12 Jun 2011 05:43:00 GMT</pubDate></item><item><title>Checking For Empty Enumerations</title><description>&lt;p&gt;Phil Haack has an interesting post about &lt;a href="http://haacked.com/archive/2010/06/10/checking-for-empty-enumerations.aspx"&gt;this topic&lt;/a&gt;, where he presents the following solution:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsNullOrEmpty&amp;lt;T&amp;gt;(&lt;span class="kwrd"&gt;this&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; items) {
    &lt;span class="kwrd"&gt;return&lt;/span&gt; items == &lt;span class="kwrd"&gt;null&lt;/span&gt; || !items.Any();
}&lt;/pre&gt;
  &lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;/blockquote&gt;

&lt;p&gt;This solution, unfortunately, suffers from a common problem related to handling IEnumerables. The assumption that you can iterate over enumerable more than once. This hold true for things like collections, but in many cases, this sort of code will silently hide data:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="csharpcode"&gt;var files = Directory.EnumerateFiles(&lt;span class="str"&gt;"."&lt;/span&gt;,&lt;span class="str"&gt;"*.cs"&lt;/span&gt;);
&lt;span class="kwrd"&gt;if&lt;/span&gt;(files.IsNullOrEmpty())
{
    Cosnole.WriteLine(&lt;span class="str"&gt;"No files"&lt;/span&gt;);
}
&lt;span class="kwrd"&gt;else&lt;/span&gt;
{
   &lt;span class="kwrd"&gt;foreach&lt;/span&gt;(var file &lt;span class="kwrd"&gt;in&lt;/span&gt; files)
   {
          Console.WriteLine(file);
   }
}&lt;/pre&gt;
  &lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;/blockquote&gt;

&lt;p&gt;The first file will never appear here.&lt;/p&gt;

&lt;p&gt;A better solution is:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsNullOrEmpty&amp;lt;T&amp;gt;(&lt;span class="kwrd"&gt;this&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; items, &lt;span class="kwrd"&gt;out&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; newItems) 
{
    newItems = items;
    &lt;span class="kwrd"&gt;if&lt;/span&gt;(items == &lt;span class="kwrd"&gt;null&lt;/span&gt;)
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;;
    
    var enumerator = items.GetEnumerator();
    &lt;span class="kwrd"&gt;if&lt;/span&gt;(enumerator.MoveNext() == &lt;span class="kwrd"&gt;false&lt;/span&gt;)
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;;
        
    newItems = &lt;span class="kwrd"&gt;new&lt;/span&gt;[]{enumerator.Current}.Concat(enumerator);
    
    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;true&lt;/span&gt;;
}&lt;/pre&gt;
  &lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;/blockquote&gt;

&lt;p&gt;That will not lose data.&lt;/p&gt;</description><link>http://ayende.com/4538/checking-for-empty-enumerations?Key=177e365f-2cd7-4cdf-9dcd-c8c698558345</link><guid>http://ayende.com/4538/checking-for-empty-enumerations?Key=177e365f-2cd7-4cdf-9dcd-c8c698558345</guid><pubDate>Thu, 10 Jun 2010 09:36:02 GMT</pubDate></item><item><title>C# Coding Challenge: What will this code do?</title><description>&lt;p&gt;What is the output of this code?&lt;/p&gt;  &lt;pre class="csharpcode"&gt; IDictionary&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;,&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; instanceToKey = &lt;span class="kwrd"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;();

 IDictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; keyToCost = &lt;span class="kwrd"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt;();

 var inst1 = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt;();
 var inst2 = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt;();

 instanceToKey[inst1] = &lt;span class="str"&gt;"one"&lt;/span&gt;;
 keyToCost[&lt;span class="str"&gt;"one"&lt;/span&gt;] = 1;

 instanceToKey[inst2] = &lt;span class="str"&gt;"two"&lt;/span&gt;;
 keyToCost[&lt;span class="str"&gt;"two"&lt;/span&gt;] = 2;

 &lt;span class="kwrd"&gt;string&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt; = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
 &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var key
     &lt;span class="kwrd"&gt;in&lt;/span&gt; (from inst &lt;span class="kwrd"&gt;in&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt;[]{inst1, inst2, &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt;()}
         &lt;span class="kwrd"&gt;where&lt;/span&gt; instanceToKey.TryGetValue(inst, &lt;span class="kwrd"&gt;out&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt;) 
         select &lt;span class="kwrd"&gt;value&lt;/span&gt;))
 {
     &lt;span class="kwrd"&gt;int&lt;/span&gt; cost;
     &lt;span class="kwrd"&gt;if&lt;/span&gt;(keyToCost.TryGetValue(key, &lt;span class="kwrd"&gt;out&lt;/span&gt; cost))
         Console.WriteLine(&lt;span class="str"&gt;"Key: {0}, Cost: {1}"&lt;/span&gt;, key, cost);
 }&lt;/pre&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;</description><link>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do?Key=3204fb8f-376e-4fd7-b430-709e963e9812</link><guid>http://ayende.com/4511/c-coding-challenge-what-will-this-code-do?Key=3204fb8f-376e-4fd7-b430-709e963e9812</guid><pubDate>Fri, 21 May 2010 06:36:00 GMT</pubDate></item><item><title>Sometimes imperative is so much easier</title><description>&lt;p&gt;Take a look at the following Erlang function:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="csharpcode"&gt;count_promises(Id, N, {_, PromisesQueue}) -&amp;gt;
    rec_count_promises(0, Id, N, PromisesQueue).

rec_count_promises(Count, _, _, []) -&amp;gt;
    Count;
rec_count_promises(Count, Id, N, [{Id, N, _, _} | RestQueue]) -&amp;gt;
    rec_count_promises((Count + 1), Id, N, RestQueue);
rec_count_promises(Count, Id, N, [_ | RestQueue]) -&amp;gt;
    rec_count_promises(Count, Id, N, RestQueue).&lt;/pre&gt;
  &lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;/blockquote&gt;

&lt;p&gt;I am reading a codebase full of this sort of things, and it is &lt;em&gt;really&lt;/em&gt; painful. I keep thinking back to how I would do it in C#:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; CountPromises(&lt;span class="kwrd"&gt;int&lt;/span&gt; id, &lt;span class="kwrd"&gt;int&lt;/span&gt; n, Tuple&amp;lt;List&amp;lt;Record&amp;gt;, List&amp;lt;Record&amp;gt;&amp;gt; phase1)
{
     &lt;span class="kwrd"&gt;int&lt;/span&gt; count=0;
     &lt;span class="kwrd"&gt;foreach&lt;/span&gt;(var record &lt;span class="kwrd"&gt;in&lt;/span&gt; phase1.Item2)
       {
            &lt;span class="kwrd"&gt;if&lt;/span&gt;(record.Id == id &amp;amp;&amp;amp; record.N == n)
                   count ++;
     }
     &lt;span class="kwrd"&gt;return count &lt;/span&gt;;
}&lt;/pre&gt;
  &lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;/blockquote&gt;

&lt;p&gt;Yet that is imperative, and involve mutating state. I agree, it is a huge improvement, but it can be made both functionally safe and easier to read:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre class="csharpcode"&gt;phase1.Items2.Count(record =&amp;gt; record.Id == id &amp;amp;&amp;amp; record.N == n);&lt;/pre&gt;
  &lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;/blockquote&gt;

&lt;blockquote /&gt;

&lt;p&gt;As I said, I am currently going through a codebase full of these sort of functions, and it is painful, annoying and irritating. I am not experienced enough with Erlang to be able to tell conclusively if this is idiomatic Erlang code, but I think so.&lt;/p&gt;</description><link>http://ayende.com/4495/sometimes-imperative-is-so-much-easier?Key=9fbfa6bd-acd3-4125-8829-5108c811ea41</link><guid>http://ayende.com/4495/sometimes-imperative-is-so-much-easier?Key=9fbfa6bd-acd3-4125-8829-5108c811ea41</guid><pubDate>Tue, 11 May 2010 09:00:00 GMT</pubDate></item><item><title>I like strong typing and compilation errors</title><description>&lt;p&gt;Today I had to modify a piece of JavaScript code. The code used return a single string, and I needed to modify it to return an array of objects. Using C#, it would have been easy, change the return type, hit the compile button, fix the errors, rinse &amp;amp; repeat until it compiles.&lt;/p&gt;  &lt;p&gt;In JavaScript code, however, it was much more complex, I had to find all the places where that method was called, and that particular parameter would pass unchanged throughout several functions before it was used, so I had to track &lt;em&gt;that&lt;/em&gt; down. Pretty annoying.&lt;/p&gt;  &lt;p&gt;And since I know that I’ll get questions on that, here is the actual example:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="csharpcode"&gt;getDocument: &lt;span class="kwrd"&gt;function&lt;/span&gt; (id, operation, successCallback) {
    $.ajax({
        url: settings.server + &lt;span class="str"&gt;'docs/'&lt;/span&gt; + id,
        dataType: &lt;span class="str"&gt;'json'&lt;/span&gt;,
        complete: &lt;span class="kwrd"&gt;function&lt;/span&gt;(xhr) {
            &lt;span class="kwrd"&gt;switch&lt;/span&gt;(xhr.status) 
            {
                &lt;span class="kwrd"&gt;case&lt;/span&gt; 200:
                    &lt;span class="kwrd"&gt;var&lt;/span&gt; data = JSON.parse(xhr.responseText);
                    &lt;span class="kwrd"&gt;var&lt;/span&gt; etag = xhr.getResponseHeader(&lt;span class="str"&gt;"Etag"&lt;/span&gt;);
                    &lt;span class="kwrd"&gt;var&lt;/span&gt; template = xhr.getResponseHeader(&lt;span class="str"&gt;'Raven-'&lt;/span&gt; + operation + &lt;span class="str"&gt;'-Template'&lt;/span&gt;);
                    successCallback(data, etag, template);
                    &lt;span class="kwrd"&gt;break&lt;/span&gt;;
                &lt;span class="kwrd"&gt;case&lt;/span&gt; 404:
                    successCallback(&lt;span class="kwrd"&gt;null&lt;/span&gt;, &lt;span class="kwrd"&gt;null&lt;/span&gt;, &lt;span class="kwrd"&gt;null&lt;/span&gt;);
                    &lt;span class="kwrd"&gt;break&lt;/span&gt;;
            }
        }
    });
},&lt;/pre&gt;
  &lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;/blockquote&gt;

&lt;p&gt;I needed to change the template variable from to a dictionary of headers, since it needed to be processed elsewhere.&lt;/p&gt;

&lt;p&gt;As for where it was actually used, here is one such example, which shows several layer of indirection (because of continuations):&lt;/p&gt;

&lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Ilikestrongtypingandcompilationerrors_114FE/image_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Ilikestrongtypingandcompilationerrors_114FE/image_thumb.png" width="895" height="120" /&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://ayende.com/4486/i-like-strong-typing-and-compilation-errors?Key=e3205a66-91bb-4f5a-b63c-f80113024183</link><guid>http://ayende.com/4486/i-like-strong-typing-and-compilation-errors?Key=e3205a66-91bb-4f5a-b63c-f80113024183</guid><pubDate>Wed, 05 May 2010 16:42:00 GMT</pubDate></item><item><title>I love ConcurrentDictionary!</title><description>&lt;p&gt;Not just because it is concurrent, because of this wonderful method:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Storage : IStorage
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; ConcurrentDictionary&amp;lt;Guid, ConcurrentDictionary&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;, List&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;&amp;gt; values =
        &lt;span class="kwrd"&gt;new&lt;/span&gt; ConcurrentDictionary&amp;lt;Guid, ConcurrentDictionary&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;, List&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;&amp;gt;();

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Store(Guid taskId, &lt;span class="kwrd"&gt;int&lt;/span&gt; level, &lt;span class="kwrd"&gt;object&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt;)
    {
        values.GetOrAdd(taskId, guid =&amp;gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ConcurrentDictionary&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;, List&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;())
            .GetOrAdd(level, i =&amp;gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt;())
            .Add(&lt;span class="kwrd"&gt;value&lt;/span&gt;);
    }
}&lt;/pre&gt;
  &lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;/blockquote&gt;

&lt;p&gt;Doing this with Dictionary is always a pain, but this is an extremely natural way of doing things.&lt;/p&gt;</description><link>http://ayende.com/4442/i-love-concurrentdictionary?Key=56aa8405-2cd1-46c6-a93b-65f159430912</link><guid>http://ayende.com/4442/i-love-concurrentdictionary?Key=56aa8405-2cd1-46c6-a93b-65f159430912</guid><pubDate>Mon, 22 Mar 2010 10:00:00 GMT</pubDate></item><item><title>Challenge: Robust enumeration over external code</title><description>&lt;p&gt;Here is an interesting little problem:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Program
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main()
    {
        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i &lt;span class="kwrd"&gt;in&lt;/span&gt; RobustEnumerating(Enumerable.Range(0, 10), FaultyFunc))
        {
            Console.WriteLine(i);
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; RobustEnumerating&amp;lt;T&amp;gt;(
        IEnumerable&amp;lt;T&amp;gt; input,Func&amp;lt;IEnumerable&amp;lt;T&amp;gt;, IEnumerable&amp;lt;T&amp;gt;&amp;gt; func)
    {
        &lt;span class="rem"&gt;// how to do this?&lt;/span&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; func(input);

    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; IEnumerable&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; FaultyFunc(IEnumerable&amp;lt;&lt;span class="kwrd"&gt;int&lt;/span&gt;&amp;gt; source)
    {
        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i &lt;span class="kwrd"&gt;in&lt;/span&gt; source)
        {
            &lt;span class="kwrd"&gt;yield&lt;/span&gt; &lt;span class="kwrd"&gt;return&lt;/span&gt; i/(i%2);
        }
    }
}&lt;/pre&gt;
  &lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;/blockquote&gt;

&lt;p&gt;This code should &lt;em&gt;not&lt;/em&gt; throw, but print: &lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;1
    &lt;br /&gt;3

    &lt;br /&gt;5

    &lt;br /&gt;7

    &lt;br /&gt;9&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Can you make this happen? You can only change the RobustEnumerating method, nothing else in the code&lt;/p&gt;</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code?Key=0b798316-f71d-436f-adef-5512b230d822</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code?Key=0b798316-f71d-436f-adef-5512b230d822</guid><pubDate>Thu, 04 Mar 2010 01:54:00 GMT</pubDate></item><item><title>Instantiating interfaces</title><description>&lt;p&gt;How do you make this code legal?&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;var foo = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; IFoo(1);&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;And yes, IFoo &lt;em&gt;is&lt;/em&gt; an interfacae.&lt;/p&gt;

&lt;p&gt;The answer is quite simple, actually. It was there since C# 1.0, I am told, and I just stumbled upon it. Take a look at this code:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Program
{
	&lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] args)
	{
		var foo = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; IFoo(1);
		foo.Do();
	}
}

[
	ComImport, 
	Guid("&lt;span style="color: #8b0000"&gt;C906C002-B214-40d7-8941-F223868B39A5&lt;/span&gt;"), 
	CoClass(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(FooImpl))
]
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IFoo
{
	&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Do();
}

&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; FooImpl : IFoo
{
	&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i;

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; FooImpl(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i)
	{
		&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.i = i;
	}

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Do()
	{
		Console.WriteLine(i);	
	}
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;We have an interface, and we specify the co class that implements it and is the default implementation. The rest is just required to make the compiler happy about it.&lt;/p&gt;

&lt;p&gt;What it &lt;em&gt;means&lt;/em&gt;, in turn, is that you can &lt;em&gt;instantiate an interface &lt;/em&gt;and have a default implementation selected. You can even use constructor parameters. It has quite a lot of implications, if you think about it right. &lt;/p&gt;

&lt;p&gt;Not sure it is a wise feature to use, but it is certainly an interesting tidbit.&lt;/p&gt;</description><link>http://ayende.com/4121/instantiating-interfaces?Key=f6a6b522-830f-40d7-b9cb-2ef30deabc42</link><guid>http://ayende.com/4121/instantiating-interfaces?Key=f6a6b522-830f-40d7-b9cb-2ef30deabc42</guid><pubDate>Fri, 14 Aug 2009 23:50:51 GMT</pubDate></item><item><title>Dictionary&lt;Enum,T&gt; Puzzler</title><description>&lt;p&gt;A while ago in the NHibernate mailing list we got a report that NHibernate is making use of a dictionary with an enum as the key, and that is causing a big performance issue.&lt;/p&gt;  &lt;p&gt;The response was almost unanimous, I think, “what?! how can that be?!!?”. Several people went in to and tried to figure out what is going on there. The answer is totally non oblivious, Dictionary&amp;lt;K,V&amp;gt; force boxing for any value type that is used as the key.&lt;/p&gt;  &lt;p&gt;That sound completely contradictory to what you would expect, after all, one of the major points in generics was the elimination of boxing, so what happened?&lt;/p&gt;  &lt;p&gt;Well, the issue is that Dictionary&amp;lt;K,V&amp;gt; has to compare the keys, and for that, it must make some assumptions about the actual key. It is abstracted into EqualityComparer, and that is where the actual problem starts. EqualityComparer has some special cases for the common types (anything that is IEquatable&amp;lt;T&amp;gt;, which most of the usual suspects implements), to speed this up.&lt;/p&gt;  &lt;p&gt;The problem is that the fall back is to an &lt;em&gt;ObjectComparer&lt;/em&gt;, and that, of course, will box any value type.&lt;/p&gt;  &lt;p&gt;And enum does not implements IEquatable&amp;lt;T&amp;gt;…&lt;/p&gt;  &lt;p&gt;Omer has a &lt;a href="http://www.codeproject.com/KB/cs/EnumComparer.aspx"&gt;good coverage&lt;/a&gt; on the subject, with really impressive results. Take a look at his results.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/DictionaryEnumTPuzzler_11F77/image_2.png"&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="332" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/DictionaryEnumTPuzzler_11F77/image_thumb.png" width="457" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;I am not going to steal his thunder, but I suggest going over and reading the &lt;a href="http://www.codeproject.com/KB/cs/EnumComparer.aspx"&gt;code&lt;/a&gt;, it is very elegant.&lt;/p&gt;</description><link>http://ayende.com/3885/dictionary-enum-t-puzzler?Key=b1eceee0-64b2-4683-90be-057bda39e89e</link><guid>http://ayende.com/3885/dictionary-enum-t-puzzler?Key=b1eceee0-64b2-4683-90be-057bda39e89e</guid><pubDate>Sat, 21 Feb 2009 04:26:38 GMT</pubDate></item><item><title>Elegant code</title><description>&lt;p&gt;I just like this code, so I thought I would publish it.&lt;/p&gt;  &lt;blockquote&gt;   &lt;div&gt;     &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;       &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ArrayExtension&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; T[] GetOtherElementsFromElement&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; T[] array , T element)&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt;     {&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt;         var index = Array.IndexOf(array, element);&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (index == -1)&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   7:&lt;/span&gt;             &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; array;&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   8:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; array.Skip(index + 1).Union(array.Take(index)).ToArray();&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   9:&lt;/span&gt;     }&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  10:&lt;/span&gt; }&lt;/pre&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;And the unit test:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;div&gt;
    &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;
      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ReplicationUnitTest&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt;     [Fact]&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Will_distribute_work_starting_with_next_node()&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt;     {&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt;         var nodes = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt;[] { 1, 2, 3 };&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   7:&lt;/span&gt;         Assert.Equal(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;[] { 3, 1 }, nodes.GetOtherElementsFromElement(2));&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   8:&lt;/span&gt;         Assert.Equal(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;[] { 1, 2 }, nodes.GetOtherElementsFromElement(3));&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;   9:&lt;/span&gt;         Assert.Equal(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;[] { 2, 3 }, nodes.GetOtherElementsFromElement(1));&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  10:&lt;/span&gt;         Assert.Equal(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;[] { 1, 2, 3 }, nodes.GetOtherElementsFromElement(4));&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  11:&lt;/span&gt;     }&lt;/pre&gt;

      &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #606060"&gt;  12:&lt;/span&gt; }&lt;/pre&gt;
    &lt;/div&gt;
  &lt;/div&gt;&lt;/blockquote&gt;</description><link>http://ayende.com/3857/elegant-code?Key=72790876-a678-4bf2-a0de-b42826c5964b</link><guid>http://ayende.com/3857/elegant-code?Key=72790876-a678-4bf2-a0de-b42826c5964b</guid><pubDate>Sat, 07 Feb 2009 18:35:33 GMT</pubDate></item><item><title>Why NH Prof isn't functional</title><description>&lt;p&gt;One of the things that I wanted to do with NH Prof is to build it in a way that would be very close to the Erlang way. That is, functional, immutable message passing. After spending some time trying to do this, I backed off, and used mutable OO with message passing.&lt;/p&gt;
&lt;p&gt;The reason for that is quite simple. State.&lt;/p&gt;
&lt;p&gt;Erlang can get away with being functional language with immutable state because it has a framework that manages that state around, and allow you to replace your state all the time. With C#, while I can create immutable data structures, if I want to actually create a large scale application using this manner, I have to write the state management framework, which is something that I didn't feel like doing.&lt;/p&gt;
&lt;p&gt;Instead, I am using a more natural model for C#, and using the bus model to manage thread safety and multi threading scenarios.&lt;/p&gt;
</description><link>http://ayende.com/3708/why-nh-prof-isnt-functional?Key=a0408ffe-2680-4db5-b06d-b13c5557f29d</link><guid>http://ayende.com/3708/why-nh-prof-isnt-functional?Key=a0408ffe-2680-4db5-b06d-b13c5557f29d</guid><pubDate>Tue, 18 Nov 2008 13:57:33 GMT</pubDate></item><item><title>Making code easier to read</title><description>&lt;p&gt;I didn't like this code:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Makingcodeeasiertoread_11A2B/image_2.png"&gt;&lt;img height="79" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Makingcodeeasiertoread_11A2B/image_thumb.png" width="667" border="0" /&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;I find this much easier to read:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Makingcodeeasiertoread_11A2B/image_4.png"&gt;&lt;img height="73" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Makingcodeeasiertoread_11A2B/image_thumb_1.png" width="640" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://ayende.com/3697/making-code-easier-to-read?Key=e884b46b-34e6-4ff0-950c-c1862ab5ca31</link><guid>http://ayende.com/3697/making-code-easier-to-read?Key=e884b46b-34e6-4ff0-950c-c1862ab5ca31</guid><pubDate>Thu, 13 Nov 2008 18:03:59 GMT</pubDate></item><item><title>A case study of bad API design: ASP.Net MVC Routing</title><description>&lt;p /&gt; &lt;p&gt;I am doing a spike in ASP.Net MVC now (and I'll talk about this at length at another time). I hit the wall when I wanted to do something that is trivially simple in MonoRail, limit a routing parameter to be a valid integer.&lt;/p&gt; &lt;p&gt;Luckily, just looking at the API signature told me that this is a supported scenario:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/AcasestudyofbadAPIdesignAS.NetMVCRouting_C865/image_2.png"&gt;&lt;img height="207" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/AcasestudyofbadAPIdesignAS.NetMVCRouting_C865/image_thumb.png" width="860" border="0" /&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Unfortunately, that is &lt;em&gt;all&lt;/em&gt; that it told me. This method accept an object. And there is no hint of documentation to explain what I am suppose to do with it. A bit of thinking suggested that I am probably supposed to pass an anonymous type with the key as the route parameter and the value is some sort of a constraint. But &lt;em&gt;what sort&lt;/em&gt; of a constraint. &lt;/p&gt; &lt;p&gt; Type information is one of those things that static language actually do, and from experience in both dynamic and static languages, while it is often a PITA to specify types, it actually help for people who read the code. Not often, I'll admit, but it is helpful for the uninitiated.&lt;/p&gt; &lt;p&gt;I am... unused to having this type of problem in C#. &lt;/p&gt; &lt;p&gt;So I did what any developer would do, hit google and tried to find some information about it. Didn't work.&lt;/p&gt; &lt;p&gt;I pulled reflector and started to track down what is going on there. Following a maze of untyped paths that I have not seen the like since the 1.1 days, I finally figured out that the value that I need to push is an instance of IRouteConstraint.&lt;/p&gt; &lt;p&gt;Obvious, isn't it?&lt;/p&gt; &lt;p&gt;In short, and the reason of this post. I am seeing a lot of parameter signatures that look like that, and have barely defined semantics. I would file this under C#.Abuse();&lt;/p&gt;</description><link>http://ayende.com/3673/a-case-study-of-bad-api-design-asp-net-mvc-routing?Key=f11ee531-ebb6-41dd-b85c-3cc7143829d1</link><guid>http://ayende.com/3673/a-case-study-of-bad-api-design-asp-net-mvc-routing?Key=f11ee531-ebb6-41dd-b85c-3cc7143829d1</guid><pubDate>Wed, 05 Nov 2008 19:16:08 GMT</pubDate></item><item><title>Reading MEF code</title><description>&lt;p&gt;Okay, here is the deal. There is a feature in MEF that I find interesting, the ability to dynamically recompose the imports that an instance have. Well, that is not accurate. that doesn't really interest me. What does interest me is some of the implementation details. Let me explain a bit better.&lt;/p&gt; &lt;p&gt;As I understand the feature, MEF can load the imports from an assembly, and if I drop another file into the appropriate location, it will be able to update my imports collection. Now, what I &lt;em&gt;am&lt;/em&gt; interested in is to know whatever MEF allow me to update file itself and update it on the fly. The reason that I am interested in that is to know how this is done without locking the file (loading an assembly usually locks the file, unless you use shadow copy assemblies, which means that you have to use a separate AppDomain). &lt;/p&gt; &lt;p&gt;As you can imagine, this is a very &lt;em&gt;specific&lt;/em&gt; need, and I want to go in, figure out if this is possible, and go away.&lt;/p&gt; &lt;p&gt;I started by checking out the MEF code:&lt;/p&gt; &lt;p&gt;svn co &lt;a href="https://mef.svn.codeplex.com/svn"&gt;https://mef.svn.codeplex.com/svn&lt;/a&gt; mef&lt;/p&gt; &lt;p&gt;I just &lt;em&gt;love&lt;/em&gt; the SVN integration that CodePlex has.&lt;/p&gt; &lt;p&gt;Now, the only way that MEF can implement this feature is by watching the file system, and that can be done using a FileSystemWatcher. Looking for that, I can see that it appears that DirectoryPartCatalog is using it, which isn't really surprising.&lt;/p&gt; &lt;p&gt;But, going there and reading the code gives us this:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/ReadingMEFcode_A44F/image_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="200" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/ReadingMEFcode_A44F/image_thumb.png" width="708" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;Note what &lt;em&gt;isn't&lt;/em&gt; there. there is no registration to Changed. This is likely not something that MEF supports.&lt;/p&gt; &lt;p&gt;Okay, one more try. Let us see how it actually load an assembly. We start from Export&amp;lt;T&amp;gt; and GetExportedObject() which calls to GetExportedObjectCore() which shell out to a delegate. Along the way I looked at CompositionException, just to make sure that it doesn't have the same problem as TypeLoadException and the hidden information, it doesn't.&lt;/p&gt; &lt;p&gt;I tried to follow the reference chain, but I quickly got lost, I then tried to figure out how MEF does delayed assembly loading, to see if it is doing anything special there, but I am currently hung at ComposablePartDefinition.Create, which seems promising, but it is accepting a delegate and no one is calling this.&lt;/p&gt; &lt;p&gt;So this looks like it for now.&lt;/p&gt;</description><link>http://ayende.com/3631/reading-mef-code?Key=780616db-8b74-403d-a79c-c7f28cb1b337</link><guid>http://ayende.com/3631/reading-mef-code?Key=780616db-8b74-403d-a79c-c7f28cb1b337</guid><pubDate>Sun, 05 Oct 2008 09:41:14 GMT</pubDate></item><item><title>More code review errors</title><description>&lt;p&gt;Take a look at this method:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Morecodereviewerrors_9C79/image_2.png"&gt;&lt;img height="482" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Morecodereviewerrors_9C79/image_thumb.png" width="854" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;Now, let us make this simple, shall we?&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Morecodereviewerrors_9C79/image_4.png"&gt;&lt;img height="301" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Morecodereviewerrors_9C79/image_thumb_1.png" width="842" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;Same meaning, and a significant reduction of complexity. Damn, but this is annoying.&lt;/p&gt;</description><link>http://ayende.com/3619/more-code-review-errors?Key=4540c7ef-5470-49c7-b4fc-67f3c2bdf168</link><guid>http://ayende.com/3619/more-code-review-errors?Key=4540c7ef-5470-49c7-b4fc-67f3c2bdf168</guid><pubDate>Tue, 30 Sep 2008 07:08:03 GMT</pubDate></item><item><title>Common issues found in code review</title><description>&lt;p&gt;I am going over a code base that I haven't seen in a while, and I am familiarizing myself with it by doing a code review to see that I understand what the code is doing now.&lt;/p&gt; &lt;p&gt;I am going to post code samples of changes that I made, along with some explanations.&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_2.png"&gt;&lt;img height="168" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_thumb.png" width="452" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;This code can be improved by introducing a guard clause, like this:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_6.png"&gt;&lt;img height="141" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_thumb_2.png" width="396" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;This reduce nesting and make the code easier to read in the long run (no nesting).&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_8.png"&gt;&lt;img height="304" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_thumb_3.png" width="627" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;I hope you recognize the issue. The code is using reflection to do an operation that is already built into the CLR.&lt;/p&gt; &lt;p&gt;This is much better:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_10.png"&gt;&lt;img height="307" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_thumb_4.png" width="513" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;Of course, there is another issue here, why the hell do we have those if statement on type instead of pushing this into polymorphic behavior. No answer yet, I am current just doing blind code review.&lt;/p&gt; &lt;p&gt;Here is another issue, using List explicitly:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_12.png"&gt;&lt;img height="72" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_thumb_5.png" width="567" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;It is generally better to rely on the most abstract type that you can use:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_16.png"&gt;&lt;img height="71" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_thumb_7.png" width="567" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;This is a matter of style more than anything else, but it drives me crazy:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_20.png"&gt;&lt;img height="223" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_thumb_9.png" width="504" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;I much rather have this:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_22.png"&gt;&lt;img height="289" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_thumb_10.png" width="513" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;Note that I added braces for both clauses, because it also bother me if one has it and the other doesn't.&lt;/p&gt; &lt;p&gt;Another issue is hanging ifs:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_24.png"&gt;&lt;img height="183" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_thumb_11.png" width="374" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;Which we can rewrite as:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_26.png"&gt;&lt;img height="138" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Commonissuesfoundincodereview_36C2/image_thumb_12.png" width="325" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;I think that this is enough for now...&lt;/p&gt;</description><link>http://ayende.com/3618/common-issues-found-in-code-review?Key=f1bba0a7-47b2-4971-b2ab-717419e3b5f6</link><guid>http://ayende.com/3618/common-issues-found-in-code-review?Key=f1bba0a7-47b2-4971-b2ab-717419e3b5f6</guid><pubDate>Mon, 29 Sep 2008 23:54:19 GMT</pubDate></item><item><title>Emulating Java Enums</title><description>&lt;p&gt;Java Enums are much more powerful than the ones that exists in the CLR. There are numerous ways of handling this issue, but here is my approach.&lt;/p&gt; &lt;p&gt;Given this enum (defined in Java):&lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;enum&lt;/span&gt; Layer {
    FIRST,
    SECOND;

	&lt;span style="color: #0000ff"&gt;public boolean&lt;/span&gt; isRightLayer(WorkType type) {
		&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; == FIRST &amp;amp;&amp;amp; type != WorkType.COLLECTION) &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;
		&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;this&lt;/span&gt; == SECOND &amp;amp;&amp;amp; type == WorkType.COLLECTION;
		}
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;And the C# version is:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Layer
{
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; Layer First = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Layer(&lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt;(WorkType type)
    {
        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; type != WorkType.Collection;
    });
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; Layer Second = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Layer(&lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt;(WorkType type)
    {
        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; type == WorkType.Collection;
    });

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;delegate&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; IsRightLayerDelegate(WorkType type);

    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; IsRightLayerDelegate isRightLayer;

    &lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; Layer(IsRightLayerDelegate isRightLayer)
    {
        &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.isRightLayer = isRightLayer;
    }

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; IsRightLayer(WorkType type)
    {
        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; isRightLayer(type);
    }
}&lt;/pre&gt;&lt;/blockquote&gt;</description><link>http://ayende.com/3617/emulating-java-enums?Key=7d16776e-a6fc-46c3-9114-912e40acd9fb</link><guid>http://ayende.com/3617/emulating-java-enums?Key=7d16776e-a6fc-46c3-9114-912e40acd9fb</guid><pubDate>Mon, 29 Sep 2008 20:52:43 GMT</pubDate></item><item><title>Setting out to break the compiler...</title><description>&lt;p&gt;I look at a bit of code that dealt with traversing expression an expression tree, using recursion, of course. The edge condition immediate popped to mind was unbounded expression. I decided to see if I can kill the compiler using this. Why? Because.&lt;/p&gt;  &lt;p&gt;The first thing to do is to find out how deep a stack we usually need. I wrote this simple test:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Program
{
	&lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] args)
	{
		Recursive(1);
	}

	&lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Recursive(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i)
	{
		Console.WriteLine(i);
		Recursive(i+1);
	}
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;The last result was: 79994&lt;/p&gt;

&lt;p&gt;Obviously this change based on how much stack space each function takes, but it is a good number to go with. I started with this code:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Program
{
	&lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] args)
	{
		&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;(var fw = File.CreateText("&lt;span style="color: #8b0000"&gt;text.txt&lt;/span&gt;"))
		{
			&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 80000; i++)
			{
				fw.Write("&lt;span style="color: #8b0000"&gt; a &amp;gt; &lt;/span&gt;"+i +"&lt;span style="color: #8b0000"&gt; &amp;amp;&amp;amp; &lt;/span&gt;");
				&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(i%10==0)
					fw.WriteLine();
			}
		}
	}
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;I then took the file ( slightly over 1 MB in size) and pasted the content to Visual Studio. &lt;/p&gt;

&lt;p&gt;That was a mistake:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Settingouttobreakthecompiler_1D2E/image_2.png"&gt;&lt;img height="217" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Settingouttobreakthecompiler_1D2E/image_thumb.png" width="368" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Okay, I can deal with this, let us try a different approach:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Program
{
	&lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] args)
	{
		&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;(var fw = File.CreateText("&lt;span style="color: #8b0000"&gt;text.cs&lt;/span&gt;"))
		{
			fw.WriteLine("&lt;span style="color: #8b0000"&gt;public class Program {&lt;/span&gt;");
			fw.WriteLine("&lt;span style="color: #8b0000"&gt;	static void Main(string[] args) {&lt;/span&gt;");
			fw.WriteLine("&lt;span style="color: #8b0000"&gt;		var a = -1;&lt;/span&gt;");
			fw.Write    ("&lt;span style="color: #8b0000"&gt;		var test = &lt;/span&gt;");
			&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 80000; i++)
			{
				fw.Write("&lt;span style="color: #8b0000"&gt; a &amp;gt; &lt;/span&gt;"+i +"&lt;span style="color: #8b0000"&gt; &amp;amp;&amp;amp; &lt;/span&gt;");
				&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(i%10==0)
					fw.WriteLine();
			}
			fw.WriteLine("&lt;span style="color: #8b0000"&gt; a &amp;lt; 0;&lt;/span&gt;");
			fw.WriteLine("&lt;span style="color: #8b0000"&gt;System.Console.WriteLine(test);&lt;/span&gt;");
			fw.WriteLine("&lt;span style="color: #8b0000"&gt;	}&lt;/span&gt;");
			fw.WriteLine("&lt;span style="color: #8b0000"&gt;}&lt;/span&gt;");

		}
	}
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Trying to compile &lt;em&gt;that&lt;/em&gt; produces:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;fatal error CS1647: An expression is too long or complex to compile near ''&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The help for CS1647 is:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;There was a stack overflow in the compiler processing your code. To resolve this error, simplify your code. If your code is valid, contact &lt;a href="http://msdn.microsoft.com/en-us/library/5a6ese6k%28VS.80%29.aspx"&gt;Product Support&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The is &lt;em&gt;valid&lt;/em&gt;, I guess, just not really reasonable. What is scary is that this is something that was added for 2.0, so at the 1.0 days, someone actually run into this issue.&lt;/p&gt;

&lt;p&gt;Some experimentation showed that the C# compiler can handle expressions composed of 23,553 nodes. Now it is the time to get to the next stage, now the code is this:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Program
{
	&lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] args)
	{
		&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;(var fw = File.CreateText("&lt;span style="color: #8b0000"&gt;text.cs&lt;/span&gt;"))
		{
			fw.WriteLine("&lt;span style="color: #8b0000"&gt;using System;&lt;/span&gt;");
			fw.WriteLine("&lt;span style="color: #8b0000"&gt;using System.Linq.Expressions;&lt;/span&gt;");
			fw.WriteLine("&lt;span style="color: #8b0000"&gt;public class Program {&lt;/span&gt;");
			fw.WriteLine("&lt;span style="color: #8b0000"&gt;	static void Main(string[] args) {&lt;/span&gt;");
			fw.Write    ("&lt;span style="color: #8b0000"&gt;		Expression&amp;lt;Predicate&amp;lt;int&amp;gt;&amp;gt; test = (a) =&amp;gt; &lt;/span&gt;");
			&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 11700; i++)
			{
				fw.Write("&lt;span style="color: #8b0000"&gt; a &amp;gt; &lt;/span&gt;"+i +"&lt;span style="color: #8b0000"&gt; &amp;amp;&amp;amp; &lt;/span&gt;");
				&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(i%10==0)
					fw.WriteLine();
			}
			fw.WriteLine("&lt;span style="color: #8b0000"&gt; a &amp;lt; 0;&lt;/span&gt;");
			fw.WriteLine("&lt;span style="color: #8b0000"&gt;System.Console.WriteLine(test);&lt;/span&gt;");
			fw.WriteLine("&lt;span style="color: #8b0000"&gt;	}&lt;/span&gt;");
			fw.WriteLine("&lt;span style="color: #8b0000"&gt;}&lt;/span&gt;");

		}
	}
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Note that I had to dramatically simplify the expression. Before it handled 23 thousands and change, but now it chokes on merely 12 thousands.&lt;/p&gt;

&lt;p&gt;What is really surprising is that after compiling the code, it is running and seems to do the expected thing. Amazing.&lt;/p&gt;

&lt;p&gt;Anyway, here is a completely useless post, but now I know that the C# compiler has well defined behavior for stack overflows. :-)&lt;/p&gt;</description><link>http://ayende.com/3528/setting-out-to-break-the-compiler?Key=219bfdc4-6a6b-4068-9c1a-04d6383cff0f</link><guid>http://ayende.com/3528/setting-out-to-break-the-compiler?Key=219bfdc4-6a6b-4068-9c1a-04d6383cff0f</guid><pubDate>Fri, 15 Aug 2008 23:05:08 GMT</pubDate></item><item><title>Not all objects are created equals</title><description>&lt;p&gt;I found something extremely surprising while profiling a project. Take a look at this piece of code:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;Stopwatch stop = Stopwatch.StartNew();
&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 1000000; i++)
{
	&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; WeakReference(&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;);
}
stop.Stop();
Console.WriteLine("&lt;span style="color: #8b0000"&gt;WeakRef: &lt;/span&gt;" + stop.ElapsedMilliseconds);

stop = Stopwatch.StartNew();
&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 1000000; i++)
{
	&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;('a', 5);
}
stop.Stop();
Console.WriteLine("&lt;span style="color: #8b0000"&gt;'aaaaa': &lt;/span&gt;" + stop.ElapsedMilliseconds);&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;On my machine, this has the following output:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;WeakRef: 980
    &lt;br /&gt;'aaaaa': 35&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Creating a WeakReference is much more costly than creating a normal object. Not surprising, when you think of it, WeakReference has deep ties to the CLR, but I couldn't really believe it when I saw it the first time.&lt;/p&gt;</description><link>http://ayende.com/3459/not-all-objects-are-created-equals?Key=828007d5-b151-4aae-9665-77ed53111768</link><guid>http://ayende.com/3459/not-all-objects-are-created-equals?Key=828007d5-b151-4aae-9665-77ed53111768</guid><pubDate>Mon, 28 Jul 2008 22:53:52 GMT</pubDate></item><item><title>Challenge: What does this code do?</title><description>&lt;p&gt;Without compiling this, can you answer me whatever this piece of code will compile? And if so, what does it do? &lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt; dummyVariable1 = 1;
&lt;font color="#0000ff"&gt;var&lt;/font&gt; dummyVariable2 = 3;
&lt;font color="#0000ff"&gt;var&lt;/font&gt; a = dummyVariable1&lt;br /&gt;+-+-+-+-+ + + + + + +-+-+-+-+-+ &lt;br /&gt;	dummyVariable2;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Oh, and I want to hear reasons, too.&lt;/p&gt;</description><link>http://ayende.com/3457/challenge-what-does-this-code-do?Key=e8416b6a-21ac-4496-bc07-a08f268e0e89</link><guid>http://ayende.com/3457/challenge-what-does-this-code-do?Key=e8416b6a-21ac-4496-bc07-a08f268e0e89</guid><pubDate>Sun, 27 Jul 2008 22:34:13 GMT</pubDate></item></channel></rss>