<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://gregbeech.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Greg Beech's Tech Blog</title><link>http://gregbeech.com/blogs/tech/default.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2008 (Build: 30417.1769)</generator><item><title>Function currying in C#</title><link>http://gregbeech.com/blogs/tech/archive/2008/09/16/function-currying-in-c.aspx</link><pubDate>Tue, 16 Sep 2008 07:36:50 GMT</pubDate><guid isPermaLink="false">64b9f9d3-1b55-4372-890f-9a35fe961b72:341</guid><dc:creator>Greg Beech</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://gregbeech.com/blogs/tech/rsscomments.aspx?PostID=341</wfw:commentRss><comments>http://gregbeech.com/blogs/tech/archive/2008/09/16/function-currying-in-c.aspx#comments</comments><description>&lt;p&gt;C# does not automatically &lt;a title="Currying at Wikipedia" href="http://en.wikipedia.org/wiki/Currying"&gt;curry&lt;/a&gt; functions, so a function that multiplies two &lt;em&gt;double&lt;/em&gt;s together and returns the result must be assigned to a delegate that takes two arguments and returns a result:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;Func&lt;/span&gt;&amp;lt;&lt;span&gt;double&lt;/span&gt;, &lt;span&gt;double&lt;/span&gt;, &lt;span&gt;double&lt;/span&gt;&amp;gt; multiply = (a, b) =&amp;gt; a * b;&lt;/pre&gt;
&lt;p&gt;This seems reasonable, but unfortunately it means that if we want to define a partial application of the function where one argument is already supplied (for example we might want to create a function that calculates the circumference of a circle from its diameter by supplying &lt;em&gt;Math.PI&lt;/em&gt;) then we can&amp;#39;t do it directly. Attempting to write the following statement gives the compiler error &lt;em&gt;&amp;quot;Delegate &amp;#39;Func&amp;#39; does not take &amp;#39;1&amp;#39; arguments&amp;quot;&lt;/em&gt;:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;var&lt;/span&gt; circumferenceFromDiameter = multiply(&lt;span&gt;Math&lt;/span&gt;.PI);      &lt;span&gt;// hopefully type Func&amp;lt;double, double&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;What we actually want to achieve is a function that takes a &lt;em&gt;double&lt;/em&gt; and returns a function that takes another &lt;em&gt;double&lt;/em&gt; and returns the result of the two multiplied together. It is possible to write this in C#, but you have to change the function declaration from the intuitive &amp;#39;two arguments goes to one result&amp;#39; syntax to somewhat harder to grasp nested lambda expression syntax. This is shown below with annotated type information so you can see what&amp;#39;s going on (although &lt;a title="To var or not to var, implicit typing is the question" href="http://gregbeech.com/blogs/tech/archive/2008/03/24/to-var-or-not-to-var-implicit-typing-is-the-question.aspx"&gt;I actually think the type information is more of a distraction than a help&lt;/a&gt;).&lt;/p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;pre class="code"&gt;&lt;span&gt;Func&lt;/span&gt;&amp;lt;&lt;span&gt;double&lt;/span&gt;, &lt;span&gt;Func&lt;/span&gt;&amp;lt;&lt;span&gt;double&lt;/span&gt;, &lt;span&gt;double&lt;/span&gt;&amp;gt;&amp;gt; multiply = a =&amp;gt; b =&amp;gt; a * b;
&lt;span&gt;var&lt;/span&gt; circumferenceFromDiameter = multiply(&lt;span&gt;Math&lt;/span&gt;.PI);      &lt;span&gt;// type Func&amp;lt;double, double&amp;gt;
&lt;/span&gt;&lt;span&gt;var&lt;/span&gt; circumference = circumferenceFromDiameter(10.0);    &lt;span&gt;// type double&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;What would be useful are some methods for the &lt;em&gt;Func&amp;lt;&amp;gt;&lt;/em&gt; class that could perform the currying for us, and fortunately as it&amp;#39;s a mechanical translation these are quite easy to write. Shown below are extension methods for the 2, 3 and 4 argument versions of &lt;em&gt;Func&amp;lt;&amp;gt;&lt;/em&gt;; there isn&amp;#39;t one for the single argument version as this is already in curried form.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;Func
&lt;/span&gt;{
    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;Func&lt;/span&gt;&amp;lt;T1, &lt;span&gt;Func&lt;/span&gt;&amp;lt;T2, TResult&amp;gt;&amp;gt; Curry&amp;lt;T1, T2, TResult&amp;gt;(
        &lt;span&gt;this&lt;/span&gt; &lt;span&gt;Func&lt;/span&gt;&amp;lt;T1, T2, TResult&amp;gt; func)
    {
        &lt;span&gt;return&lt;/span&gt; arg1 =&amp;gt; arg2 =&amp;gt; func(arg1, arg2);
    }

    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;Func&lt;/span&gt;&amp;lt;T1, &lt;span&gt;Func&lt;/span&gt;&amp;lt;T2, &lt;span&gt;Func&lt;/span&gt;&amp;lt;T3, TResult&amp;gt;&amp;gt;&amp;gt; Curry&amp;lt;T1, T2, T3, TResult&amp;gt;(
        &lt;span&gt;this&lt;/span&gt; &lt;span&gt;Func&lt;/span&gt;&amp;lt;T1, T2, T3, TResult&amp;gt; func)
    {
        &lt;span&gt;return&lt;/span&gt; arg1 =&amp;gt; arg2 =&amp;gt; arg3 =&amp;gt; func(arg1, arg2, arg3);
    }

    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;Func&lt;/span&gt;&amp;lt;T1, &lt;span&gt;Func&lt;/span&gt;&amp;lt;T2, &lt;span&gt;Func&lt;/span&gt;&amp;lt;T3, &lt;span&gt;Func&lt;/span&gt;&amp;lt;T4, TResult&amp;gt;&amp;gt;&amp;gt;&amp;gt; Curry&amp;lt;T1, T2, T3, T4, TResult&amp;gt;(
        &lt;span&gt;this&lt;/span&gt; &lt;span&gt;Func&lt;/span&gt;&amp;lt;T1, T2, T3, T4, TResult&amp;gt; func)
    {
        &lt;span&gt;return&lt;/span&gt; arg1 =&amp;gt; arg2 =&amp;gt; arg3 =&amp;gt; arg4 =&amp;gt; func(arg1, arg2, arg3, arg4);
    }
}&lt;/pre&gt;
&lt;p&gt;We can now write the initial multiply expression as we&amp;#39;d prefer, and still be able to partially apply the function. It does take an extra line because you can&amp;#39;t directly apply the extension method to the lambda expression without specifying that it is typed as a &lt;em&gt;Func&amp;lt;&amp;gt;&lt;/em&gt;, and to do that in a single line results in a lot of ugly casting syntax.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;Func&lt;/span&gt;&amp;lt;&lt;span&gt;double&lt;/span&gt;, &lt;span&gt;double&lt;/span&gt;, &lt;span&gt;double&lt;/span&gt;&amp;gt; multiply = (a, b) =&amp;gt; a * b;
&lt;span&gt;var&lt;/span&gt; multiplyBy = multiply.Curry();                      &lt;span&gt;// type Func&amp;lt;double, Func&amp;lt;double, double&amp;gt;&amp;gt;
&lt;/span&gt;&lt;span&gt;var&lt;/span&gt; circumferenceFromDiameter = multiplyBy(&lt;span&gt;Math&lt;/span&gt;.PI);    &lt;span&gt;// type Func&amp;lt;double, double&amp;gt;
&lt;/span&gt;&lt;span&gt;var&lt;/span&gt; circumference = circumferenceFromDiameter(10.0);    &lt;span&gt;// type double&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Note that this approach doesn&amp;#39;t just facilitate writing local lambda expressions in a simpler way, it also allows you to partially apply library methods that you don&amp;#39;t have control over the signature of by assigning them to an appropriate &lt;em&gt;Func&amp;lt;&amp;gt;&lt;/em&gt; and then currying it. It&amp;#39;s not ideal because you have to add syntactic noise to do the currying manually, but it&amp;#39;s better than not being able to do it at all.&lt;/p&gt;
&lt;p&gt;Just for fun, here&amp;#39;s all you have to do to get the above scenario working in F#. We don&amp;#39;t need to manually curry the methods as it is done automatically. There&amp;#39;s also a lot less syntactic noise because F# doesn&amp;#39;t require you to declare the concrete type of lambda expressions; it performs type checking based on what parameter type they take and what they return which makes a lot more sense than the C# approach - if you&amp;#39;ve ever come across the &lt;em&gt;Predicate&amp;lt;T&amp;gt; != Func&amp;lt;T, bool&amp;gt;&lt;/em&gt; paradox then you&amp;#39;ll know what I mean.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;let&lt;/span&gt; multiply (a:double) b = a * b                   &lt;span&gt;// type double -&amp;gt; double -&amp;gt; double
&lt;/span&gt;&lt;span&gt;let&lt;/span&gt; circumferenceFromDiameter = multiply Math.PI    &lt;span&gt;// type double -&amp;gt; double
&lt;/span&gt;&lt;span&gt;let&lt;/span&gt; circumference = circumferenceFromDiameter 10.0&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://gregbeech.com/aggbug.aspx?PostID=341" width="1" height="1"&gt;</description><category domain="http://gregbeech.com/blogs/tech/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Functional+Programming/default.aspx">Functional Programming</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/F_2300_/default.aspx">F#</category></item><item><title>Images wrong or missing in blog posts</title><link>http://gregbeech.com/blogs/tech/archive/2008/09/09/images-wrong-or-missing-in-blog-posts.aspx</link><pubDate>Tue, 09 Sep 2008 00:17:48 GMT</pubDate><guid isPermaLink="false">64b9f9d3-1b55-4372-890f-9a35fe961b72:329</guid><dc:creator>Greg Beech</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://gregbeech.com/blogs/tech/rsscomments.aspx?PostID=329</wfw:commentRss><comments>http://gregbeech.com/blogs/tech/archive/2008/09/09/images-wrong-or-missing-in-blog-posts.aspx#comments</comments><description>&lt;p&gt;Recently I&amp;#39;ve been using more images to illustrate blog posts, and unfortunately have run into an &lt;a title="Windows LiveWriter Image Corruption Issue at Community Server Forums" href="http://dev.communityserver.com/forums/t/500673.aspx"&gt;issue with Windows Live Writer and Community Server 2008&lt;/a&gt;. The full details are in that forum, including a workaround and a fix, but essentially if the image is pasted from the clipboard without saving it to disk first Writer generates file names that overlap, and Community Server uses the same folder for all images, so images from old blog posts get randomly overwritten.&lt;/p&gt; &lt;p&gt;Fortunately I managed to recover all of the image files from the post history so have been able to fix up all the posts on-site (I think - if you see any that are wrong please let me know) but I have a feeling that anyone subscribing to the RSS feed will still have broken or wrong image links. I&amp;#39;ve also patched my server so hopefully won&amp;#39;t have any more problems in the future.&lt;/p&gt; &lt;p&gt;Sorry for any confusion or inconvenience this may have caused.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://gregbeech.com/aggbug.aspx?PostID=329" width="1" height="1"&gt;</description><category domain="http://gregbeech.com/blogs/tech/archive/tags/Off+Topic/default.aspx">Off Topic</category></item><item><title>Determining the bias of a shuffle algorithm</title><link>http://gregbeech.com/blogs/tech/archive/2008/09/09/determining-the-bias-of-a-shuffle-algorithm.aspx</link><pubDate>Mon, 08 Sep 2008 23:17:59 GMT</pubDate><guid isPermaLink="false">64b9f9d3-1b55-4372-890f-9a35fe961b72:327</guid><dc:creator>Greg Beech</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://gregbeech.com/blogs/tech/rsscomments.aspx?PostID=327</wfw:commentRss><comments>http://gregbeech.com/blogs/tech/archive/2008/09/09/determining-the-bias-of-a-shuffle-algorithm.aspx#comments</comments><description>&lt;p&gt;Last time I wrote &lt;a title="Shuffle and TakeRandom extension methods for IEnumerable&amp;lt;T&amp;gt;" href="http://gregbeech.com/blogs/tech/archive/2008/09/03/shuffle-and-takerandom-extension-methods-for-ienumerable-lt-t-gt.aspx"&gt;&lt;em&gt;Shuffle&lt;/em&gt; and &lt;em&gt;TakeRandom&lt;/em&gt; extension methods for &lt;em&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/em&gt;&lt;/a&gt; which use the Fisher-Yates algorithm to perform an unbiased shuffle of a sequence and take a random subset from it. In order to optimise the performance of the &lt;em&gt;TakeRandom&lt;/em&gt; method I had to reverse the documented implementation of the shuffle so it produced the results at the start of the sequence rather than the end, and thus allow it to be short-circuited once enough values had been randomised. &lt;/p&gt; &lt;p&gt;It all looks fine in theory, but as even simple algorithms are notoriously hard to implement correctly I wouldn&amp;#39;t have been happy publishing it without testing to ensure that (a) the shuffle works, and (b) it really is unbiased.&lt;/p&gt; &lt;p&gt;One way to determine the bias is to shuffle integer sequences and then add the values at corresponding indexes, giving the total value at each index. If the shuffle is unbiased then each value should appear at each index an equal number of times, and so the total value calculated for each index should be the same. If we compute the &lt;a title="Relative standard deviation at Wikipedia" href="http://en.wikipedia.org/wiki/Relative_standard_deviation"&gt;percentage relative standard deviation&lt;/a&gt; (%RSD) of the totals, a value of zero indicates an unbiased algorithm and any non-zero value indicates biased one, with larger values being more biased.&lt;/p&gt; &lt;p&gt;Below are a couple of extension methods to compute the &lt;a title="Standard deviation at Wikipedia" href="http://en.wikipedia.org/wiki/Standard_deviation"&gt;standard deviation&lt;/a&gt; and relative standard deviation for sequences of integers. It&amp;#39;s impressive how terse the syntax is to do this with the Linq extensions; however, it is frustrating that in C# there is no way to create them as open generic methods and indicate that they apply to anything that can have mathematical operations performed on it, so if you want versions of these that work with sequences of &lt;em&gt;float &lt;/em&gt;you have to cut-and-paste the implementations and replace the &lt;em&gt;int&lt;/em&gt; keyword.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;Enumerable
&lt;/span&gt;{
    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;double&lt;/span&gt; StandardDeviation(&lt;span&gt;this&lt;/span&gt; &lt;span&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span&gt;int&lt;/span&gt;&amp;gt; values)
    {
        &lt;span&gt;return&lt;/span&gt; StandardDeviationInternal(values, values.Average());
    }

    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;double&lt;/span&gt; RelativeStandardDeviation(&lt;span&gt;this&lt;/span&gt; &lt;span&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span&gt;int&lt;/span&gt;&amp;gt; values)
    {
        &lt;span&gt;var&lt;/span&gt; average = values.Average();
        &lt;span&gt;var&lt;/span&gt; standardDeviation = StandardDeviationInternal(values, average);
        &lt;span&gt;return&lt;/span&gt; (standardDeviation * 100.0) / average;
    }

    &lt;span&gt;private&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;double&lt;/span&gt; StandardDeviationInternal(&lt;span&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span&gt;int&lt;/span&gt;&amp;gt; values, &lt;span&gt;double&lt;/span&gt; average)
    {
        &lt;span&gt;return&lt;/span&gt; &lt;span&gt;Math&lt;/span&gt;.Sqrt(values.Select(value =&amp;gt; &lt;span&gt;Math&lt;/span&gt;.Pow(value - average, 2.0)).Average());
    }
}&lt;/pre&gt;
&lt;p&gt;To compute the %RSD we need to consider the effect that the randomness has on the distribution of the values. With a low number of sequences shuffled there may be a uneven distribution of randomness leading to invalid results, however as the number of sequences shuffled increases the randomness should be more evenly distributed and cancel itself out (assuming an unbiased random number generator). From this we can see that as the number of sequences shuffled approaches infinity, the calculated %RSD approaches the correct answer.&lt;/p&gt;
&lt;p&gt;Unfortunately we haven&amp;#39;t got time to shuffle an infinite number of sequences, so my test application makes do with shuffling a logarithmic range from 100 to 100,000,000 sequences of the numbers 1..10, printing the results to the console (and yes, I know I could have used a &lt;em&gt;for&lt;/em&gt; loop):&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;class&lt;/span&gt; &lt;span&gt;Program
&lt;/span&gt;{
    &lt;span&gt;static&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; Main()
    {
        ShuffleTest(100);
        ShuffleTest(1000);
        ShuffleTest(10000);
        ShuffleTest(100000);
        ShuffleTest(1000000);
        ShuffleTest(10000000);
        ShuffleTest(100000000);
        &lt;span&gt;Console&lt;/span&gt;.ReadLine();
    }

    &lt;span&gt;private&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; ShuffleTest(&lt;span&gt;int&lt;/span&gt; iterations)
    {
        &lt;span&gt;var&lt;/span&gt; totals = &lt;span&gt;new&lt;/span&gt; &lt;span&gt;int&lt;/span&gt;[10];
        &lt;span&gt;var&lt;/span&gt; generator = System.Linq.&lt;span&gt;Enumerable&lt;/span&gt;.Range(1, 10);
        &lt;span&gt;for&lt;/span&gt; (&lt;span&gt;var&lt;/span&gt; iteration = 0; iteration &amp;lt; iterations; iteration++)
        {
            &lt;span&gt;var&lt;/span&gt; index = 0;
            &lt;span&gt;foreach&lt;/span&gt; (&lt;span&gt;var&lt;/span&gt; value &lt;span&gt;in&lt;/span&gt; generator.Shuffle())
            {
                totals[index] += value;
                index++;
            }
        }

        &lt;span&gt;Console&lt;/span&gt;.WriteLine(&lt;span&gt;&amp;quot;{0,-9} iterations =&amp;gt; {1:0.000} %RSD&amp;quot;&lt;/span&gt;, iterations, totals.RelativeStandardDeviation());
    }
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Plotting the results in Excel shows that as the number of iterations increases the %RSD does approach zero; by one hundred million iterations the measured %RSD is only 0.005. Note that if the %RSD scale is also plotted as logarithmic the trend-line is straight, but I think most people find the non-logarithmic scale easier to understand at a glance. We can therefore say with confidence that the shuffle implementation from the previous entry is completely unbiased.&lt;/p&gt;
&lt;p align="center"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="301" alt="Convergence of Fisher-Yates shuffle" src="http://gregbeech.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/tech/image_5F00_6.png" width="509" border="0" /&gt;&amp;nbsp; &lt;/p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;If you want to compare this against a biased algorithm then shown below is a common mistake people make when implementing the Fisher-Yates shuffle, where the random range is computed from &lt;em&gt;0&lt;/em&gt; rather than &lt;em&gt;n:&lt;/em&gt;&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;private&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; ShuffleInternal&amp;lt;T&amp;gt;(T[] array, &lt;span&gt;int&lt;/span&gt; count)
{
    &lt;span&gt;// this algorithm has a mistake in the random.Next call: 0 should be n&lt;/span&gt;&lt;span&gt;
&lt;/span&gt;    &lt;span&gt;for&lt;/span&gt; (&lt;span&gt;var&lt;/span&gt; n = 0; n &amp;lt; count; n++)
    {
        &lt;span&gt;var&lt;/span&gt; k = random.Next(&lt;strong&gt;0&lt;/strong&gt;, array.Length);
        &lt;span&gt;var&lt;/span&gt; temp = array[n];
        array[n] = array[k];
        array[k] = temp;
    }

    &lt;span&gt;return&lt;/span&gt; array;
}&lt;/pre&gt;
&lt;p&gt;Plotting the results on the same chart as the correctly implemented algorithm shows that this one approaches a value of approximately 3.17 %RSD and so is proven to be biased: &lt;/p&gt;
&lt;p align="center"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="301" alt="Convergence of Fisher-Yates relative standard deviation" src="http://gregbeech.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/tech/image_5F00_3.png" width="584" border="0" /&gt; &lt;/p&gt;
&lt;p&gt;This isn&amp;#39;t the only approach to prove that a shuffle algorithm implementation is unbiased, but it is quick to implement and run, and gives a high level of confidence in the results. You can, and should, test any other shuffle algorithm you implement in a similar manner.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://gregbeech.com/aggbug.aspx?PostID=327" width="1" height="1"&gt;</description><category domain="http://gregbeech.com/blogs/tech/archive/tags/Performance/default.aspx">Performance</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Testing/default.aspx">Testing</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Linq/default.aspx">Linq</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Algorithms/default.aspx">Algorithms</category></item><item><title>Shuffle and TakeRandom extension methods for IEnumerable&lt;T&gt;</title><link>http://gregbeech.com/blogs/tech/archive/2008/09/03/shuffle-and-takerandom-extension-methods-for-ienumerable-lt-t-gt.aspx</link><pubDate>Wed, 03 Sep 2008 00:13:08 GMT</pubDate><guid isPermaLink="false">64b9f9d3-1b55-4372-890f-9a35fe961b72:324</guid><dc:creator>Greg Beech</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://gregbeech.com/blogs/tech/rsscomments.aspx?PostID=324</wfw:commentRss><comments>http://gregbeech.com/blogs/tech/archive/2008/09/03/shuffle-and-takerandom-extension-methods-for-ienumerable-lt-t-gt.aspx#comments</comments><description>&lt;p&gt;It&amp;#39;s a fairly common practice on web sites to display sets of randomly ordered items, whether it&amp;#39;s a selection of your friends, items related to the one you&amp;#39;re looking at, or even something as simple as a tag cloud. Sometimes the entire set of items is randomised, and sometimes you want to take a random selection from the set and display that (the latter is typically done so you can cache a larger set of data and then take items from it without going to the database again).&lt;/p&gt; &lt;p&gt;The &lt;em&gt;System.Linq.Enumerable&lt;/em&gt; class provides a lot of methods to process collections, but it appears that randomisation is beyond its remit. As such I wrote a couple of extension methods to shuffle a collection and take a random selection from it, which use the &lt;a title="Fisher-Yates shuffle at Wikipedia" href="http://en.wikipedia.org/wiki/Fisher-Yates_shuffle"&gt;Fisher-Yates algorithm&lt;/a&gt; to give an unbiased shuffle in &lt;em&gt;O(n)&lt;/em&gt; time. &lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;Enumerable
&lt;/span&gt;{
    &lt;span&gt;private&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;readonly&lt;/span&gt; &lt;span&gt;Random&lt;/span&gt; random = &lt;span&gt;new&lt;/span&gt; &lt;span&gt;Random&lt;/span&gt;();

    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Shuffle&amp;lt;T&amp;gt;(&lt;span&gt;this&lt;/span&gt; &lt;span&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; source)
    {
        &lt;span&gt;// Durstenfeld implementation of the Fisher-Yates algorithm for an O(n) unbiased shuffle
&lt;/span&gt;        &lt;span&gt;var&lt;/span&gt; array = source.ToArray();
        &lt;span&gt;var&lt;/span&gt; n = array.Length;
        &lt;span&gt;while&lt;/span&gt; (n &amp;gt; 1)
        {
            &lt;span&gt;var&lt;/span&gt; k = random.Next(n);
            n--;
            &lt;span&gt;var&lt;/span&gt; temp = array[n];
            array[n] = array[k];
            array[k] = temp;
        }

        &lt;span&gt;return&lt;/span&gt; array;
    }

    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; TakeRandom&amp;lt;T&amp;gt;(&lt;span&gt;this&lt;/span&gt; &lt;span&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; source, &lt;span&gt;int&lt;/span&gt; count)
    {
        &lt;span&gt;return&lt;/span&gt; source.Shuffle().Take(count);
    }
}&lt;/pre&gt;
&lt;p&gt;These work perfectly well, but unfortunately the &lt;em&gt;TakeRandom&lt;/em&gt; method isn&amp;#39;t as efficient as it could be because the &lt;em&gt;Shuffle&lt;/em&gt; method always shuffles the entire list even though we&amp;#39;re only interested in the first &lt;em&gt;count&lt;/em&gt; elements. This implementation of the algorithm can&amp;#39;t be stopped to retrieve the first elements as it constructs the result at the end of the array, and although we could use the existing &lt;em&gt;Reverse&lt;/em&gt; extension method to work around this it would be somewhat inefficient.&lt;/p&gt;
&lt;p&gt;Instead, we can rewrite the algorithm in reverse so that it constructs the result at the start, and then stop it after &lt;em&gt;count&lt;/em&gt; iterations to retrieve the part-shuffled sequence. To illustrate I&amp;#39;ll use the same &lt;a title="Fisher-Yates modern method walk-through at Wikipedia" href="http://en.wikipedia.org/wiki/Fisher-Yates_shuffle#Modern_method"&gt;stepwise walk-through as Wikipedia&lt;/a&gt;, but with every random &amp;#39;roll&amp;#39; swapped (1 -&amp;gt; 8, 2 -&amp;gt; 7, 3 -&amp;gt; 6, etc.) because we&amp;#39;re working from the opposite end:&lt;/p&gt;
&lt;table&gt;

&lt;tr&gt;
&lt;th&gt;Range&lt;/th&gt;
&lt;th&gt;Roll&lt;/th&gt;
&lt;th style="white-space:nowrap;"&gt;Result | Scratch&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;| 1 2 3 4 5 6 7 8&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1 - 8&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;3&lt;/strong&gt; | 2 &lt;strong&gt;1&lt;/strong&gt; 4 5 6 7 8&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2 - 8&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;3 &lt;strong&gt;7&lt;/strong&gt; | 1 4 5 6 &lt;strong&gt;2&lt;/strong&gt; 8&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3 - 8&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;3 7 &lt;strong&gt;1&lt;/strong&gt; | 4 5 6 2 8&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4 - 8&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;3 7 1 &lt;strong&gt;8&lt;/strong&gt; | 5 6 2 &lt;strong&gt;4&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5 - 8&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;3 7 1 8 &lt;strong&gt;6&lt;/strong&gt; | &lt;strong&gt;5&lt;/strong&gt; 2 4&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6 - 8&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;3 7 1 8 6 &lt;strong&gt;5&lt;/strong&gt; | 2 4&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7 - 8&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;3 7 1 8 6 5 &lt;strong&gt;4&lt;/strong&gt; | &lt;strong&gt;2&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;p&gt;Looking at the result, if we substitute each digit using the same mechanism as for the rolls (1 -&amp;gt; 8, 2 -&amp;gt; 7, etc.) it becomes 62813457 which is the reverse of the Wikipedia example&amp;#39;s result of 75431826, indicating that this approach produces equivalent, unbiased results. The reversed algorithm can be trivially implemented as a &lt;em&gt;for&lt;/em&gt; loop in a private function and then consumed by both the public &lt;em&gt;Shuffle&lt;/em&gt; and &lt;em&gt;TakeRandom&lt;/em&gt; methods.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;Enumerable
&lt;/span&gt;{
    &lt;span&gt;private&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;readonly&lt;/span&gt; &lt;span&gt;Random&lt;/span&gt; random = &lt;span&gt;new&lt;/span&gt; &lt;span&gt;Random&lt;/span&gt;();

    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Shuffle&amp;lt;T&amp;gt;(&lt;span&gt;this&lt;/span&gt; &lt;span&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; source)
    {
        &lt;span&gt;var&lt;/span&gt; array = source.ToArray();
        &lt;span&gt;return&lt;/span&gt; ShuffleInternal(array, array.Length);
    }

    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; TakeRandom&amp;lt;T&amp;gt;(&lt;span&gt;this&lt;/span&gt; &lt;span&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; source, &lt;span&gt;int&lt;/span&gt; count)
    {
        &lt;span&gt;var&lt;/span&gt; array = source.ToArray();
        &lt;span&gt;return&lt;/span&gt; ShuffleInternal(array, &lt;span&gt;Math&lt;/span&gt;.Min(count, array.Length)).Take(count);
    }

    &lt;span&gt;private&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; ShuffleInternal&amp;lt;T&amp;gt;(T[] array, &lt;span&gt;int&lt;/span&gt; count)
    {
        &lt;span&gt;// Durstenfeld implementation of the Fisher-Yates algorithm for an O(n) unbiased shuffle
&lt;/span&gt;        &lt;span&gt;// starts from the beginning rather than the end so we can just shuffle the first count
&lt;/span&gt;        &lt;span&gt;for&lt;/span&gt; (&lt;span&gt;var&lt;/span&gt; n = 0; n &amp;lt; count; n++)
        {
            &lt;span&gt;var&lt;/span&gt; k = random.Next(n, array.Length);
            &lt;span&gt;var&lt;/span&gt; temp = array[n];
            array[n] = array[k];
            array[k] = temp;
        }

        &lt;span&gt;return&lt;/span&gt; array;
    }
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;So there you have it - a pair of fast and unbiased methods to shuffle a sequence, or take a random subset of elements from it. You&amp;#39;ll notice that unlike most Linq methods they do not use deferred execution; this is because &lt;a title="Expressions involving deferred execution should be re-entrant" href="http://gregbeech.com/blogs/tech/archive/2008/02/28/expressions-involving-deferred-execution-should-be-re-entrant.aspx"&gt;methods using deferred execution should be re-entrant&lt;/a&gt; and due to their random nature these are not.&lt;/p&gt;
&lt;p&gt;Finally I should point out that if you&amp;#39;re doing anything that requires completely unpredictable shuffling (e.g. a deck of cards in a casino application) you need to replace the random number generator with a cryptographically strong one such as the .NET &lt;a title="RandomNumberGenerator at MSDN Library" href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.randomnumbergenerator.aspx"&gt;RandomNumberGenerator&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://gregbeech.com/aggbug.aspx?PostID=324" width="1" height="1"&gt;</description><category domain="http://gregbeech.com/blogs/tech/archive/tags/Performance/default.aspx">Performance</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Linq/default.aspx">Linq</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Algorithms/default.aspx">Algorithms</category></item><item><title>Optimised IP address to country code mapping</title><link>http://gregbeech.com/blogs/tech/archive/2008/09/01/optimised-ip-address-to-country-code-mapping.aspx</link><pubDate>Mon, 01 Sep 2008 19:52:48 GMT</pubDate><guid isPermaLink="false">64b9f9d3-1b55-4372-890f-9a35fe961b72:322</guid><dc:creator>Greg Beech</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://gregbeech.com/blogs/tech/rsscomments.aspx?PostID=322</wfw:commentRss><comments>http://gregbeech.com/blogs/tech/archive/2008/09/01/optimised-ip-address-to-country-code-mapping.aspx#comments</comments><description>&lt;p&gt;One of the things that web sites tend to do frequently is look up the country a user is from based on their IP address, for example to direct them to the appropriate site for their country or to restrict content that cannot be legally shown to them. There are numerous solutions to perform this mapping such as calling third-party web services or looking up the country from a database, but as it is such a commonly used function it is worthy of being optimised into an in-memory data structure.&lt;/p&gt; &lt;p&gt;This is potentially tricky to build as IP addresses are organised in ranges with allocated country codes (about 92000 at the current time) but there are approximately two billion possible values for the IP address itself, so to handle the mapping with reasonable memory usage we need to be able to efficiently map an individual IP address to a range without storing all of the IP addresses in the data structure.&lt;/p&gt; &lt;p&gt;Initial research on the net shows that a commonly used structure for IP address routing is the &lt;a title="Radix trie [aka Patricia trie] at Wikipedia" href="http://en.wikipedia.org/wiki/Radix_tree"&gt;Patricia trie&lt;/a&gt;, and I also found a number of articles about it being used for &lt;a title="Optimized IP to ISO3166 Country Code Mapping in C# at CodeProject" href="http://www.codeproject.com/KB/cs/iptocountry.aspx"&gt;IP address to&lt;/a&gt; &lt;a title="Extreme Optimization #1.1: Mapping IP addresses to country codes" href="http://www.codeproject.com/KB/cs/iplookupoptimise.aspx"&gt;country code mapping&lt;/a&gt;. Unfortunately the code for the all the articles I found were either released under, or appeared to be derived from code which is released under, the dreaded GPL license which means that it&amp;#39;s too risky to even download it to take a look. The only other implementations I could find were from academia (read: incomprehensible ANSI C) which meant I&amp;#39;d be writing the trie myself from scratch.&lt;/p&gt; &lt;p&gt;Fast-forward a day and a half, and I&amp;#39;ve got a working IP address lookup table based on a binary Patricia trie, which supports the basic functions of adding IP address ranges and finding the range that an IP address is associated with. An optimisation I added was that instead of using one trie to hold all the nodes, I used a sparse array of 256 lazily created tries with each one corresponding to the first byte of the IPv4 address; as most of the first byte values are distinct on each bit this allows a single byte comparison rather than walking eight nodes.&lt;/p&gt; &lt;p&gt;The performance is pretty good. On my 2.1MHz Intel Core2 Duo machine, the trie takes around 0.8 seconds to build and can perform ten million lookups in 8.2 seconds (or around 1.2 million lookups per second).&lt;/p&gt; &lt;p&gt;However, there is a price to pay for this. The code is complex. It&amp;#39;s difficult to ascertain that the code is bug-free without doing an exhaustive search of all IP addresses, and it would be difficult to come back to the code and perform any maintenance work even with the large amount of commenting. Although it was fun to investigate the structure, I&amp;#39;d prefer to have something simpler in our production codebase even if it does sacrifice a bit of efficiency.&lt;/p&gt; &lt;p&gt;Enter &lt;a title="Binary search at Wikipedia" href="http://en.wikipedia.org/wiki/Binary_search"&gt;binary search&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;For any sorted list of values a binary search executes in &lt;em&gt;O(lg N)&lt;/em&gt; time, and if it doesn&amp;#39;t find an exact match the .NET implementation returns the bitwise complement of the next largest result. This means that if the values being searched are the start IP addresses in the range, that the index of the appropriate range can be returned by taking the value (if found) or the bitwise complement minus one (if not found) of the result for the candidate IP address. If we used a single list then we&amp;#39;d expect around &lt;em&gt;lg 92000&lt;/em&gt;&amp;nbsp; = 17 comparisons per search, however if we keep the idea of using 256 lists we can bring this down to around &lt;em&gt;lg (92000/256) &lt;/em&gt;= 9 comparisons plus one to find the correct root.&lt;/p&gt; &lt;p&gt;This is a much simpler data structure to build, and within about half an hour I had a working version for comparison of performance. The results were as follows, loading 92000 entries from a flat file and then performing ten million lookups of various addresses:&lt;/p&gt; &lt;p align="center"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="252" alt="IP Address Lookup Performance" src="http://gregbeech.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/tech/image_5F00_8.png" width="435" border="0" /&gt; &lt;/p&gt; &lt;p&gt;Yes, you are reading that right. The simple multiple sorted list structure using binary search turned out to be nearly twice as fast for lookups as the complex multiple Patricia trie one, managing around 2.1 million lookups per second. It was also faster to load, although this is slightly unfair as the data was already sorted when loaded so this result would be different if it was being loaded from unsorted data as that would affect the lists but not the tries.&lt;/p&gt; &lt;p&gt;Thinking about it, it isn&amp;#39;t hard to see why this result was achieved. The nodes in the Patricia trie are necessarily implemented as classes so they can have references to each other which means that traversing the tree involves following object references around the heap, and both the building of the tree and the lookups require bit-shifting and masking operations as well as integer comparisons. By contrast the entries in the sorted lists can be structures meaning that each list is using contiguous memory, and although more comparisons are required they are simple integer ones without any bit-shifting and as such only take a single clock cycle to execute.&lt;/p&gt; &lt;p&gt;In terms of memory usage the binary search solution will also be better due to its entries being structures and thus having no object header, and not needing pointers to their left and right children.&lt;/p&gt; &lt;p&gt;Unfortunately I can&amp;#39;t release the code for the lookup table as I wrote it during work time, but to write your own lookup table using sorted lists and binary search only takes about fifty lines and half an hour, and it will comprehensively outperform an implementation using Patricia tries which takes hundreds of lines and many hours to implement .&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://gregbeech.com/aggbug.aspx?PostID=322" width="1" height="1"&gt;</description><category domain="http://gregbeech.com/blogs/tech/archive/tags/Performance/default.aspx">Performance</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Algorithms/default.aspx">Algorithms</category></item><item><title>Disposing and finalizing partially constructed objects</title><link>http://gregbeech.com/blogs/tech/archive/2008/08/27/disposing-and-finalizing-partially-constructed-objects.aspx</link><pubDate>Wed, 27 Aug 2008 22:44:43 GMT</pubDate><guid isPermaLink="false">64b9f9d3-1b55-4372-890f-9a35fe961b72:320</guid><dc:creator>Greg Beech</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://gregbeech.com/blogs/tech/rsscomments.aspx?PostID=320</wfw:commentRss><comments>http://gregbeech.com/blogs/tech/archive/2008/08/27/disposing-and-finalizing-partially-constructed-objects.aspx#comments</comments><description>&lt;p&gt;On &lt;a title="MSDN Forums" href="http://forums.msdn.microsoft.com"&gt;MSDN forums&lt;/a&gt; there was a question about &lt;a title="Small or big C# contructor [sic] at MSDN Forums" href="http://forums.msdn.microsoft.com/en-US/csharpgeneral/thread/e39ed467-bca7-4432-8266-1f095c220c2c"&gt;whether class constructors should be permitted to do a lot of work or not&lt;/a&gt;:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;I have a dilemma and a dispute with some colleagues: should the constructor of a C# class do a lot or the minimum and latter in a method call put a lot of logic?&lt;/p&gt; &lt;p&gt;I am coming from a C++ background and there it is better to do just simple data initialization in the constructor, throwing an exception from a constructor is bad since the object is not fully constructed yet.&lt;/p&gt; &lt;p&gt;Does the same argument work in C#?&lt;/p&gt; &lt;p&gt;Should I do the minimum work in the constructor?&lt;/p&gt; &lt;p&gt;My colleagues&amp;#39; position is that having a minimal constructor will leave the state of the object as not fully defined and then changed by a subsequent call to another method.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;The two main factors to whether a constructor should do a significant amount of work are whether it should leave the class in a state ready to be used for real work if possible, and whether it matters if the constructor throws an exception and leaves the instance in a partially constructed state. I think we can all agree that it is much more intuitive to construct an object and have it ready for use than to construct it and have to call some sort of &lt;em&gt;Initialize&lt;/em&gt; method afterwards, so lets assume that the real issue is with resource clean-up of partially constructed objects.&lt;/p&gt; &lt;p&gt;Rather than offer opinion here, I wrote a simple program to demonstrate disposal and finalization of a partially constructed object.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;class&lt;/span&gt; &lt;span&gt;Program
&lt;/span&gt;{
    &lt;span&gt;static&lt;/span&gt; &lt;span&gt;unsafe&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; Main(&lt;span&gt;string&lt;/span&gt;[] args)
    {
        &lt;span&gt;Foo&lt;/span&gt; foo = &lt;span&gt;null&lt;/span&gt;;
        &lt;span&gt;try
&lt;/span&gt;        {
            foo = &lt;span&gt;new&lt;/span&gt; &lt;span&gt;Foo&lt;/span&gt;();
        }
        &lt;span&gt;catch&lt;/span&gt; (&lt;span&gt;Exception&lt;/span&gt; ex)
        {
            &lt;span&gt;Console&lt;/span&gt;.WriteLine(&lt;span&gt;&amp;quot;{0} occurred constructing Foo&amp;quot;&lt;/span&gt;, ex.GetType());
        } 
        
        &lt;span&gt;try
&lt;/span&gt;        {
            foo.Dispose();
        }
        &lt;span&gt;catch&lt;/span&gt; (&lt;span&gt;Exception&lt;/span&gt; ex)
        {
            &lt;span&gt;Console&lt;/span&gt;.WriteLine(&lt;span&gt;&amp;quot;{0} occurred disposing Foo&amp;quot;&lt;/span&gt;, ex.GetType());
        } 

        &lt;span&gt;GC&lt;/span&gt;.Collect();
        &lt;span&gt;GC&lt;/span&gt;.WaitForPendingFinalizers();
    }
}

&lt;span&gt;class&lt;/span&gt; &lt;span&gt;Foo&lt;/span&gt; : &lt;span&gt;IDisposable
&lt;/span&gt;{
    &lt;span&gt;public&lt;/span&gt; Foo()
    {
        &lt;span&gt;throw&lt;/span&gt; &lt;span&gt;new&lt;/span&gt; &lt;span&gt;Exception&lt;/span&gt;();
    }

    ~Foo()
    {
        &lt;span&gt;Console&lt;/span&gt;.WriteLine(&lt;span&gt;&amp;quot;Foo finalizer called&amp;quot;&lt;/span&gt;);
    }

    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; Dispose()
    {
        &lt;span&gt;Console&lt;/span&gt;.WriteLine(&lt;span&gt;&amp;quot;Foo dispose called&amp;quot;&lt;/span&gt;);
    }
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The output from this program is:&lt;/p&gt;&lt;pre class="code"&gt;System.Exception occurred constructing Foo
System.NullReferenceException occurred disposing Foo
Foo finalizer called&lt;/pre&gt;
&lt;p&gt;From this we can ascertain that if the object is not constructed correctly then the reference to the object will not be assigned, which means that no methods can be called on it, so the Dispose method cannot be used to deterministically clean up managed resources. The implication here is that if the constructor creates expensive managed resources which need to be cleaned up at the earliest opportunity then it should do so in an exception handler within the constructor as it will not get another chance.&lt;/p&gt;
&lt;p&gt;We can also ascertain that the finalizer will be called on the object even if the constructor throws an exception. This is possible because, unlike C++, &lt;a title="A Fundamental Difference in Class Behavior between the Native and Managed Object Model at Stan Lippman&amp;#39;s Blog" href="http://blogs.msdn.com/slippman/archive/2004/01/28/63917.aspx"&gt;CLR objects do not start as their base type and then morph into derived types as they are constructed but start out as the type they will end up as&lt;/a&gt; so the garbage collector knows what type the object is and where the finalizer is in the method table. As such, the presence of a finalizer does not affect whether you should throw exceptions from a constructor because as long as you write the finalizer code defensively to only clean up unmanaged resources that have been allocated (which you should be doing anyway) then it will work fine whether the object was fully constructed or not.&lt;/p&gt;
&lt;p&gt;And, of course, there is the final scenario in which the constructor neither allocates expensive managed resources, nor any unmanaged resources, so it doesn&amp;#39;t matter whether you throw an exception because the partially constructed instance will just be cleaned up by the garbage collector.&lt;/p&gt;
&lt;p&gt;In summary, do as much work as needed to get the object in a usable state in the constructor, but clean up any expensive managed resources you allocate in an exception handler if you can&amp;#39;t complete it successfully.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://gregbeech.com/aggbug.aspx?PostID=320" width="1" height="1"&gt;</description><category domain="http://gregbeech.com/blogs/tech/archive/tags/.NET/default.aspx">.NET</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Design+Guidelines/default.aspx">Design Guidelines</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Exceptions/default.aspx">Exceptions</category></item><item><title>Computers don't care whether your resources are directly addressable</title><link>http://gregbeech.com/blogs/tech/archive/2008/08/24/computers-don-t-care-whether-your-resources-are-directly-addressable.aspx</link><pubDate>Sun, 24 Aug 2008 16:16:58 GMT</pubDate><guid isPermaLink="false">64b9f9d3-1b55-4372-890f-9a35fe961b72:317</guid><dc:creator>Greg Beech</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://gregbeech.com/blogs/tech/rsscomments.aspx?PostID=317</wfw:commentRss><comments>http://gregbeech.com/blogs/tech/archive/2008/08/24/computers-don-t-care-whether-your-resources-are-directly-addressable.aspx#comments</comments><description>&lt;p&gt;The article &lt;a title="Roots of the REST/SOAP Debate by Paul Prescod" href="http://www.prescod.net/rest/rest_vs_soap_overview/#section_4.1"&gt;Roots of the REST/SOAP Debate&lt;/a&gt; (along with many others like it) claims that SOAP web services have a limitation in that resources they work with are not directly addressable via a URI, but that you have to connect to the SOAP endpoint and then call a method on it. The analogy used is as follows:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Let me offer an analogy. Suppose you were living temporarily in a hotel. The hotel might not have direct dial connections from the outside. In order to call a room you have to contact the operator first (this is like contacting the &amp;quot;SOAP endpoint&amp;quot;) and then ask them to connect you to your room. &lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;This is simply incorrect. Saying that you need to contact the operator first and then connect them to the room implies that to make a call to a SOAP service you have to first connect to the service endpoint itself, and then send a message to it which performs an operation. When you&amp;#39;re writing code that uses web service proxies then it may appear this way because you first create the proxy and then call a method on it, but this doesn&amp;#39;t reflect what actually happens on the wire. &lt;/p&gt; &lt;p&gt;When you create the proxy nothing at all is sent on the wire, it simply constructs a local object that knows where to send requests to (you do not need an instance of a proxy as it could also be done with static methods where you pass the service URI in each time; the proxy instance is typically used for convenience if you&amp;#39;re making multiple method calls). When you call the method on the proxy, then a single message is sent to the service URI which interprets it using the designated method, and sends a response. From an HTTP perspective, this is &lt;em&gt;exactly&lt;/em&gt; the same mode of operation as REST-style services.&lt;/p&gt; &lt;p&gt;The analogy continues:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;Now imagine that there is an outside service that you would like to buy. It is a once-a-day automated wake-up call and horoscope service. You try to sign up for the service but when you are asked to enter the phone number to call back you realize that there is no single number you can provide. The service must contact the operator first and then the operator must patch them through to you. Obviously the computer on the other end is not going to be smart enough to know to go through the operator and the operator will not know to patch the call through to your room.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;So lets say that the person in the analogy does provide them with a direct phone number, and the service is ready to call them back with a horoscope. Assuming both parties speak English then the service might say &lt;em&gt;&amp;quot;Hello, your horoscope is...&amp;quot;&lt;/em&gt; or it might say &lt;em&gt;&amp;quot;Good morning, today you will be...&amp;quot;&lt;/em&gt; and the person would understand either because we intelligently interpret the response. However computers do not do this because they are not sentient; they need a well defined response representation and will not understand the horoscope unless they already know what it is. The article does allude to this fact later...&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;If two HTTP services can agree on an information representation (for instance RSS (the Rich Site Summary) or SAML (Security Assertions Markup Language) then those two services can communicate about these information resources merely by passing URIs to resources.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;...but it forgets to include the point that if two HTTP services have agreed on the information representation of the response, then they could also easily agree on the representation of the request. This means that as long as the service knows &lt;em&gt;how&lt;/em&gt; to call the phone number you pass then it doesn&amp;#39;t matter whether it needs a payload, which means it doesn&amp;#39;t matter whether it is a directly-addressable REST-style URI or a SOAP web service URI. The problem is not the protocol itself; the problem is a lack of universal data type definitions.&lt;/p&gt; &lt;p&gt;I know what you&amp;#39;re thinking at this point - that it&amp;#39;s simpler just to use a REST-style URI than a SOAP web service for the callback as you don&amp;#39;t need a payload. And you&amp;#39;re right. But lets explore the last quote a little further and talk about retrieving RSS feeds. If I told you that the RSS feed for this blog was at &lt;a title="http://gregbeech.com/blogs/tech/rss.aspx" href="http://gregbeech.com/blogs/tech/rss.aspx"&gt;http://gregbeech.com/blogs/tech/rss.aspx&lt;/a&gt; then you&amp;#39;d find it easy to get the latest posts, but if I told you that this URI is capable of restricting posts to date ranges then what URI would you use to only retrieve posts in the first half of 2007? And would you extend the URI in the same way on an RSS feed that wasn&amp;#39;t generated by Community Server?&lt;/p&gt; &lt;p&gt;Exactly.&lt;/p&gt; &lt;p&gt;As soon as you want to do anything other than call a static URI on a REST-style service, you need to know how to construct and extend the URI to return the right data. And if it&amp;#39;s a complex service you may also need to pass an XML payload containing the data (this is particularly common for &lt;em&gt;PUT&lt;/em&gt; commands). Just because a service uses REST-style URIs does not mean that it can be easily used by any other service, irrespective of whether it understands the response format.&lt;/p&gt; &lt;p&gt;So if REST-style services are no different in their on-wire interaction to SOAP services, and are no more interoperable than SOAP services, where does the argument come from that they are preferable because of their directly-addressable URIs? Well, people care what the URIs are so they can type them and see where they link to before they click them, and they don&amp;#39;t want to have to create a payload because it&amp;#39;s inconvenient. A service client, however, does not care what the actual URIs are as long as it has been told how to build them (which means it does not care whether resource is directly-addressed using a REST-style URI or whether it is indirectly addressed using a SOAP service) and similarly has no issue with creating a payload as it has no concept of convenience.&lt;/p&gt; &lt;p&gt;All of which means that any preference for REST-style services over SOAP services on the basis of them being directly addressable or &amp;quot;more like the web&amp;quot; is based on a flawed anthropomorphism of a service client. So lets drop this pointless facet of the SOAP vs REST argument once and for all, OK?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://gregbeech.com/aggbug.aspx?PostID=317" width="1" height="1"&gt;</description><category domain="http://gregbeech.com/blogs/tech/archive/tags/Rants/default.aspx">Rants</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/WCF/default.aspx">WCF</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Web+Services/default.aspx">Web Services</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/REST/default.aspx">REST</category></item><item><title>Emitting primitive type conversions with Reflection.Emit</title><link>http://gregbeech.com/blogs/tech/archive/2008/08/20/emitting-primitive-type-conversions-with-reflection-emit.aspx</link><pubDate>Wed, 20 Aug 2008 21:34:39 GMT</pubDate><guid isPermaLink="false">64b9f9d3-1b55-4372-890f-9a35fe961b72:314</guid><dc:creator>Greg Beech</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://gregbeech.com/blogs/tech/rsscomments.aspx?PostID=314</wfw:commentRss><comments>http://gregbeech.com/blogs/tech/archive/2008/08/20/emitting-primitive-type-conversions-with-reflection-emit.aspx#comments</comments><description>&lt;p&gt;Conversion between primitive types is trivial in high-level languages like C# as you can use the cast operator, optionally surrounded with &lt;em&gt;checked&lt;/em&gt; or &lt;em&gt;unchecked&lt;/em&gt; to force the integer overflow semantics. At CIL level the conversion is less trivial, involving the choice of no fewer than 27 different op-codes, which are required to be used in combination with each other under some circumstances.&lt;/p&gt; &lt;p&gt;This is yet another place where extension methods are incredibly useful, because rather than having to remember all this it is possible to capture the information about how to convert between two types in a method and expose it alongside the low-level ones on the &lt;em&gt;ILGenerator&lt;/em&gt; class. The extension method to do this is below (note that &lt;em&gt;IsUnsignedInteger&lt;/em&gt; is an extension method for the &lt;em&gt;Type&lt;/em&gt; class which returns whether the type is one of &lt;em&gt;byte&lt;/em&gt;, &lt;em&gt;ushort&lt;/em&gt;, &lt;em&gt;uint&lt;/em&gt; or &lt;em&gt;ulong&lt;/em&gt;).&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; EmitConv(&lt;span&gt;this&lt;/span&gt; &lt;span&gt;ILGenerator&lt;/span&gt; il, &lt;span&gt;Type&lt;/span&gt; fromType, &lt;span&gt;Type&lt;/span&gt; toType, &lt;span&gt;bool&lt;/span&gt; ovf)
{
    &lt;span&gt;if&lt;/span&gt; (fromType == toType)
    {
        &lt;span&gt;// no conversion required
&lt;/span&gt;    }
    &lt;span&gt;else&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; (toType == &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;float&lt;/span&gt;) || toType == &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;double&lt;/span&gt;))
    {
        &lt;span&gt;// if converting to a floating point number then we must use a conv.* instruction as there are no ovf
&lt;/span&gt;        &lt;span&gt;// checked operations that work with them. if the starting point is an unsigned integer then we must
&lt;/span&gt;        &lt;span&gt;// convert it to a real first as conv.r4 and conv.r8 don&amp;#39;t work with unsigned integers (at IL level
&lt;/span&gt;        &lt;span&gt;// the char data type is considered to be a 2-byte unsigned integer)
&lt;/span&gt;        &lt;span&gt;if&lt;/span&gt; (fromType.IsUnsignedInteger() || fromType == &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;char&lt;/span&gt;))
        {
            il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_R_Un);
        }

        EmitConv(il, toType);
    }
    &lt;span&gt;else&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; (ovf)
    {
        &lt;span&gt;// overflow checking required so use conv.ovf.* or conv.ovf.*.un depending on the source type
&lt;/span&gt;        &lt;span&gt;if&lt;/span&gt; (fromType.IsUnsignedInteger() || fromType == &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;char&lt;/span&gt;))
        {
            EmitConv_Ovf_Un(il, toType);
        }
        &lt;span&gt;else
&lt;/span&gt;        {
            EmitConv_Ovf(il, toType);
        }
    }
    &lt;span&gt;else
&lt;/span&gt;    {
        &lt;span&gt;// no overflow checking so use the conv.* instruction
&lt;/span&gt;        EmitConv(il, toType);
    }
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Not many of the 27 instructions present there; just one to handle the special case of converting unsigned integers to reals. The real heavy lifting is done by the three other &lt;em&gt;Emit*&lt;/em&gt; methods, which all use dictionary lookups in the same way as &lt;a title="Emitting arbitrary constants with Reflection.Emit" href="http://gregbeech.com/blogs/tech/archive/2008/07/20/emitting-arbitrary-constants-with-reflection-emit.aspx"&gt;my post about emitting arbitrary constants&lt;/a&gt;. I doubt there&amp;#39;s any point in putting more text at the bottom of the post, so I&amp;#39;ll say now that this code has been tested by over 350 unit tests which cover every possible type conversion, using both in-range and out-of-range values for both checked and unchecked variants.&lt;/p&gt;
&lt;p&gt;Here are the lookup dictionaries:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;private&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;readonly&lt;/span&gt; &lt;span&gt;IDictionary&lt;/span&gt;&amp;lt;&lt;span&gt;Type&lt;/span&gt;, &lt;span&gt;Action&lt;/span&gt;&amp;lt;&lt;span&gt;ILGenerator&lt;/span&gt;&amp;gt;&amp;gt; emitConvMethods = 
    &lt;span&gt;new&lt;/span&gt; &lt;span&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span&gt;Type&lt;/span&gt;, &lt;span&gt;Action&lt;/span&gt;&amp;lt;&lt;span&gt;ILGenerator&lt;/span&gt;&amp;gt;&amp;gt;
        {
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;sbyte&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_I1) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;byte&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_U1) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;short&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_I2) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;ushort&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_U2) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;int&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_I4) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;uint&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_U4) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;long&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_I8) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;ulong&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_U8) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;float&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_R4) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;double&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_R8) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;char&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_U2) },
        };

&lt;span&gt;private&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;readonly&lt;/span&gt; &lt;span&gt;IDictionary&lt;/span&gt;&amp;lt;&lt;span&gt;Type&lt;/span&gt;, &lt;span&gt;Action&lt;/span&gt;&amp;lt;&lt;span&gt;ILGenerator&lt;/span&gt;&amp;gt;&amp;gt; emitConvOvfMethods = 
    &lt;span&gt;new&lt;/span&gt; &lt;span&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span&gt;Type&lt;/span&gt;, &lt;span&gt;Action&lt;/span&gt;&amp;lt;&lt;span&gt;ILGenerator&lt;/span&gt;&amp;gt;&amp;gt;
        {
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;sbyte&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_I1) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;byte&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_U1) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;short&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_I2) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;ushort&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_U2) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;int&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_I4) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;uint&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_U4) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;long&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_I8) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;ulong&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_U8) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;char&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_U2) },
        };

&lt;span&gt;private&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;readonly&lt;/span&gt; &lt;span&gt;IDictionary&lt;/span&gt;&amp;lt;&lt;span&gt;Type&lt;/span&gt;, &lt;span&gt;Action&lt;/span&gt;&amp;lt;&lt;span&gt;ILGenerator&lt;/span&gt;&amp;gt;&amp;gt; emitConvOvfUnMethods = 
    &lt;span&gt;new&lt;/span&gt; &lt;span&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span&gt;Type&lt;/span&gt;, &lt;span&gt;Action&lt;/span&gt;&amp;lt;&lt;span&gt;ILGenerator&lt;/span&gt;&amp;gt;&amp;gt;
        {
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;sbyte&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_I1_Un) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;byte&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_U1_Un) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;short&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_I2_Un) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;ushort&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_U2_Un) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;int&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_I4_Un) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;uint&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_U4_Un) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;long&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_I8_Un) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;ulong&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_U8_Un) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;char&lt;/span&gt;), il =&amp;gt; il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Conv_Ovf_U2_Un) },
        };&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;And here are the methods that make use of them:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;private&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; EmitConv(&lt;span&gt;ILGenerator&lt;/span&gt; il, &lt;span&gt;Type&lt;/span&gt; toType)
{
    &lt;span&gt;if&lt;/span&gt; (emitConvMethods.ContainsKey(toType))
    {
        &lt;span&gt;var&lt;/span&gt; emitConv = emitConvMethods[toType];
        emitConv(il);
    }
    &lt;span&gt;else
&lt;/span&gt;    {
        &lt;span&gt;throw&lt;/span&gt; &lt;span&gt;new&lt;/span&gt; &lt;span&gt;NotSupportedException&lt;/span&gt;(&lt;span&gt;&amp;quot;A conv.* op-code cannot be emitted for &amp;quot;&lt;/span&gt; + toType + &lt;span&gt;&amp;quot;.&amp;quot;&lt;/span&gt;);
    }
}

&lt;span&gt;private&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; EmitConv_Ovf(&lt;span&gt;ILGenerator&lt;/span&gt; il, &lt;span&gt;Type&lt;/span&gt; toType)
{
    &lt;span&gt;if&lt;/span&gt; (emitConvOvfMethods.ContainsKey(toType))
    {
        &lt;span&gt;var&lt;/span&gt; emitConvOvf = emitConvOvfMethods[toType];
        emitConvOvf(il);
    }
    &lt;span&gt;else
&lt;/span&gt;    {
        &lt;span&gt;throw&lt;/span&gt; &lt;span&gt;new&lt;/span&gt; &lt;span&gt;NotSupportedException&lt;/span&gt;(&lt;span&gt;&amp;quot;A conv.ovf.* op-code cannot be emitted for &amp;quot;&lt;/span&gt; + toType + &lt;span&gt;&amp;quot;.&amp;quot;&lt;/span&gt;);
    }
}

&lt;span&gt;private&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; EmitConv_Ovf_Un(&lt;span&gt;ILGenerator&lt;/span&gt; il, &lt;span&gt;Type&lt;/span&gt; toType)
{
    &lt;span&gt;if&lt;/span&gt; (emitConvOvfUnMethods.ContainsKey(toType))
    {
        &lt;span&gt;var&lt;/span&gt; emitConvOvfUn = emitConvOvfUnMethods[toType];
        emitConvOvfUn(il);
    }
    &lt;span&gt;else
&lt;/span&gt;    {
        &lt;span&gt;throw&lt;/span&gt; &lt;span&gt;new&lt;/span&gt; &lt;span&gt;NotSupportedException&lt;/span&gt;(&lt;span&gt;&amp;quot;A conv.ovf.*.un op-code cannot be emitted for &amp;quot;&lt;/span&gt; + toType + &lt;span&gt;&amp;quot;.&amp;quot;&lt;/span&gt;);
    }
}&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://gregbeech.com/aggbug.aspx?PostID=314" width="1" height="1"&gt;</description><category domain="http://gregbeech.com/blogs/tech/archive/tags/.NET/default.aspx">.NET</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Reflection.Emit/default.aspx">Reflection.Emit</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/CIL/default.aspx">CIL</category></item><item><title>Code without comments is code that doesn't work</title><link>http://gregbeech.com/blogs/tech/archive/2008/08/04/code-without-comments-is-code-that-doesn-t-work.aspx</link><pubDate>Mon, 04 Aug 2008 19:58:52 GMT</pubDate><guid isPermaLink="false">64b9f9d3-1b55-4372-890f-9a35fe961b72:296</guid><dc:creator>Greg Beech</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://gregbeech.com/blogs/tech/rsscomments.aspx?PostID=296</wfw:commentRss><comments>http://gregbeech.com/blogs/tech/archive/2008/08/04/code-without-comments-is-code-that-doesn-t-work.aspx#comments</comments><description>&lt;p&gt;The other week Jeff Atwood posted a blog entry named &lt;a title="Coding Without Comments at CodingHorror.com" href="http://www.codinghorror.com/blog/archives/001150.html"&gt;Coding Without Comments&lt;/a&gt; that stated... well I&amp;#39;m actually not sure. It started by making the valid point that comments should indicate &lt;em&gt;why&lt;/em&gt; your code works the way it does and shouldn&amp;#39;t be needed to explain &lt;em&gt;what&lt;/em&gt; it does, but then went entirely off track and ended up pretty much stating that all comments are detrimental to code quality (emphasis Jeff&amp;#39;s):&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;While comments are neither inherently good or bad, they are frequently used as a crutch. &lt;b&gt;You should always write your code as if comments didn&amp;#39;t exist.&lt;/b&gt; This &lt;i&gt;forces&lt;/i&gt; you to write your code in the simplest, plainest, most self-documenting way you can humanly come up with.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;The example used in the blog post was refactoring some code that calculated a square root using the Newton-Raphson approximation to make it more clear how it worked. Which is all very useful. But the problem is that there are &lt;a title="Methods of computing square roots at Wikipedia" href="http://en.wikipedia.org/wiki/Methods_of_computing_square_roots"&gt;a lot of different ways to calculate a square root&lt;/a&gt; which make trade-offs in terms of speed, accuracy, memory usage and simplicity, and by the end of the post we still didn&amp;#39;t know &lt;em&gt;why the Newton-Raphson method was chosen in the first place&lt;/em&gt; because there were no comments with that information. And I really doubt this is something you want to encode in the method name:&lt;/p&gt;&lt;pre class="code"&gt;var sqrt = NewtonRaphsonApproximationBecauseItConvergesQuicklyWithAGoodInitialGuess(n);&lt;/pre&gt;
&lt;p&gt;Clearly the claim that you should write code as if comments didn&amp;#39;t exist is a fallacy because they are necessary to explain why you wrote it that way (or, indeed, why you didn&amp;#39;t write it another way). But what about my claim that code without comments doesn&amp;#39;t even work? This is down to the probability that code without any comments hasn&amp;#39;t been used in the real world.&lt;/p&gt;
&lt;p&gt;When you start writing code, you write it to meet a specification, using the available documentation for any APIs you&amp;#39;re calling, and it all looks right. Everything has been code reviewed by at least one other person, you&amp;#39;ve added argument checking and assertions about your current state to every method, and run plenty of unit and system tests against it, so you know that when it goes into production everything will work perfectly. &lt;/p&gt;
&lt;p&gt;Except it doesn&amp;#39;t.&lt;/p&gt;
&lt;p&gt;One example at &lt;a title="blinkBox" href="http://www.blinkbox.com"&gt;blinkBox&lt;/a&gt; is the frame-grabbing code which uses DirectShow to grab a frame and then loads the resultant byte array into a &lt;em&gt;Bitmap&lt;/em&gt; class. This worked perfectly during testing, and then suddenly in the live environment we kept getting an error with one particular file where the &lt;em&gt;Bitmap&lt;/em&gt; constructor claimed the bytes were invalid. Just to see what the bits looked like if we &lt;em&gt;could&lt;/em&gt; load them into the image I tried using &lt;em&gt;LockBits&lt;/em&gt; and an unsafe memory copy into the bitmap&amp;#39;s memory area; the result was a skewed black-and-white image. Noticing that the stride of the image was not native-pointer aligned and many APIs prefer aligned data, my colleague Jon and I set about padding the data to an aligned stride and then trying to use that as the image data. The constructor still wouldn&amp;#39;t accept it, but using the unsafe memory copy and then trimming the width to the original size produced a perfect image. I still don&amp;#39;t know why the data is invalid, or why this hack works, but we&amp;#39;ve found that for about 1 in 500 source files we need it.&lt;/p&gt;
&lt;p&gt;There&amp;#39;s simply no way that you could predict this, or even find it without huge quantities of test data, and putting that sort of hack in the exception handling block without a comment explaining the empirical reasoning behind it and the fact that it may not be perfect as we don&amp;#39;t even know why it really works would be reproachable. I think the comment came out at around 20 lines, including ASCII art.&lt;/p&gt;
&lt;p&gt;As our codebase matures, there are more and more parts of it that have long comments explaining why things are implemented the way they are, what alternatives were considered and discounted, what circumstances were observed in the live environment that cause this edge case or that edge case or the other edge case, and why the code looks wrong but needs to be that way. Some particularly tortuous parts have significantly more comments than code itself.&lt;/p&gt;
&lt;p&gt;Code without this type of comment is code that has never been hardened by real-world use; code that has never hit the 0.1% edge cases; code that simply doesn&amp;#39;t work properly. I&amp;#39;m sure Jeff will find this out once he&amp;#39;s had &lt;a title="StackOverflow.com" href="http://stackoverflow.com"&gt;StackOverflow&lt;/a&gt; up and running for a few months.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://gregbeech.com/aggbug.aspx?PostID=296" width="1" height="1"&gt;</description><category domain="http://gregbeech.com/blogs/tech/archive/tags/Coding/default.aspx">Coding</category></item><item><title>Tips &amp; Tricks: Use DebuggerDisplayAttribute for easier debugging</title><link>http://gregbeech.com/blogs/tech/archive/2008/07/28/tips-amp-tricks-use-debuggerdisplayattribute-for-easier-debugging.aspx</link><pubDate>Mon, 28 Jul 2008 20:44:29 GMT</pubDate><guid isPermaLink="false">64b9f9d3-1b55-4372-890f-9a35fe961b72:288</guid><dc:creator>Greg Beech</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://gregbeech.com/blogs/tech/rsscomments.aspx?PostID=288</wfw:commentRss><comments>http://gregbeech.com/blogs/tech/archive/2008/07/28/tips-amp-tricks-use-debuggerdisplayattribute-for-easier-debugging.aspx#comments</comments><description>&lt;p&gt;When debugging, it&amp;#39;s common to hover over an object to get a quick view of what its properties are. With a single object this is fine because you can easily expand it to view its contents, but when you have a collection of objects it&amp;#39;s painful to see the contents as you have to expand every element. The typical view in the debugger of a collection of simple movie classes is like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;Movie
&lt;/span&gt;{
    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;int&lt;/span&gt; Id { &lt;span&gt;get&lt;/span&gt;; &lt;span&gt;set&lt;/span&gt;; }
    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;string&lt;/span&gt; Title { &lt;span&gt;get&lt;/span&gt;; &lt;span&gt;set&lt;/span&gt;; }
    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;float&lt;/span&gt; Rating { &lt;span&gt;get&lt;/span&gt;; &lt;span&gt;set&lt;/span&gt;; }
}&lt;/pre&gt;
&lt;p align="center"&gt;&amp;nbsp;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" border="0" alt="Debugger default view" src="http://gregbeech.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/tech/debugger_2D00_display_2D00_default_2D00_view_5F00_3.png" width="336" height="135" /&gt; &lt;/p&gt;
&lt;p&gt;This is where &lt;em&gt;DebuggerDisplayAttribute&lt;/em&gt; comes in; by applying it to our &lt;em&gt;Movie&lt;/em&gt; class the collection hover can be made much more informative. The format string looks similar to normal .NET format strings in that the values to replace are between curly braces, but instead of being told what to substitute the debugger treats these values as expressions and attempts to evaluate them using the current context. Here we simply list the properties of the object:&lt;/p&gt;&lt;pre class="code"&gt;[&lt;span&gt;DebuggerDisplay&lt;/span&gt;(&lt;span&gt;&amp;quot;Id = {Id}, Title = {Title}, Rating = {Rating}&amp;quot;&lt;/span&gt;)]
&lt;span&gt;public&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;Movie
&lt;/span&gt;{
    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;int&lt;/span&gt; Id { &lt;span&gt;get&lt;/span&gt;; &lt;span&gt;set&lt;/span&gt;; }
    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;string&lt;/span&gt; Title { &lt;span&gt;get&lt;/span&gt;; &lt;span&gt;set&lt;/span&gt;; }
    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;float&lt;/span&gt; Rating { &lt;span&gt;get&lt;/span&gt;; &lt;span&gt;set&lt;/span&gt;; }
}&lt;/pre&gt;
&lt;p align="center"&gt;&lt;img border="0" alt="Debugger display with attribute expressions" src="http://gregbeech.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/tech/debugger_2D00_display_2D00_attribute_2D00_view_5F00_3c501f7c_2D00_ff44_2D00_4e0e_2D00_bc5a_2D00_9e549994d6a0.png" width="510" height="136" /&gt;&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Where this gets really interesting, however, is that with the C# debugger you can enter much more powerful expressions to evaluate than just the property names. If we didn&amp;#39;t want to have really long titles listed in full (because &lt;em&gt;&amp;quot;The assassination of Jesse James by the coward Robert Ford&amp;quot;&lt;/em&gt; might scroll off all but the widest of monitors) we could truncate them to 20 characters and show an ellipsis to indicate the truncation:&lt;/p&gt;&lt;pre class="code"&gt;[&lt;span&gt;DebuggerDisplay&lt;/span&gt;(
    &lt;span&gt;&amp;quot;Id = {Id}, &amp;quot;&lt;/span&gt; +
    &lt;span&gt;&amp;quot;Title = {Title.Substring(0, System.Math.Min(Title.Length, 20)) + (Title.Length &amp;gt; 20 ? \&amp;quot;&lt;b&gt;…&lt;/b&gt;\&amp;quot; : \&amp;quot;\&amp;quot;)}, &amp;quot;&lt;/span&gt; +
    &lt;span&gt;&amp;quot;Rating = {Rating}&amp;quot;&lt;/span&gt;)]
&lt;span&gt;public&lt;/span&gt; &lt;span&gt;class&lt;/span&gt; &lt;span&gt;Movie
&lt;/span&gt;{
    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;int&lt;/span&gt; Id { &lt;span&gt;get&lt;/span&gt;; &lt;span&gt;set&lt;/span&gt;; }
    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;string&lt;/span&gt; Title { &lt;span&gt;get&lt;/span&gt;; &lt;span&gt;set&lt;/span&gt;; }
    &lt;span&gt;public&lt;/span&gt; &lt;span&gt;float&lt;/span&gt; Rating { &lt;span&gt;get&lt;/span&gt;; &lt;span&gt;set&lt;/span&gt;; }
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p align="center"&gt;&amp;nbsp;&lt;img border="0" alt="Debugger display with complex expressions" src="http://gregbeech.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/tech/debugger_2D00_display_2D00_expression_2D00_view_5F00_2b03c126_2D00_06a2_2D00_4e23_2D00_a172_2D00_624d7746d92e.png" width="463" height="138" /&gt; &lt;/p&gt;
&lt;p&gt;There is a down-side to this power though - because evaluation of the &lt;em&gt;DebuggerDisplayAttribute&lt;/em&gt; format string is compiler-specific, the first example will work fine most other debuggers such as VB, but the second one will not evaluate due to its use of the C#-specific ternary operator. Depending on the debugger this will cause it to display either the default value of the class name, or an error indicating which part of the syntax could not be evaluated; this may be important if you work in a multi-language environment, or if you ship your classes as a library to customers.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://gregbeech.com/aggbug.aspx?PostID=288" width="1" height="1"&gt;</description><category domain="http://gregbeech.com/blogs/tech/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Tips+_2600_+Tricks/default.aspx">Tips &amp; Tricks</category></item><item><title>Dynamic sorting and paging in SQL Server with the CASE expression</title><link>http://gregbeech.com/blogs/tech/archive/2008/07/21/dynamic-sorting-and-paging-in-sql-server-with-the-case-expression.aspx</link><pubDate>Mon, 21 Jul 2008 22:04:21 GMT</pubDate><guid isPermaLink="false">64b9f9d3-1b55-4372-890f-9a35fe961b72:284</guid><dc:creator>Greg Beech</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://gregbeech.com/blogs/tech/rsscomments.aspx?PostID=284</wfw:commentRss><comments>http://gregbeech.com/blogs/tech/archive/2008/07/21/dynamic-sorting-and-paging-in-sql-server-with-the-case-expression.aspx#comments</comments><description>&lt;p&gt;The normal use for the &lt;em&gt;CASE&lt;/em&gt; expression in T-SQL is to perform simple modifications to one of the columns in a &lt;em&gt;SELECT&lt;/em&gt; statement; the type of example given in MSDN show it being used to expand state abbreviations such as &amp;#39;CA&amp;#39; to &amp;#39;California&amp;#39;, or to categorise products into &amp;#39;Cheap&amp;#39; and &amp;#39;Expensive&amp;#39; depending on their price. However, the &lt;em&gt;CASE&lt;/em&gt; expression is a lot more flexible than that, and can be used to implement dynamic sorting and paging without dynamic SQL.&lt;/p&gt; &lt;p&gt;Taking the classic Northwind database, here is a query that can retrieve pages of customers, sorted by either contact name, company name or country, ordered either ascending or descending:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;DECLARE&lt;/span&gt; @SortType &lt;span&gt;TINYINT&lt;/span&gt;&lt;span&gt;,&lt;/span&gt; @SortAscending &lt;span&gt;BIT&lt;/span&gt;&lt;span&gt;,&lt;/span&gt; @FirstRow &lt;span&gt;INT&lt;/span&gt;&lt;span&gt;,&lt;/span&gt; @MaxRows &lt;span&gt;INT&lt;/span&gt;&lt;span&gt;;
&lt;/span&gt;&lt;span&gt;SELECT&lt;/span&gt; @SortType &lt;span&gt;=&lt;/span&gt; 1&lt;span&gt;,&lt;/span&gt; @SortAscending &lt;span&gt;=&lt;/span&gt; 1&lt;span&gt;,&lt;/span&gt; @FirstRow &lt;span&gt;=&lt;/span&gt; 10&lt;span&gt;,&lt;/span&gt; @MaxRows &lt;span&gt;=&lt;/span&gt; 10&lt;span&gt;;

&lt;/span&gt;&lt;span&gt;WITH&lt;/span&gt; FoundCustomers &lt;span&gt;AS
&lt;/span&gt;&lt;span&gt;(
&lt;/span&gt;    &lt;span&gt;SELECT
&lt;/span&gt;        ROW_NUMBER&lt;span&gt;()&lt;/span&gt; &lt;span&gt;OVER
&lt;/span&gt;        &lt;span&gt;(
&lt;/span&gt;            &lt;span&gt;ORDER&lt;/span&gt; &lt;span&gt;BY&lt;/span&gt; &lt;span&gt;CASE&lt;/span&gt; @SortType
                &lt;span&gt;WHEN&lt;/span&gt; 0 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;ContactName
                &lt;span&gt;WHEN&lt;/span&gt; 1 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;CompanyName
                &lt;span&gt;WHEN&lt;/span&gt; 2 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;Country
            &lt;span&gt;END&lt;/span&gt; &lt;span&gt;ASC
&lt;/span&gt;        &lt;span&gt;)&lt;/span&gt; &lt;span&gt;AS&lt;/span&gt; RowNumberAsc
        &lt;span&gt;,&lt;/span&gt;ROW_NUMBER&lt;span&gt;()&lt;/span&gt; &lt;span&gt;OVER
&lt;/span&gt;        &lt;span&gt;(
&lt;/span&gt;            &lt;span&gt;ORDER&lt;/span&gt; &lt;span&gt;BY&lt;/span&gt; &lt;span&gt;CASE&lt;/span&gt; @SortType
                &lt;span&gt;WHEN&lt;/span&gt; 0 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;ContactName
                &lt;span&gt;WHEN&lt;/span&gt; 1 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;CompanyName
                &lt;span&gt;WHEN&lt;/span&gt; 2 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;Country
            &lt;span&gt;END&lt;/span&gt; &lt;span&gt;DESC
&lt;/span&gt;        &lt;span&gt;)&lt;/span&gt; &lt;span&gt;AS&lt;/span&gt; RowNumberDesc
        &lt;span&gt;,&lt;/span&gt;c&lt;span&gt;.*
&lt;/span&gt;    &lt;span&gt;FROM
&lt;/span&gt;        dbo&lt;span&gt;.&lt;/span&gt;Customers c
&lt;span&gt;)
&lt;/span&gt;&lt;span&gt;SELECT
&lt;/span&gt;    fc&lt;span&gt;.*
&lt;/span&gt;&lt;span&gt;FROM
&lt;/span&gt;    FoundCustomers fc
&lt;span&gt;WHERE
&lt;/span&gt;    &lt;span&gt;CASE&lt;/span&gt; @SortAscending
        &lt;span&gt;WHEN&lt;/span&gt; 1 &lt;span&gt;THEN&lt;/span&gt; fc&lt;span&gt;.&lt;/span&gt;RowNumberAsc
        &lt;span&gt;WHEN&lt;/span&gt; 0 &lt;span&gt;THEN&lt;/span&gt; fc&lt;span&gt;.&lt;/span&gt;RowNumberDesc
    &lt;span&gt;END
&lt;/span&gt;    &lt;span&gt;BETWEEN&lt;/span&gt; @FirstRow &lt;span&gt;AND&lt;/span&gt; @FirstRow &lt;span&gt;+&lt;/span&gt; @MaxRows &lt;span&gt;-&lt;/span&gt; 1
&lt;span&gt;ORDER&lt;/span&gt; &lt;span&gt;BY
&lt;/span&gt;    &lt;span&gt;CASE&lt;/span&gt; @SortAscending 
        &lt;span&gt;WHEN&lt;/span&gt; 1 &lt;span&gt;THEN&lt;/span&gt; fc&lt;span&gt;.&lt;/span&gt;RowNumberAsc 
        &lt;span&gt;WHEN&lt;/span&gt; 0 &lt;span&gt;THEN&lt;/span&gt; fc&lt;span&gt;.&lt;/span&gt;RowNumberDesc 
    &lt;span&gt;END&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Although this is an elegant solution, it does have some issues with performance; this particular query takes approximately twice as long as a statement hard-coded to use a given sort type and order. Looking at the execution plan shows the main cost in the query is the sorting (on my machine it is around 70% of the cost of the hard-coded query) and that even though &lt;em&gt;RowNumberDesc&lt;/em&gt; is never used the sort orders are still computed for it, which is where the extra time is going.&lt;/p&gt;
&lt;p&gt;In pursuit of performance, we&amp;#39;re going to have to repeat most of the statement by breaking the query into separate ascending and descending ones. This isn&amp;#39;t so good for maintenance, but as performance is critical in most databases the additional overhead of the combined query is the worse of two evils.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;DECLARE&lt;/span&gt; @SortType &lt;span&gt;TINYINT&lt;/span&gt;&lt;span&gt;,&lt;/span&gt; @SortAscending &lt;span&gt;BIT&lt;/span&gt;&lt;span&gt;,&lt;/span&gt; @FirstRow &lt;span&gt;INT&lt;/span&gt;&lt;span&gt;,&lt;/span&gt; @MaxRows &lt;span&gt;INT&lt;/span&gt;&lt;span&gt;;
&lt;/span&gt;&lt;span&gt;SELECT&lt;/span&gt; @SortType &lt;span&gt;=&lt;/span&gt; 2&lt;span&gt;,&lt;/span&gt; @SortAscending &lt;span&gt;=&lt;/span&gt; 1&lt;span&gt;,&lt;/span&gt; @FirstRow &lt;span&gt;=&lt;/span&gt; 10&lt;span&gt;,&lt;/span&gt; @MaxRows &lt;span&gt;=&lt;/span&gt; 10&lt;span&gt;;

&lt;/span&gt;&lt;span&gt;IF&lt;/span&gt; @SortAscending &lt;span&gt;=&lt;/span&gt; 1
&lt;span&gt;BEGIN
&lt;/span&gt;    &lt;span&gt;WITH&lt;/span&gt; FoundCustomers &lt;span&gt;AS
&lt;/span&gt;    &lt;span&gt;(
&lt;/span&gt;        &lt;span&gt;SELECT
&lt;/span&gt;            ROW_NUMBER&lt;span&gt;()&lt;/span&gt; &lt;span&gt;OVER
&lt;/span&gt;            &lt;span&gt;(
&lt;/span&gt;                &lt;span&gt;ORDER&lt;/span&gt; &lt;span&gt;BY&lt;/span&gt; &lt;span&gt;CASE&lt;/span&gt; @SortType
                    &lt;span&gt;WHEN&lt;/span&gt; 0 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;ContactName
                    &lt;span&gt;WHEN&lt;/span&gt; 1 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;CompanyName
                    &lt;span&gt;WHEN&lt;/span&gt; 2 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;Country
                &lt;span&gt;END&lt;/span&gt; &lt;span&gt;ASC
&lt;/span&gt;            &lt;span&gt;)&lt;/span&gt; &lt;span&gt;AS&lt;/span&gt; RowNumber
            &lt;span&gt;,&lt;/span&gt;c&lt;span&gt;.*
&lt;/span&gt;        &lt;span&gt;FROM
&lt;/span&gt;            dbo&lt;span&gt;.&lt;/span&gt;Customers c
    &lt;span&gt;)
&lt;/span&gt;    &lt;span&gt;SELECT
&lt;/span&gt;        fc&lt;span&gt;.*
&lt;/span&gt;    &lt;span&gt;FROM
&lt;/span&gt;        FoundCustomers fc
    &lt;span&gt;WHERE
&lt;/span&gt;        fc&lt;span&gt;.&lt;/span&gt;RowNumber &lt;span&gt;BETWEEN&lt;/span&gt; @FirstRow &lt;span&gt;AND&lt;/span&gt; @FirstRow &lt;span&gt;+&lt;/span&gt; @MaxRows &lt;span&gt;-&lt;/span&gt; 1
    &lt;span&gt;ORDER&lt;/span&gt; &lt;span&gt;BY
&lt;/span&gt;        fc&lt;span&gt;.&lt;/span&gt;RowNumber&lt;span&gt;;
&lt;/span&gt;&lt;span&gt;END
ELSE
BEGIN
&lt;/span&gt;    &lt;span&gt;WITH&lt;/span&gt; FoundCustomers &lt;span&gt;AS
&lt;/span&gt;    &lt;span&gt;(
&lt;/span&gt;        &lt;span&gt;SELECT
&lt;/span&gt;            ROW_NUMBER&lt;span&gt;()&lt;/span&gt; &lt;span&gt;OVER
&lt;/span&gt;            &lt;span&gt;(
&lt;/span&gt;                &lt;span&gt;ORDER&lt;/span&gt; &lt;span&gt;BY&lt;/span&gt; &lt;span&gt;CASE&lt;/span&gt; @SortType
                    &lt;span&gt;WHEN&lt;/span&gt; 0 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;ContactName
                    &lt;span&gt;WHEN&lt;/span&gt; 1 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;CompanyName
                    &lt;span&gt;WHEN&lt;/span&gt; 2 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;Country
                &lt;span&gt;END&lt;/span&gt; &lt;span&gt;DESC
&lt;/span&gt;            &lt;span&gt;)&lt;/span&gt; &lt;span&gt;AS&lt;/span&gt; RowNumber
            &lt;span&gt;,&lt;/span&gt;c&lt;span&gt;.*
&lt;/span&gt;        &lt;span&gt;FROM
&lt;/span&gt;            dbo&lt;span&gt;.&lt;/span&gt;Customers c
    &lt;span&gt;)
&lt;/span&gt;    &lt;span&gt;SELECT
&lt;/span&gt;        fc&lt;span&gt;.*
&lt;/span&gt;    &lt;span&gt;FROM
&lt;/span&gt;        FoundCustomers fc
    &lt;span&gt;WHERE
&lt;/span&gt;        fc&lt;span&gt;.&lt;/span&gt;RowNumber &lt;span&gt;BETWEEN&lt;/span&gt; @FirstRow &lt;span&gt;AND&lt;/span&gt; @FirstRow &lt;span&gt;+&lt;/span&gt; @MaxRows &lt;span&gt;-&lt;/span&gt; 1
    &lt;span&gt;ORDER&lt;/span&gt; &lt;span&gt;BY
&lt;/span&gt;        fc&lt;span&gt;.&lt;/span&gt;RowNumber&lt;span&gt;;
&lt;/span&gt;&lt;span&gt;END&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Examining the execution plan for this query shows that it is almost exactly the same as the query hard-coded to a particular sort type and order, with the addition of a &lt;em&gt;Compute Scalar&lt;/em&gt; step before the sort. This additional step takes less than 0.001% of the query time, so is a perfectly acceptable addition. We can infer from this that SQL Server can optimise a statement ordered by a &lt;em&gt;CASE&lt;/em&gt; expression equally as well as one ordered by a hard-coded column.&lt;/p&gt;
&lt;p&gt;The typical approach for dynamic sorting and paging that I&amp;#39;ve seen in the past has used dynamic SQL with &lt;em&gt;sp_executesql&lt;/em&gt; to achieve the results. By using the &lt;em&gt;CASE&lt;/em&gt; expression instead you can make your stored procedures verifiable at design time which aids database refactoring immensely, and achieve the same performance as hard-coded queries.&lt;/p&gt;
&lt;h3&gt;Update: 28 July 08&lt;/h3&gt;
&lt;p&gt;It turns out you actually don&amp;#39;t have to split the query into two to get optimal performance; my colleague &lt;a title="Jason Birth at LinkedIn" href="http://www.linkedin.com/in/jasonbirth"&gt;Jason&lt;/a&gt; was playing around with this some more and came up with a combined query using multiple searched &lt;em&gt;CASE&lt;/em&gt; expressions rather than the a single simple &lt;em&gt;CASE&lt;/em&gt; expression as shown above. Amazingly this is actually legal syntax, and gives the same optimal execution plan as the simple form:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;DECLARE&lt;/span&gt; @SortType &lt;span&gt;TINYINT&lt;/span&gt;&lt;span&gt;,&lt;/span&gt; @SortAscending &lt;span&gt;BIT&lt;/span&gt;&lt;span&gt;,&lt;/span&gt; @FirstRow &lt;span&gt;INT&lt;/span&gt;&lt;span&gt;,&lt;/span&gt; @MaxRows &lt;span&gt;INT&lt;/span&gt;&lt;span&gt;;
&lt;/span&gt;&lt;span&gt;SELECT&lt;/span&gt; @SortType &lt;span&gt;=&lt;/span&gt; 2&lt;span&gt;,&lt;/span&gt; @SortAscending &lt;span&gt;=&lt;/span&gt; 0&lt;span&gt;,&lt;/span&gt; @FirstRow &lt;span&gt;=&lt;/span&gt; 10&lt;span&gt;,&lt;/span&gt; @MaxRows &lt;span&gt;=&lt;/span&gt; 10&lt;span&gt;;

&lt;/span&gt;&lt;span&gt;WITH&lt;/span&gt; FoundCustomers &lt;span&gt;AS
&lt;/span&gt;&lt;span&gt;(
&lt;/span&gt;    &lt;span&gt;SELECT
&lt;/span&gt;        ROW_NUMBER&lt;span&gt;()&lt;/span&gt; &lt;span&gt;OVER
&lt;/span&gt;        &lt;span&gt;(
&lt;/span&gt;            &lt;span&gt;ORDER&lt;/span&gt; &lt;span&gt;BY&lt;/span&gt; 
                &lt;span&gt;CASE&lt;/span&gt; &lt;span&gt;WHEN&lt;/span&gt; @SortType &lt;span&gt;=&lt;/span&gt; 0 &lt;span&gt;AND&lt;/span&gt; @SortAscending &lt;span&gt;=&lt;/span&gt; 1 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;ContactName &lt;span&gt;END&lt;/span&gt; &lt;span&gt;ASC
&lt;/span&gt;                &lt;span&gt;,&lt;/span&gt;&lt;span&gt;CASE&lt;/span&gt; &lt;span&gt;WHEN&lt;/span&gt; @SortType &lt;span&gt;=&lt;/span&gt; 0 &lt;span&gt;AND&lt;/span&gt; @SortAscending &lt;span&gt;=&lt;/span&gt; 0 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;ContactName &lt;span&gt;END&lt;/span&gt; &lt;span&gt;DESC
&lt;/span&gt;                &lt;span&gt;,&lt;/span&gt;&lt;span&gt;CASE&lt;/span&gt; &lt;span&gt;WHEN&lt;/span&gt; @SortType &lt;span&gt;=&lt;/span&gt; 1 &lt;span&gt;AND&lt;/span&gt; @SortAscending &lt;span&gt;=&lt;/span&gt; 1 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;CompanyName &lt;span&gt;END&lt;/span&gt; &lt;span&gt;ASC
&lt;/span&gt;                &lt;span&gt;,&lt;/span&gt;&lt;span&gt;CASE&lt;/span&gt; &lt;span&gt;WHEN&lt;/span&gt; @SortType &lt;span&gt;=&lt;/span&gt; 1 &lt;span&gt;AND&lt;/span&gt; @SortAscending &lt;span&gt;=&lt;/span&gt; 0 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;CompanyName &lt;span&gt;END&lt;/span&gt; &lt;span&gt;DESC
&lt;/span&gt;                &lt;span&gt;,&lt;/span&gt;&lt;span&gt;CASE&lt;/span&gt; &lt;span&gt;WHEN&lt;/span&gt; @SortType &lt;span&gt;=&lt;/span&gt; 2 &lt;span&gt;AND&lt;/span&gt; @SortAscending &lt;span&gt;=&lt;/span&gt; 1 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;Country &lt;span&gt;END&lt;/span&gt; &lt;span&gt;ASC
&lt;/span&gt;                &lt;span&gt;,&lt;/span&gt;&lt;span&gt;CASE&lt;/span&gt; &lt;span&gt;WHEN&lt;/span&gt; @SortType &lt;span&gt;=&lt;/span&gt; 2 &lt;span&gt;AND&lt;/span&gt; @SortAscending &lt;span&gt;=&lt;/span&gt; 0 &lt;span&gt;THEN&lt;/span&gt; c&lt;span&gt;.&lt;/span&gt;Country &lt;span&gt;END&lt;/span&gt; &lt;span&gt;DESC
&lt;/span&gt;        &lt;span&gt;)&lt;/span&gt; &lt;span&gt;AS&lt;/span&gt; RowNumber
        &lt;span&gt;,&lt;/span&gt;c&lt;span&gt;.*
&lt;/span&gt;    &lt;span&gt;FROM
&lt;/span&gt;        dbo&lt;span&gt;.&lt;/span&gt;Customers c
&lt;span&gt;)
&lt;/span&gt;&lt;span&gt;SELECT
&lt;/span&gt;    fc&lt;span&gt;.*
&lt;/span&gt;&lt;span&gt;FROM
&lt;/span&gt;    FoundCustomers fc
&lt;span&gt;WHERE
&lt;/span&gt;    fc&lt;span&gt;.&lt;/span&gt;RowNumber &lt;span&gt;BETWEEN&lt;/span&gt; @FirstRow &lt;span&gt;AND&lt;/span&gt; @FirstRow &lt;span&gt;+&lt;/span&gt; @MaxRows &lt;span&gt;-&lt;/span&gt; 1
&lt;span&gt;ORDER&lt;/span&gt; &lt;span&gt;BY
&lt;/span&gt;    fc&lt;span&gt;.&lt;/span&gt;RowNumber&lt;span&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://gregbeech.com/aggbug.aspx?PostID=284" width="1" height="1"&gt;</description><category domain="http://gregbeech.com/blogs/tech/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Data+Access/default.aspx">Data Access</category></item><item><title>Emitting arbitrary constants with Reflection.Emit</title><link>http://gregbeech.com/blogs/tech/archive/2008/07/20/emitting-arbitrary-constants-with-reflection-emit.aspx</link><pubDate>Sun, 20 Jul 2008 22:51:57 GMT</pubDate><guid isPermaLink="false">64b9f9d3-1b55-4372-890f-9a35fe961b72:282</guid><dc:creator>Greg Beech</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://gregbeech.com/blogs/tech/rsscomments.aspx?PostID=282</wfw:commentRss><comments>http://gregbeech.com/blogs/tech/archive/2008/07/20/emitting-arbitrary-constants-with-reflection-emit.aspx#comments</comments><description>&lt;p&gt;I&amp;#39;ve been writing a fair bit of Reflection.Emit code recently, and along the way have developed quite a number of extension methods for the &lt;em&gt;ILGenerator&lt;/em&gt; class to &lt;a title="Emitting decimal pseudo-constants with Reflection.Emit" href="http://gregbeech.com/blogs/tech/archive/2008/07/07/emitting-decimal-pseudo-constants-with-reflection-emit.aspx"&gt;handle the complex semantics of some seemingly trivial operations&lt;/a&gt;, and choose the most efficient op-codes (e.g. when can the short forms be used). I had already written &lt;em&gt;EmitLdc&lt;/em&gt; methods to handle constant values for every type that can be represented as constant, but then came across the slightly odd circumstance where I had values that I knew could be emitted as constants, but they were in the form of a boxed &lt;em&gt;System.Object&lt;/em&gt; instance so couldn&amp;#39;t directly bind to the correct method.&lt;/p&gt; &lt;p&gt;The obvious solution is to add a new &lt;em&gt;EmitLdc&lt;/em&gt; overload that takes a plain object as an argument and performs manual casting and dispatch to the correct overload at runtime. Unfortunately as C# doesn&amp;#39;t allow types to be used in &lt;em&gt;switch&lt;/em&gt; statements, and doesn&amp;#39;t support pattern matching, there&amp;#39;s no trivial way to do this except with a very large &lt;em&gt;if/else&lt;/em&gt; statement or heavily nested ternary operators. These are both a bit inefficient and look clumsy, so a better option is with a static dictionary of &lt;em&gt;EmitLdc &lt;/em&gt;methods keyed by type.&lt;/p&gt; &lt;p&gt;Fortunately C# 3.0 introduced object initializers and lamda expressions which allow this dictionary to be built in a very concise and declarative manner: &lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;private&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;readonly&lt;/span&gt; &lt;span&gt;IDictionary&lt;/span&gt;&amp;lt;&lt;span&gt;Type&lt;/span&gt;, &lt;span&gt;Action&lt;/span&gt;&amp;lt;&lt;span&gt;ILGenerator&lt;/span&gt;, &lt;span&gt;object&lt;/span&gt;&amp;gt;&amp;gt; emitLdcMethods = 
    &lt;span&gt;new&lt;/span&gt; &lt;span&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span&gt;Type&lt;/span&gt;, &lt;span&gt;Action&lt;/span&gt;&amp;lt;&lt;span&gt;ILGenerator&lt;/span&gt;, &lt;span&gt;object&lt;/span&gt;&amp;gt;&amp;gt;
        {
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;bool&lt;/span&gt;), (il, val) =&amp;gt; il.EmitLdc((&lt;span&gt;bool&lt;/span&gt;)val) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;sbyte&lt;/span&gt;), (il, val) =&amp;gt; il.EmitLdc((&lt;span&gt;sbyte&lt;/span&gt;)val) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;byte&lt;/span&gt;), (il, val) =&amp;gt; il.EmitLdc((&lt;span&gt;byte&lt;/span&gt;)val) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;short&lt;/span&gt;), (il, val) =&amp;gt; il.EmitLdc((&lt;span&gt;short&lt;/span&gt;)val) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;ushort&lt;/span&gt;), (il, val) =&amp;gt; il.EmitLdc((&lt;span&gt;ushort&lt;/span&gt;)val) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;int&lt;/span&gt;), (il, val) =&amp;gt; il.EmitLdc((&lt;span&gt;int&lt;/span&gt;)val) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;uint&lt;/span&gt;), (il, val) =&amp;gt; il.EmitLdc((&lt;span&gt;uint&lt;/span&gt;)val) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;long&lt;/span&gt;), (il, val) =&amp;gt; il.EmitLdc((&lt;span&gt;long&lt;/span&gt;)val) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;ulong&lt;/span&gt;), (il, val) =&amp;gt; il.EmitLdc((&lt;span&gt;ulong&lt;/span&gt;)val) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;float&lt;/span&gt;), (il, val) =&amp;gt; il.EmitLdc((&lt;span&gt;float&lt;/span&gt;)val) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;double&lt;/span&gt;), (il, val) =&amp;gt; il.EmitLdc((&lt;span&gt;double&lt;/span&gt;)val) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;char&lt;/span&gt;), (il, val) =&amp;gt; il.EmitLdc((&lt;span&gt;char&lt;/span&gt;)val) },
            { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;decimal&lt;/span&gt;), (il, val) =&amp;gt; il.EmitLdc((&lt;span&gt;decimal&lt;/span&gt;)val) }
        };&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;The method can then be written to perform a key lookup from the dictionary based on the type of the value being emitted, and fail otherwise. &lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; EmitLdc(&lt;span&gt;this&lt;/span&gt; &lt;span&gt;ILGenerator&lt;/span&gt; il, &lt;span&gt;object&lt;/span&gt; value)
{
    &lt;span&gt;var&lt;/span&gt; valueType = value.GetType();
    &lt;span&gt;if&lt;/span&gt; (emitLdcMethods.ContainsKey(valueType))
    {
        &lt;span&gt;var&lt;/span&gt; emitLdc = emitLdcMethods[valueType];
        emitLdc(il, value);
    }
    &lt;span&gt;else&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; (valueType.IsEnum)
    {
        EmitLdc(il, (&lt;span&gt;Enum&lt;/span&gt;)value);
    }
    &lt;span&gt;else
&lt;/span&gt;    {
        &lt;span&gt;throw&lt;/span&gt; &lt;span&gt;new&lt;/span&gt; &lt;span&gt;NotSupportedException&lt;/span&gt;(&lt;span&gt;&amp;quot;The value cannot be emitted as a constant.&amp;quot;&lt;/span&gt;);
    }
}&lt;/pre&gt;The eagle-eyed will notice that there&amp;#39;s a special case for enumerated types, because these can be emitted as constants (being based on a CLR primitive type) but retrieving the type of an &lt;em&gt;Enum&lt;/em&gt; object will give the enumeration type, not the underlying type or &lt;em&gt;System.Enum&lt;/em&gt;, so won&amp;#39;t work with the dictionary approach. The &lt;em&gt;EmitLdc&lt;/em&gt; method for enums simply uses &lt;em&gt;Enum.GetUnderlyingType&lt;/em&gt; and then uses the same lookup dictionary with that type.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://gregbeech.com/aggbug.aspx?PostID=282" width="1" height="1"&gt;</description><category domain="http://gregbeech.com/blogs/tech/archive/tags/Reflection.Emit/default.aspx">Reflection.Emit</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/CIL/default.aspx">CIL</category></item><item><title>Service-orientation is inevitable for performant database applications</title><link>http://gregbeech.com/blogs/tech/archive/2008/07/16/service-orientation-is-inevitable-for-performant-database-applications.aspx</link><pubDate>Wed, 16 Jul 2008 22:05:31 GMT</pubDate><guid isPermaLink="false">64b9f9d3-1b55-4372-890f-9a35fe961b72:276</guid><dc:creator>Greg Beech</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://gregbeech.com/blogs/tech/rsscomments.aspx?PostID=276</wfw:commentRss><comments>http://gregbeech.com/blogs/tech/archive/2008/07/16/service-orientation-is-inevitable-for-performant-database-applications.aspx#comments</comments><description>&lt;p&gt;Databases cause two performance problems for applications. Firstly they are difficult to scale out compared to other server roles which means they can easily become the scalability bottleneck, and secondly they are on a remote machine which means that each query made to them involves network latency. To reduce processing overhead and latency, a common approach is to make as few queries as possible, and to only return as much data as is strictly needed for any query, meaning that fewer joins are likely in the database and the network payload is smaller.&lt;/p&gt; &lt;p&gt;The object-oriented approach to reducing payload size is lazy-loading, where only the most frequently used properties of objects are retrieved in the query, and properties that are thought to be less frequently used are loaded on-demand when they are accessed. Lets take a concrete example. Say you are modelling a movie with information such as the title, thumbnail, and actors you might model this in the object-oriented world as a &lt;em&gt;Movie&lt;/em&gt; class with &lt;em&gt;Title&lt;/em&gt;, &lt;em&gt;Thumbnail &lt;/em&gt;and &lt;em&gt;Actors &lt;/em&gt;properties. If you find that most of the time you only need to display the title and thumbnail but not the actors, then you might decide to lazy-load the &lt;em&gt;Actors &lt;/em&gt;property to remove the join in the database to the table of actors and reduce the network payload.&lt;/p&gt; &lt;p&gt;But what happens when you have a view of movies that needs to display the actors for each movie in the list? It&amp;#39;s not a commonly used view so you want to keep the actors lazily loaded (as always populating them would make the general case less efficient) but it&amp;#39;s not so infrequently used that poor performance can be ignored. &lt;/p&gt; &lt;p&gt;Without breaking encapsulation the only possible solution is that each movie in the list makes an independent call to the database to retrieve its actors, so you cause multiple database accesses in a relatively inefficient way (databases are optimised for processing data in sets so single rows are typically not much quicker than a small set) and add significant network latency too. To reduce the latency you could use a parallel loop, but even then you haven&amp;#39;t alleviated any of the database overhead which is the thing we&amp;#39;re most concerned about.&lt;/p&gt; &lt;p&gt;If you have collections of objects that have lazily loaded properties, you have to break encapsulation to improve performance.&lt;/p&gt; &lt;p&gt;We could break encapsulation only as far as the collection and say that movies have a specialised collection type and that they are aware of the collections they are contained in, so when a movie is asked to load its actors it requests the collection to load the actors for all the movies as a bulk operation. This means that the movie has to keep track of which collections it is in using callbacks from the add/insert/remove operations, and that you are restricted to specific types of collection which precludes the use of things like Linq-to-Objects. Breaking encapsulation in this way isn&amp;#39;t an attractive option.&lt;/p&gt; &lt;p&gt;The next level at which we could break encapsulation is to create a static method on the &lt;em&gt;Movie&lt;/em&gt; class that accepts a sequence of movies and populates the actors on each of them. We now aren&amp;#39;t restricted to specific collection types, and the population of the actor properties can be done in a single efficient batch operation, but this is no longer transparent to the user as they have to call a method to have the properties populated. Moreover, because the properties exist on the class this is somewhat unintuitive as you don&amp;#39;t expect to have to pass an object to a method to have its properties populated, so it probably makes more sense to remove the &lt;em&gt;Actors&lt;/em&gt; property from the &lt;em&gt;Movie&lt;/em&gt; object and have the method that does the bulk retrieval return a dictionary of actors by movie.&lt;/p&gt; &lt;p&gt;Unfortunately when it comes to testing this static method to retrieve the actors, we find that it isn&amp;#39;t very test-friendly as it can&amp;#39;t be stubbed or mocked, so there always has to be a database with suitable test data behind it. Instead of a static method, then, we&amp;#39;ll move it off the &lt;em&gt;Movie&lt;/em&gt; class and make it an instance method on a &lt;em&gt;MovieService&lt;/em&gt; class which is retrieved in an indirect way (such as from a service container) to enable stubbing and/or mocking. We&amp;#39;ve now got a highly performant and testable solution, but to achieve this we had to remove any direct relationship between movies and actors.&lt;/p&gt; &lt;p&gt;This scenario can be applied to any lazily loaded property on any type of object which can exist in collections, which leads us to the inescapable conclusion that lazy loading simply isn&amp;#39;t a reasonable option in most cases, thus objects must always be returned as a whole. As such, objects must not have a direct relationship with any other object that does not form part of that whole. Any properties that would have been lazily loaded are now separate objects, and retrieved in bulk by services as and when needed. The domain model has been transformed into a set of state objects with no direct relationship which are passed between services; it has been transformed into messages.&lt;/p&gt; &lt;p&gt;By optimising the performance of our object-oriented model, we arrived at a service-oriented message-passing architecture.&lt;/p&gt; &lt;p&gt;You may not like it. You may say that this isn&amp;#39;t the easiest model to program against. You may say it reduces the discoverability of related items and functionality. And I&amp;#39;m not going to argue with you on any of those points. But take consolation in the fact that it was inevitable from the start.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://gregbeech.com/aggbug.aspx?PostID=276" width="1" height="1"&gt;</description><category domain="http://gregbeech.com/blogs/tech/archive/tags/Performance/default.aspx">Performance</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Data+Access/default.aspx">Data Access</category></item><item><title>Object-Relational Mapping frameworks won't improve your timescales</title><link>http://gregbeech.com/blogs/tech/archive/2008/07/10/object-relational-mapping-frameworks-won-t-improve-your-timescales.aspx</link><pubDate>Thu, 10 Jul 2008 22:52:30 GMT</pubDate><guid isPermaLink="false">64b9f9d3-1b55-4372-890f-9a35fe961b72:273</guid><dc:creator>Greg Beech</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://gregbeech.com/blogs/tech/rsscomments.aspx?PostID=273</wfw:commentRss><comments>http://gregbeech.com/blogs/tech/archive/2008/07/10/object-relational-mapping-frameworks-won-t-improve-your-timescales.aspx#comments</comments><description>&lt;p&gt;On every database-backed project I&amp;#39;ve worked on, which is quite a few ranging from 4 to over 50 developers, the number of people working on the database is relatively small. Typically in a three-tier application the ratio of developers on UI:services:data is around 5:2:1 because high quality user interface development requires a lot of work, and it is far more time consuming to display data in an attractive way than it is to retrieve or save it. At &lt;a title="blinkBox" href="http://www.blinkbox.com"&gt;blinkBox&lt;/a&gt; the ratio is even higher at about 8:2:1.&lt;/p&gt; &lt;p&gt;This means that even if you assume that the services/data teams spend all their time writing data access code, and you could improve the productivity there, you still won&amp;#39;t save a huge amount of development time because your main overhead is in the user interface team, and the data access method doesn&amp;#39;t affect them too much (or shouldn&amp;#39;t if you&amp;#39;ve designed your API properly).&lt;/p&gt; &lt;p&gt;In reality services/data don&amp;#39;t spend all their time writing data access code or stored procedures. I&amp;#39;ve spent all of the last year leading our services team and working closely with the data team. As a rough estimate, I reckon I spend about 5% of my time writing data access code and the rest on thinking through scenarios, designing and creating higher level APIs, tuning caching, implementing security, implementing logging, adding performance counters, and troubleshooting issues that have nothing to do with data access because we use &lt;a title="A functional approach to data access code, updated for C# 3.0" href="http://gregbeech.com/blogs/tech/archive/2008/06/09/a-functional-approach-to-data-access-code-updated-for-c-3-0.aspx"&gt;a very thin layer on top of SqlClient&lt;/a&gt; which has no surprising side effects. The database guys probably spend about 20% of their time writing stored procedures, and the rest on schema refactoring (which is only possible due to the stored procedure layer), data migration, reporting, performance optimisation, and asynchronous data publishing&lt;/p&gt; &lt;p&gt;Which means that even if you reduce all the time spent on data access code time to zero, you could only save about (0.25 * 0.05) + (0.13 * 0.20) = 0.038 = 3.8% of your overall development time, which really isn&amp;#39;t significant enough to worry about. You&amp;#39;ll lose more time than that from hangovers due to office parties.&lt;/p&gt; &lt;p&gt;Any improvement in productivity in data access code will have a negligible effect on the timescales of a project, so don&amp;#39;t buy into Object-Relational Mapping frameworks on the premise that they will save you time, because even if they could the time just isn&amp;#39;t there to save.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://gregbeech.com/aggbug.aspx?PostID=273" width="1" height="1"&gt;</description><category domain="http://gregbeech.com/blogs/tech/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Data+Access/default.aspx">Data Access</category></item><item><title>Emitting decimal pseudo-constants with Reflection.Emit</title><link>http://gregbeech.com/blogs/tech/archive/2008/07/07/emitting-decimal-pseudo-constants-with-reflection-emit.aspx</link><pubDate>Mon, 07 Jul 2008 00:14:29 GMT</pubDate><guid isPermaLink="false">64b9f9d3-1b55-4372-890f-9a35fe961b72:270</guid><dc:creator>Greg Beech</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://gregbeech.com/blogs/tech/rsscomments.aspx?PostID=270</wfw:commentRss><comments>http://gregbeech.com/blogs/tech/archive/2008/07/07/emitting-decimal-pseudo-constants-with-reflection-emit.aspx#comments</comments><description>&lt;p&gt;Some languages such as C# let you treat the &lt;em&gt;System.Decimal&lt;/em&gt; type as an integral type most of the time, even having the keyword &lt;em&gt;decimal&lt;/em&gt;, and will let you declare constant fields of the type under some circumstances. However, decimal isn&amp;#39;t a primitive type in the same way as the other numerics, and is implemented quite differently from them. At CIL level, you can&amp;#39;t actually declare a constant decimal in the normal way; you have to create an &lt;a title="The SLAR (vol2) on System.Runtime.CompilerServices. DecimalConstantAttribute at Brad Abrams&amp;#39; Blog" href="http://blogs.msdn.com/brada/archive/2005/11/09/475095.aspx"&gt;&lt;em&gt;initonly&lt;/em&gt; field marked with &lt;em&gt;DecimalConstantAttribute&lt;/em&gt;&lt;/a&gt; which is understood by the CLR.&lt;/p&gt; &lt;p&gt;The logic shown here is optimised to emit decimal constants declared within a method body, however the logic at the bottom of the method could also be used for the constructor of &lt;em&gt;DecimalConstantAttribute&lt;/em&gt;. It&amp;#39;s interesting to note that the C# 3.0 compiler does not special-case any of the static fields in the same way as this method does, which may be because there&amp;#39;s such minimal performance difference that it isn&amp;#39;t really worth the added complexity.&lt;/p&gt; &lt;p&gt;Other &lt;em&gt;EmitLdc&lt;/em&gt; method calls within this are additional extension methods I implemented to cover other primitive data types, which do things such as special-casing the loading of the &lt;em&gt;i4&lt;/em&gt; type for the built-in constant values, and performing unchecked casting to emit unsigned values. If anyone wants me to post the code for them then I&amp;#39;m happy to do so, but they really are quite trivial.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; EmitLdc(&lt;span&gt;this&lt;/span&gt; &lt;span&gt;ILGenerator&lt;/span&gt; il, &lt;span&gt;decimal&lt;/span&gt; value)
{
    &lt;span&gt;const&lt;/span&gt; &lt;span&gt;BindingFlags&lt;/span&gt; FieldFlags = &lt;span&gt;BindingFlags&lt;/span&gt;.Public | &lt;span&gt;BindingFlags&lt;/span&gt;.Static;
    &lt;span&gt;if&lt;/span&gt; (value == &lt;span&gt;decimal&lt;/span&gt;.Zero)
    {
        il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Ldsfld, &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;decimal&lt;/span&gt;).GetField(&lt;span&gt;&amp;quot;Zero&amp;quot;&lt;/span&gt;, FieldFlags));
    }
    &lt;span&gt;else&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; (value == &lt;span&gt;decimal&lt;/span&gt;.One)
    {
        il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Ldsfld, &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;decimal&lt;/span&gt;).GetField(&lt;span&gt;&amp;quot;One&amp;quot;&lt;/span&gt;, FieldFlags));
    }
    &lt;span&gt;else&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; (value == &lt;span&gt;decimal&lt;/span&gt;.MinusOne)
    {
        il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Ldsfld, &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;decimal&lt;/span&gt;).GetField(&lt;span&gt;&amp;quot;MinusOne&amp;quot;&lt;/span&gt;, FieldFlags));
    }
    &lt;span&gt;else&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; (value == &lt;span&gt;decimal&lt;/span&gt;.MinValue)
    {
        il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Ldsfld, &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;decimal&lt;/span&gt;).GetField(&lt;span&gt;&amp;quot;MinValue&amp;quot;&lt;/span&gt;, FieldFlags));
    }
    &lt;span&gt;else&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; (value == &lt;span&gt;decimal&lt;/span&gt;.MaxValue)
    {
        il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Ldsfld, &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;decimal&lt;/span&gt;).GetField(&lt;span&gt;&amp;quot;MaxValue&amp;quot;&lt;/span&gt;, FieldFlags));
    }
    &lt;span&gt;else&lt;/span&gt; &lt;span&gt;if&lt;/span&gt; (&lt;span&gt;Math&lt;/span&gt;.Round(value) == value &amp;amp;&amp;amp; value &amp;gt;= &lt;span&gt;long&lt;/span&gt;.MinValue &amp;amp;&amp;amp; value &amp;lt;= &lt;span&gt;long&lt;/span&gt;.MaxValue)
    {
        &lt;span&gt;// if the decimal is a whole number then we can optimise the construction by pushing the smallest
&lt;/span&gt;        &lt;span&gt;// acceptable integer type value onto the stack and using the appropriate constructor
&lt;/span&gt;        &lt;span&gt;if&lt;/span&gt; (value &amp;gt;= &lt;span&gt;int&lt;/span&gt;.MinValue &amp;amp;&amp;amp; value &amp;lt;= &lt;span&gt;int&lt;/span&gt;.MaxValue)
        {
            EmitLdc(il, (&lt;span&gt;int&lt;/span&gt;)value);
            il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Newobj, &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;decimal&lt;/span&gt;).GetConstructor(&lt;span&gt;new&lt;/span&gt;[] { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;int&lt;/span&gt;) }));
        }
        &lt;span&gt;else
&lt;/span&gt;        {
            EmitLdc(il, (&lt;span&gt;long&lt;/span&gt;)value);
            il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Newobj, &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;decimal&lt;/span&gt;).GetConstructor(&lt;span&gt;new&lt;/span&gt;[] { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;long&lt;/span&gt;) }));
        }
    }
    &lt;span&gt;else
&lt;/span&gt;    {
        &lt;span&gt;// the Decimal.GetBits method returns four integers, the first three of which are the lo/med/hi parts 
&lt;/span&gt;        &lt;span&gt;// of the decimal and the fourth which is structured as follows:
&lt;/span&gt;        &lt;span&gt;// -------------------------------------------------------------------------------------------------
&lt;/span&gt;        &lt;span&gt;// |31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10|09|08|07|06|05|04|03|02|01|00|                     
&lt;/span&gt;        &lt;span&gt;// |ns|      not used      |         scale         |                   not used                    |
&lt;/span&gt;        &lt;span&gt;// -------------------------------------------------------------------------------------------------
&lt;/span&gt;        &lt;span&gt;// where ns is the negative sign (1 indicates negative) and the scale is a value between 1 and 28

&lt;/span&gt;        &lt;span&gt;var&lt;/span&gt; parts = &lt;span&gt;Decimal&lt;/span&gt;.GetBits(value);
        &lt;span&gt;Debug&lt;/span&gt;.Assert(parts.Length == 4, &lt;span&gt;&amp;quot;Expected the Decimal.GetBits method to return 4 parts&amp;quot;&lt;/span&gt;);
        &lt;span&gt;var&lt;/span&gt; negative = ((parts[3] &amp;gt;&amp;gt; 31) &amp;amp; 1) == 1;
        &lt;span&gt;var&lt;/span&gt; scale = (parts[3] &amp;gt;&amp;gt; 16) &amp;amp; 0xFF;

        EmitLdc(il, parts[0]);
        EmitLdc(il, parts[1]);
        EmitLdc(il, parts[2]);
        EmitLdc(il, negative);
        EmitLdc(il, scale);
        il.Emit(&lt;span&gt;OpCodes&lt;/span&gt;.Newobj, &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;decimal&lt;/span&gt;).GetConstructor(
            &lt;span&gt;new&lt;/span&gt;[] { &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;int&lt;/span&gt;), &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;int&lt;/span&gt;), &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;int&lt;/span&gt;), &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;bool&lt;/span&gt;), &lt;span&gt;typeof&lt;/span&gt;(&lt;span&gt;byte&lt;/span&gt;) }));
    }
}&lt;/pre&gt;There are optimisations that could be made here to make the emit code itself faster, namely caching the &lt;em&gt;decimal &lt;/em&gt;constructors in static fields so we don&amp;#39;t have to use reflection to find them each time. We could also set up a &lt;em&gt;Dictionary&amp;lt;decimal, Action&amp;gt;&lt;/em&gt; containing the &lt;em&gt;Ldsfld&lt;/em&gt; emit functions so there is only one specific-value check needed rather than five (which is essentially what a &lt;em&gt;switch&lt;/em&gt; statement would do, but because a decimal isn&amp;#39;t a real constant C# won&amp;#39;t allow a &lt;em&gt;switch&lt;/em&gt; statement based on it). For the sake of readability though, I&amp;#39;ve shown the code in a single method.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://gregbeech.com/aggbug.aspx?PostID=270" width="1" height="1"&gt;</description><category domain="http://gregbeech.com/blogs/tech/archive/tags/.NET/default.aspx">.NET</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/Reflection.Emit/default.aspx">Reflection.Emit</category><category domain="http://gregbeech.com/blogs/tech/archive/tags/CIL/default.aspx">CIL</category></item></channel></rss>