<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>C#</title>
        <link>http://ayende.com/Blog/category/500.aspx</link>
        <description>C#</description>
        <language>en-US</language>
        <copyright>Ayende Rahien</copyright>
        <managingEditor>Ayende@ayende.com</managingEditor>
        <generator>Subtext Version 2.0.0.0</generator>
        <item>
            <title>Challenge: Robust enumeration over external code</title>
            <link>http://ayende.com/Blog/archive/2010/03/04/challenge-robust-enumeration-over-external-code.aspx</link>
            <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;&lt;img src="http://ayende.com/Blog/aggbug/11349.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2010/03/04/challenge-robust-enumeration-over-external-code.aspx</guid>
            <pubDate>Thu, 04 Mar 2010 01:54:00 GMT</pubDate>
            <wfw:comment>http://ayende.com/Blog/comments/11349.aspx</wfw:comment>
            <comments>http://ayende.com/Blog/archive/2010/03/04/challenge-robust-enumeration-over-external-code.aspx#feedback</comments>
            <slash:comments>98</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/11349.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Instantiating interfaces</title>
            <link>http://ayende.com/Blog/archive/2009/08/15/instantiating-interfaces.aspx</link>
            <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;&lt;img src="http://ayende.com/Blog/aggbug/11042.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2009/08/15/instantiating-interfaces.aspx</guid>
            <pubDate>Fri, 14 Aug 2009 23:50:51 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2009/08/15/instantiating-interfaces.aspx#feedback</comments>
            <slash:comments>20</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/11042.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Dictionary&amp;lt;Enum,T&amp;gt; Puzzler</title>
            <link>http://ayende.com/Blog/archive/2009/02/21/dictionaryltenumtgt-puzzler.aspx</link>
            <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;&lt;img src="http://ayende.com/Blog/aggbug/10796.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2009/02/21/dictionaryltenumtgt-puzzler.aspx</guid>
            <pubDate>Sat, 21 Feb 2009 04:26:38 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2009/02/21/dictionaryltenumtgt-puzzler.aspx#feedback</comments>
            <slash:comments>7</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10796.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Elegant code</title>
            <link>http://ayende.com/Blog/archive/2009/02/07/elegant-code-again.aspx</link>
            <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;&lt;img src="http://ayende.com/Blog/aggbug/10768.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2009/02/07/elegant-code-again.aspx</guid>
            <pubDate>Sat, 07 Feb 2009 18:35:33 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2009/02/07/elegant-code-again.aspx#feedback</comments>
            <slash:comments>22</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10768.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Why NH Prof isn't functional</title>
            <link>http://ayende.com/Blog/archive/2008/11/18/why-nh-prof-isnt-functional.aspx</link>
            <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;
&lt;img src="http://ayende.com/Blog/aggbug/10619.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/11/18/why-nh-prof-isnt-functional.aspx</guid>
            <pubDate>Tue, 18 Nov 2008 13:57:33 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/11/18/why-nh-prof-isnt-functional.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10619.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Making code easier to read</title>
            <link>http://ayende.com/Blog/archive/2008/11/13/making-code-easier-to-read.aspx</link>
            <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;&lt;img src="http://ayende.com/Blog/aggbug/10608.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/11/13/making-code-easier-to-read.aspx</guid>
            <pubDate>Thu, 13 Nov 2008 18:03:59 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/11/13/making-code-easier-to-read.aspx#feedback</comments>
            <slash:comments>23</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10608.aspx</wfw:commentRss>
        </item>
        <item>
            <title>A case study of bad API design: ASP.Net MVC Routing</title>
            <link>http://ayende.com/Blog/archive/2008/11/05/a-case-study-of-bad-api-design-asp.net-mvc-routing.aspx</link>
            <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;&lt;img src="http://ayende.com/Blog/aggbug/10584.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/11/05/a-case-study-of-bad-api-design-asp.net-mvc-routing.aspx</guid>
            <pubDate>Wed, 05 Nov 2008 19:16:08 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/11/05/a-case-study-of-bad-api-design-asp.net-mvc-routing.aspx#feedback</comments>
            <slash:comments>23</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10584.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Reading MEF code</title>
            <link>http://ayende.com/Blog/archive/2008/10/05/reading-mef-code.aspx</link>
            <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;&lt;img src="http://ayende.com/Blog/aggbug/10542.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/10/05/reading-mef-code.aspx</guid>
            <pubDate>Sun, 05 Oct 2008 09:41:14 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/10/05/reading-mef-code.aspx#feedback</comments>
            <slash:comments>11</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10542.aspx</wfw:commentRss>
        </item>
        <item>
            <title>More code review errors</title>
            <link>http://ayende.com/Blog/archive/2008/09/30/more-code-review-errors.aspx</link>
            <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;&lt;img src="http://ayende.com/Blog/aggbug/10530.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/09/30/more-code-review-errors.aspx</guid>
            <pubDate>Tue, 30 Sep 2008 08:08:03 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/09/30/more-code-review-errors.aspx#feedback</comments>
            <slash:comments>20</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10530.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Common issues found in code review</title>
            <link>http://ayende.com/Blog/archive/2008/09/30/common-issues-found-in-code-review.aspx</link>
            <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;&lt;img src="http://ayende.com/Blog/aggbug/10529.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/09/30/common-issues-found-in-code-review.aspx</guid>
            <pubDate>Tue, 30 Sep 2008 00:54:19 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/09/30/common-issues-found-in-code-review.aspx#feedback</comments>
            <slash:comments>17</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10529.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>