<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Over It. &#187; C#</title>
	<atom:link href="http://jason.diamond.name/weblog/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://jason.diamond.name/weblog</link>
	<description>(a Weblog by Jason Diamond)</description>
	<lastBuildDate>Mon, 30 Jan 2012 20:08:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Why won&#8217;t my iterator throw?</title>
		<link>http://jason.diamond.name/weblog/2010/10/27/why-wont-my-iterator-throw/</link>
		<comments>http://jason.diamond.name/weblog/2010/10/27/why-wont-my-iterator-throw/#comments</comments>
		<pubDate>Wed, 27 Oct 2010 23:56:19 +0000</pubDate>
		<dc:creator>Jason Diamond</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://jason.diamond.name/weblog/?p=116</guid>
		<description><![CDATA[I ran into a spot of confusion last night doing some TDD on a method that returns an IEnumerable&#60;T>. I was using multiple yield return statements in it which made the method an iterator and not just a normal method. Even though I know how iterators work, I don&#8217;t use them enough to remember their [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into a spot of confusion last night doing some TDD on a method that returns an <code>IEnumerable&lt;T></code>. I was using multiple <code>yield return</code> statements in it which made the method an <a href="http://msdn.microsoft.com/en-us/library/dscyy5s0.aspx">iterator</a> and not just a normal method.</p>
<p>Even though I <em>know</em> how iterators work, I don&#8217;t use them enough to remember their idiosyncrasies. The main one being that it&#8217;s easy to &#8220;invoke&#8221; them without actually executing any of the code inside them!</p>
<p>To illustrate this using a highly contrived example, imagine you wrote the following test:</p>
<pre class="brush:csharp">[Test]
public void It_throws_when_you_pass_in_null()
{
    Assert.Throws&lt;ArgumentNullException>(
        () => MyObject.MyMethod(null));
}</pre>
<p>And then implemented the method it tests it like so:</p>
<pre class="brush:csharp">public static IEnumerable&lt;object> MyMethod(object arg)
{
    if (arg == null)
        throw new ArgumentNullException("arg");

    yield return "whatever";
}</pre>
<p>Surprise! Your test fails.</p>
<p>Why? Because the code in your iterator doesn&#8217;t start executing until you invoke <code>GetEnumerator</code> on its return value then invoke <code>MoveNext</code> on that.</p>
<p>One really quick way to force these method calls to happen is to use the <code>ToArray</code> extension method:</p>
<pre class="brush:csharp">[Test]
public void It_throws_when_you_pass_in_null()
{
    Assert.Throws&lt;ArgumentNullException>(
        () => MyObject.MyMethod(null).ToArray());
}</pre>
<p><code>ToList</code> or manually iterating over the result with <code>foreach</code> would work just as well.</p>
<p>A better way to fix this is to change your implementation so that it uses two methods:</p>
<pre class="brush:csharp">public static IEnumerable&lt;object> MyMethod(object arg)
{
    if (arg == null)
        throw new ArgumentNullException("arg");

    return MyMethodHelper(arg);
}

private static IEnumerable&lt;object> MyMethodHelper(object arg)
{
    yield return "whatever";
}</pre>
<p>Written this way, <code>MyMethod</code> isn&#8217;t an iterator anymore. It&#8217;s just a plain old method that gets executed the way you&#8217;d expect it to. <code>MyMethodHelper</code> becomes the iterator. Its code won&#8217;t get executed until you start calling <code>MoveNext</code>, but that&#8217;s OK because the validation code you care about already ran.</p>
<p>Problem solved, right? Unfortunately, this wasn&#8217;t quite my <em>exact</em> problem.</p>
<p>My method was actually throwing <em>after</em> it did its argument checking and <em>while</em> it was yielding values. There&#8217;s really no solution (that I know of) to handle this without invoking <code>MoveNext</code> (or something else that will invoke it for you like <code>ToArray</code>) until whatever condition throws your exception is triggered.</p>
<p>Adding <code>ToArray</code> to my test wasn&#8217;t a big deal, but it took me a bit of time to figure out why it was failing. I was actually setting breakpoints in my method, running the test in the debugger, and tripping out when my breakpoints weren&#8217;t hitting.</p>
<p>Maybe going through the trouble of writing this post will save me 10 minutes next time I write an iterator.</p>
]]></content:encoded>
			<wfw:commentRss>http://jason.diamond.name/weblog/2010/10/27/why-wont-my-iterator-throw/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>One-line lazy initialization</title>
		<link>http://jason.diamond.name/weblog/2010/02/23/one-line-lazy-initialization/</link>
		<comments>http://jason.diamond.name/weblog/2010/02/23/one-line-lazy-initialization/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 22:54:19 +0000</pubDate>
		<dc:creator>Jason Diamond</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://jason.diamond.name/weblog/?p=91</guid>
		<description><![CDATA[Is this too obscure? public class MyObject { private MyExpensiveObject _myExpensiveObject; public MyExpensiveObject ExpensiveObject { get { return _myExpensiveObject ?? (_myExpensiveObject = new MyExpensiveObject()); } } }]]></description>
			<content:encoded><![CDATA[<p>Is this too obscure?</p>
<pre class="brush:csharp">public class MyObject
{
    private MyExpensiveObject _myExpensiveObject;

    public MyExpensiveObject ExpensiveObject
    {
        get { return _myExpensiveObject ??
                     (_myExpensiveObject = new MyExpensiveObject()); }
    }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://jason.diamond.name/weblog/2010/02/23/one-line-lazy-initialization/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>RogueSharp</title>
		<link>http://jason.diamond.name/weblog/2010/02/15/roguesharp/</link>
		<comments>http://jason.diamond.name/weblog/2010/02/15/roguesharp/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 02:49:44 +0000</pubDate>
		<dc:creator>Jason Diamond</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Rogue]]></category>

		<guid isPermaLink="false">http://jason.diamond.name/weblog/?p=86</guid>
		<description><![CDATA[I just pushed a pet project of mine up to GitHub today. It&#8217;s a port of the original game of Rogue from C to C#. It&#8217;s not complete&#8211;it doesn&#8217;t save your progress and let you continue on later, but it is playable. Now that the actual port is done, I plan on cleaning up the [...]]]></description>
			<content:encoded><![CDATA[<p>I just pushed a pet project of mine up to GitHub today. It&#8217;s a port of the original game of <a href="http://en.wikipedia.org/wiki/Rogue_(computer_game)">Rogue</a> from C to C#. It&#8217;s not complete&#8211;it doesn&#8217;t save your progress and let you continue on later, but it is playable.</p>
<p>Now that the actual port is done, I plan on cleaning up the code. Like most software written in C in the 80s, it relies on way too many global variables, uses an abundance of switch statements on object types, and has no abstraction between the game logic and the UI. Since it&#8217;s on GitHub, that should leave a nice history of the refactorings I try to apply to it. Of course, before I can try to do any refactoring, I need to get some tests in place. That should be fun considering how much it relies on direct access to the <a href="http://en.wikipedia.org/wiki/Curses_(programming_library)">curses</a> library and a random number generator.</p>
<p>The repository is <a href="http://github.com/jdiamond/rogue-sharp">here</a>. Feel free to fork it to help out if you like.</p>
]]></content:encoded>
			<wfw:commentRss>http://jason.diamond.name/weblog/2010/02/15/roguesharp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Splitting CamelCase With Regular Expressions</title>
		<link>http://jason.diamond.name/weblog/2009/08/15/splitting-camelcase-with-regular-expressions/</link>
		<comments>http://jason.diamond.name/weblog/2009/08/15/splitting-camelcase-with-regular-expressions/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 22:22:40 +0000</pubDate>
		<dc:creator>Jason Diamond</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://jason.diamond.name/weblog/?p=27</guid>
		<description><![CDATA[On my new project, I needed to split a method name up into its constituent parts. For Ruby programmers, this is easy: just split on underscores. For us .NET programmers, we need something a little fancier since we like to SquashOurMethodNamesTogetherLikeThis. Here&#8217;s a little regular expression that can do exactly that: (?&#60;!^)(?=[A-Z]) That basically says [...]]]></description>
			<content:encoded><![CDATA[<p>On my new project, I needed to split a method name up into its constituent parts.</p>
<p>For Ruby programmers, this is easy: just split on underscores. For us .NET programmers, we need something a little fancier since we like to <code>SquashOurMethodNamesTogetherLikeThis</code>.</p>
<p>Here&#8217;s a little regular expression that can do exactly that:</p>
<pre>(?&lt;!^)(?=[A-Z])</pre>
<p>That basically says to match right before every capital letter unless the capital letter is at the beginning of the string.</p>
<p>You can use the Regex.Split method to do the actual work of turning the method name into a string array:</p>
<pre class="brush:csharp">
Regex splitter = new Regex(@"(?&lt;!^)(?=[A-Z])");
string[] words = splitter.Split(methodName);
</pre>
<p>My friend, <a href="http://www.michaelckennedy.net/">Michael Kennedy</a>, demoed in class recently how to use <a href="http://www.linqpad.net/">LINQPad</a> to test regular expressions. It came in really handy while working on this:</p>
<p><img src="http://jason.diamond.name/weblog/wp-content/uploads/2009/08/linqpad-with-regex.png" alt="linqpad-with-regex" title="linqpad-with-regex" width="667" height="362" class="alignnone size-full wp-image-32" /></p>
<p>I really need to click that &#8220;Activate Autocompletion&#8221; button so I can give <a href="http://www.albahari.com/">Joseph Albahari</a> the money he deserves for creating such a useful tool.</p>
<p>By the way, I used to think that &#8220;camel case&#8221; was for words like &#8220;camelCase&#8221; and &#8220;pascal case&#8221; was for words like &#8220;PascalCase&#8221;, but <a href="http://en.wikipedia.org/wiki/PascalCase">Wikipedia</a> doesn&#8217;t make that distinction.</p>
]]></content:encoded>
			<wfw:commentRss>http://jason.diamond.name/weblog/2009/08/15/splitting-camelcase-with-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>My UnWordify Extension Method</title>
		<link>http://jason.diamond.name/weblog/2009/08/09/my-unwordify-extension-method/</link>
		<comments>http://jason.diamond.name/weblog/2009/08/09/my-unwordify-extension-method/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 04:41:55 +0000</pubDate>
		<dc:creator>Jason Diamond</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://jason.diamond.name/weblog/?p=8</guid>
		<description><![CDATA[Tim Barcz is looking for a solution to an interesting problem in his Coding Contest: Create a Programming Pearl post. I decided to challenge myself by writing my solution as a single &#8220;line&#8221; of code: public static class StringExtensions { private static Dictionary&#60;char, char> _charMap = new Dictionary&#60;char, char> { { (char)8208, (char)45 }, { [...]]]></description>
			<content:encoded><![CDATA[<p>Tim Barcz is looking for a solution to an interesting problem in his <a href="http://devlicio.us/blogs/tim_barcz/archive/2009/08/09/coding-contest-create-a-programming-pearl.aspx">Coding Contest: Create a Programming Pearl</a> post.</p>
<p>I decided to challenge myself by writing my solution as a single &#8220;line&#8221; of code:</p>
<pre class="brush:csharp">public static class StringExtensions
{
    private static Dictionary&lt;char, char> _charMap = new Dictionary&lt;char, char>
    {
        { (char)8208,  (char)45 },
        { (char)8211,  (char)45 },
        { (char)8212,  (char)45 },
        { (char)8722,  (char)45 },
        { (char)173,   (char)45 },
        { (char)8209,  (char)45 },
        { (char)8259,  (char)45 },
        { (char)96,    (char)39 },
        { (char)8216,  (char)39 },
        { (char)8217,  (char)39 },
        { (char)8242,  (char)39 },
        { (char)769,   (char)39 },
        { (char)768,   (char)39 },
        { (char)8220,  (char)34 },
        { (char)8221,  (char)34 },
        { (char)8243,  (char)34 },
        { (char)12291, (char)34 },
        { (char)160,   (char)32 },
        { (char)8195,  (char)32 },
        { (char)8194,  (char)32 }
    };

    public static string UnWordify(this string value)
    {
        return new string((from c in value
                           select _charMap.ContainsKey(c) ? _charMap[c] : c)
                          .ToArray());
    }
}</pre>
<p>OK, so I cheated and used a dictionary to create my map from the bad characters to the good ones, but the actual method <em>is</em> a single statement.</p>
]]></content:encoded>
			<wfw:commentRss>http://jason.diamond.name/weblog/2009/08/09/my-unwordify-extension-method/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

