<?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>Elegant Code &#187; Sean Timm</title>
	<atom:link href="http://elegantcode.com/author/stimm/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 20 Jul 2010 12:52:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Comparing Bytes</title>
		<link>http://elegantcode.com/2010/01/03/comparing-bytes/</link>
		<comments>http://elegantcode.com/2010/01/03/comparing-bytes/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 18:21:01 +0000</pubDate>
		<dc:creator>Sean Timm</dc:creator>
				<category><![CDATA[Esoterica]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/01/03/comparing-bytes/</guid>
		<description><![CDATA[While preparing for another post (coming soon), I ran across the need to compare two arrays of bytes.&#160; I’ve done this in the past, but when I started poking around I realized that I had forgotten that there was only a manual method of doing so… that is until .NET 3.5!&#160; You can now use [...]]]></description>
			<content:encoded><![CDATA[<p>While preparing for another post (coming soon), I ran across the need to compare two arrays of bytes.&#160; I’ve done this in the past, but when I started poking around I realized that I had forgotten that there was only a manual method of doing so… that is until .NET 3.5!&#160; You can now use LINQ’s <a href="http://msdn.microsoft.com/en-us/library/bb348567.aspx">SequenceEqual extension method</a>:</p>
<div id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">using</span> System.Linq;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #0000ff">namespace</span> CompareBytes</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>     <span style="color: #0000ff">class</span> Program</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>         <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Main(<span style="color: #0000ff">string</span>[] args)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>             var foo = <span style="color: #0000ff">new</span> <span style="color: #0000ff">byte</span>[] {1, 2};</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>             var bar = <span style="color: #0000ff">new</span> <span style="color: #0000ff">byte</span>[] {1, 2, 3};</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>             var isEqual = foo.SequenceEqual(bar);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>             ...</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>&#160;</p>
<p>Note that you can use this extension method for any other sequence comparisons (including custom objects – you’ll need to implement <a href="http://msdn.microsoft.com/en-us/library/ms132151.aspx">IEqualityComparer&lt;T&gt;</a> in your custom class).</p>
<p>Go forth and compare manually no longer!</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/01/03/comparing-bytes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Review: The Passionate Programmer</title>
		<link>http://elegantcode.com/2009/11/29/review-the-passionate-programmer/</link>
		<comments>http://elegantcode.com/2009/11/29/review-the-passionate-programmer/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 00:43:47 +0000</pubDate>
		<dc:creator>Sean Timm</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[Craftsmanship]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/29/review-the-passionate-programmer/</guid>
		<description><![CDATA[As an Elegant Coder, I suffer the fate of sitting in front of a computer screen for large parts of my day, and I can often be found at home in front of one as well.&#160; This can promote a fairly sedentary lifestyle if one is not careful.&#160; Over the years, I have been both [...]]]></description>
			<content:encoded><![CDATA[<p>As an Elegant Coder, I suffer the fate of sitting in front of a computer screen for large parts of my day, and I can often be found at home in front of one as well.&#160; This can promote a fairly sedentary lifestyle if one is not careful.&#160; Over the years, I have been both more and less careful in this regard at various times.&#160; This has resulted in my current state – a state we will refer to (for purposes of this conversation) – as “overweight”.&#160; I’ve participated in a number of weight loss programs over the years until I finally wised up and realized that every successful program had at its heart two common factors – caloric restriction and exercise.&#160; I am now actively engaged in working both of those factors until I achieve a state we will call “fit”.&#160; However, becoming fit is not an immediate process.&#160; It is a daily process requiring both attention and discipline.&#160; I’m fond of saying, “It took me 15 years to achieve this weight.&#160; Do you expect it to be gone in 30 days?”</p>
<p><a href="http://www.amazon.com/Passionate-Programmer-Remarkable-Development-Pragmatic/dp/1934356344%3FSubscriptionId%3D0JTCV5ZMHMF7ZYTXGFR2%26tag%3Delegantcode-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D1934356344">The Passionate Programmer: Creating a Remarkable Career in Software Development</a> by <a href="http://chadfowler.com/">Chad Fowler</a> is a book focused on helping you become fit as a developer – leading you along the path of life decisions that make the difference between a job and a remarkable career.&#160; As Chad says, “You don’t win a race by trying not to lose.&#160; And you don’t win at life by trying not to suck.”&#160; You have to be tactical, strategic, and deliberate if you want to avoid your career being “one big series of undirected coincidences.”</p>
<p>I won’t belabor all of the points you can find in this book, but I’d like to call out some things that stood out to me.</p>
<ul>
<li>Be the worst
<ul>
<li>If you’re the best, nothing is challenging and pushing you to grow/perform.&#160; Seek associations with people that are better/faster/smarter than you. </li>
<li>You’re not as bad as you assume.&#160; Give yourself the chance to discover it. </li>
</ul>
</li>
<li>Practice at your limits
<ul>
<li>Chad’s background as a musician provided opportunity to compare skill improvement to music practice.&#160; While performances are expected to be as perfect as possible, practice is raw, gritty, and doesn’t always sound good. </li>
<li>If all you’re ever doing is sitting down churning out elegant code, you’re not pushing at the edges (where all the improvement comes from). </li>
</ul>
</li>
<li>Study the work of masters
<ul>
<li>In the arts (and many other disciplines), much time is given to learning from the work of those that have gone before.&#160; In the past, this was more difficult in software development due to its proprietary nature, but the sheer volume and quality of open source projects makes this much more accessible now. </li>
<li>Existing code reflects on your own capabilities for growth and new avenues of thought. </li>
</ul>
</li>
<li>Focus on the present
<ul>
<li>If your head is always in the clouds of tomorrow, you’ll miss the daily victories. </li>
</ul>
<p> One topic Chad alluded to but didn’t build much on was approaching your career development in an Agile fashion.&#160; I thought this was a great concept, and from some comments on his blog, it looks like he’d like to build on this in the future.&#160; My takeaway was that I’d better start my sprint planning for my career.</li>
</ul>
<p>There were many other points and topics covered including interaction with peers, management, and customers.&#160; Chad includes a number of “Act on It” exercises which will challenge you to grow in the areas he covers.&#160; Overall, it was a great read with a message I needed to hear, and I’d highly recommend it.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/29/review-the-passionate-programmer/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Connecting to Live Mesh with the Live ID Client SDK</title>
		<link>http://elegantcode.com/2009/03/14/connecting-to-live-mesh-with-the-live-id-client-sdk/</link>
		<comments>http://elegantcode.com/2009/03/14/connecting-to-live-mesh-with-the-live-id-client-sdk/#comments</comments>
		<pubDate>Sat, 14 Mar 2009 21:29:48 +0000</pubDate>
		<dc:creator>Sean Timm</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[Live Framework]]></category>
		<category><![CDATA[Live ID]]></category>
		<category><![CDATA[Live Mesh]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/03/14/connecting-to-live-mesh-with-the-live-id-client-sdk/</guid>
		<description><![CDATA[Microsoft’s Live Framework team recently released the April CTP update of the Live Framework SDK and Tools.&#160; This update has been released publicly on the Download Center, so you can grab it and get started right away.
If you want to run the Live Mesh Client beta and the Live Framework Client side by side, you’ll [...]]]></description>
			<content:encoded><![CDATA[<p>Microsoft’s Live Framework team <a href="http://blogs.msdn.com/liveframework/archive/2009/03/11/live-framework-updated.aspx">recently released</a> the April CTP update of the Live Framework SDK and Tools.&#160; This update has been released publicly on the <a href="http://go.microsoft.com/fwlink/?LinkID=145417">Download Center</a>, so you can grab it and get started right away.</p>
<p>If you want to run the Live Mesh Client beta and the Live Framework Client side by side, you’ll want to make sure and <a href="http://social.msdn.microsoft.com/Forums/en-US/liveframework/thread/2fa9d3f1-029e-44af-9568-1cea81ca78a9">follow the instructions</a> prior to installing the April CTP Update.&#160; Note that you will actually be developing against the Live Framework Client and cannot currently target the Live Mesh Client.&#160; I assume these will merge at some point, and you’ll be able to play in both the live runtime as well as the development runtime, but for now, you’re stuck in the developer playground, so we’ll technically be connecting to the Live Framework Client (not the Live Mesh Client) for now.</p>
<p>I’ve been working on a Live Mesh demo rich client application, and I figured I may as well use the <a href="http://go.microsoft.com/fwlink/?linkid=86974">Live ID Client SDK</a> rather than worry about secure credential storage.&#160; This proved to be a bit challenging for a number of reasons, so I thought I’d post my working formula for those that might follow this dark path.</p>
<p>It turns out that the Live ID Client SDK only supports the x86 platform, so the first thing you’ll run across is the installer blows up while installing it on the x64 platform.&#160; It turns out that it still manages to drop the DLLs you’ll need in <strong>%CommonProgramFiles(x86)%\Microsoft Shared\WLIDClient</strong>.</p>
<p>Create a WPF Application project and add references to Microsoft.LiveFX.Client, Microsoft.LiveFX.ResourceModel, and Microsoft.WindowsLive.Id.Client (found in the aforementioned path as well as <strong>%ProgramFiles(x86)%\Microsoft SDKs\Live Framework\v0.91\API Toolkits\.Net Library</strong>).&#160; Since the Live ID Client SDK will only work for x86, you’ll need to change your Project Settings for the project that will be utilizing the SDK (highlighted below):</p>
<p><a href="http://elegantcode.com/wp-content/uploads/2009/03/image1.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="241" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/03/image-thumb1.png" width="499" border="0" /></a> </p>
<p>While you’re here, add a DefaultUserName setting to store the user’s Live ID name.&#160; We’ll use this later for automatic sign-in:</p>
<p><a href="http://elegantcode.com/wp-content/uploads/2009/03/image2.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="257" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/03/image-thumb2.png" width="491" border="0" /></a> </p>
<p>Now that all that’s out of the way, we can get on with writing code.&#160; Throw a button on the default layout, rename it, and add a click handler.&#160; Inside the click handler, add the following code:</p>
<div style="font-size: 10pt; background: white; color: black; font-family: courier new">
<p><span style="color: #2b91af">IdentityManager</span> manager = <span style="color: #2b91af">IdentityManager</span>.CreateInstance(</p>
<p style="margin: 0px; padding-top: 0px">&#160;&#160;&#160; <span style="color: #a31515">&quot;Sean Timm;thread@gmail.com;Live Mesh Demo&quot;</span>,</p>
<p style="margin: 0px; padding-top: 0px">&#160;&#160;&#160; <span style="color: #a31515">&quot;Live Mesh Demo&quot;</span>);</p>
<p style="margin: 0px; padding-top: 0px">&#160;</p>
<p style="margin: 0px; padding-top: 0px"><span style="color: blue">string</span> currentUser = <span style="color: #2b91af">Settings</span>.Default.DefaultUserName;</p>
<p style="margin: 0px; padding-top: 0px"><span style="color: #2b91af">Identity</span> identity;</p>
<p style="margin: 0px; padding-top: 0px"><span style="color: blue">if</span>(<span style="color: #2b91af">String</span>.IsNullOrEmpty(currentUser))</p>
<p style="margin: 0px; padding-top: 0px">{</p>
<p style="margin: 0px; padding-top: 0px">&#160;&#160;&#160; identity = manager.CreateIdentity();</p>
<p style="margin: 0px; padding-top: 0px">}</p>
<p style="margin: 0px; padding-top: 0px"><span style="color: blue">else</span></p>
<p style="margin: 0px; padding-top: 0px">{</p>
<p style="margin: 0px; padding-top: 0px">&#160;&#160;&#160; identity = manager.CreateIdentity(currentUser);</p>
<p style="margin: 0px; padding-top: 0px">}</p>
</p></div>
<p>This code creates the Identity instance that will provide your authentication token.&#160; <font face="Courier New">IdentityManager.CreateInstance</font> takes two parameters: an application ID and an application name.&#160; The application ID is composed of three parts: <em>Company Name</em>;<em>Email Address</em>;<em>Application Name</em>.&#160; In practice, I’ve found I can set this to whatever I want, but the documentation and examples call it out this way, so you’re probably best to stick with this format.&#160; The application name passed in as the second parameter is used during display of the sign in dialog.</p>
<p>If a <font face="Courier New">DefaultUserName</font> has previously been saved, it will instantiate the instance using the value to take advantage of any stored credentials for automatic sign in.&#160; Now add the logic to do the actual sign in:</p>
<div style="font-size: 10pt; background: white; color: black; font-family: courier new">
<p><span style="color: blue">if</span>(identity.SavedCredentials == <span style="color: #2b91af">CredentialType</span>.UserNameAndPassword)</p>
<p style="margin: 0px; padding-top: 0px">{</p>
<p style="margin: 0px; padding-top: 0px">&#160;&#160;&#160; identity.Authenticate(<span style="color: #2b91af">AuthenticationType</span>.Silent);</p>
<p style="margin: 0px; padding-top: 0px">}</p>
<p style="margin: 0px; padding-top: 0px">&#160;</p>
<p style="margin: 0px; padding-top: 0px"><span style="color: blue">if</span>(!identity.IsAuthenticated)</p>
<p style="margin: 0px; padding-top: 0px">{</p>
<p style="margin: 0px; padding-top: 0px">&#160;&#160;&#160; identity.Authenticate();</p>
<p style="margin: 0px; padding-top: 0px">&#160;&#160;&#160; <span style="color: #2b91af">Settings</span>.Default.DefaultUserName = identity.UserName;</p>
<p style="margin: 0px; padding-top: 0px">&#160;&#160;&#160; <span style="color: #2b91af">Settings</span>.Default.Save();</p>
<p style="margin: 0px; padding-top: 0px">}</p>
</p></div>
<p>This logic checks if both the username and password have been stored from a previous login (based on the <font face="Courier New">DefaultUserName</font> passed into the instance).&#160; If they are available, it authenticates silently.&#160; Otherwise, you’ll get the standard sign in dialog:</p>
<p><a href="http://elegantcode.com/wp-content/uploads/2009/03/image3.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="479" alt="image" src="http://elegantcode.com/wp-content/uploads/2009/03/image-thumb3.png" width="444" border="0" /></a> </p>
<p>Notice the application name above the blue guy.&#160; That’s the value you passed in during creation of the <font face="Courier New">IdentityManager</font>.&#160; Once you log in, the username is saved for future automatic sign-in (if you checked Remember my Password).</p>
<p>To connect to the <font face="Courier New">LiveOperatingEnvironment</font> in the Live Framework Client, you’ll need an authentication token.&#160; You can retrieve an authentication token from your authenticated identity via the following:</p>
<div style="font-size: 10pt; background: white; color: black; font-family: courier new">
<p><span style="color: blue">string</span> token = identity.GetTicket(<span style="color: #a31515">&quot;user-ctp.windows.net&quot;</span>, <span style="color: #a31515">&quot;MBI_SSL&quot;</span>, <span style="color: blue">true</span>);</p>
</p></div>
<p>There are a number of other ways to obtain a token.&#160; If you had been using credentials directly (rather than through the Live ID Client SDK), you could have done so via the following:</p>
<div style="font-size: 10pt; background: white; color: black; font-family: courier new">
<p><span style="color: blue">string</span> token = <span style="color: blue">new</span> System.Net.<span style="color: #2b91af">NetworkCredential</span>(<span style="color: #a31515">&quot;myliveid&quot;</span>, <span style="color: #a31515">&quot;mypassword&quot;</span>).GetWindowsLiveAuthenticationToken();</p>
</p></div>
<p>Now that you have a token, it’s time to connect to the <font face="Courier New">LiveOperatingEnvironment</font>:</p>
<div style="font-size: 10pt; background: white; color: black; font-family: courier new">
<p><span style="color: blue">var</span> uri = <span style="color: blue">new</span> <span style="color: #2b91af">Uri</span>(<span style="color: #a31515">&quot;https://user-ctp.windows.net&quot;</span>);</p>
<p style="margin: 0px; padding-top: 0px"><span style="color: blue">var</span> loe = <span style="color: blue">new</span> <span style="color: #2b91af">LiveOperatingEnvironment</span>();</p>
<p style="margin: 0px; padding-top: 0px">loe.Connect(token,</p>
<p style="margin: 0px; padding-top: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">AuthenticationTokenType</span>.UserToken,</p>
<p style="margin: 0px; padding-top: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; uri,</p>
<p style="margin: 0px; padding-top: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">LiveItemAccessOptions</span>(<span style="color: blue">true</span>));</p>
</p></div>
<p>Once this returns, you’re good to go.&#160; It will automatically connect to either the local or cloud environment depending on what’s available, and you can start playing around with the Mesh!</p>
<p>There are a number of asynchronous call alternatives that you can use when it makes sense.&#160; I opted to throw it all in a thread to avoid an unresponsive UI.&#160; Here’s the completed class:</p>
<pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 550px; padding-top: 5px; border-bottom: #cecece 1px solid; background-color: #fbfbfb">
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"><span style="color: #0000ff">using</span> System;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"><span style="color: #0000ff">using</span> System.Threading;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"><span style="color: #0000ff">using</span> System.Windows;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"><span style="color: #0000ff">using</span> MeshDemo.Properties;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"><span style="color: #0000ff">using</span> Microsoft.LiveFX.Client;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"><span style="color: #0000ff">using</span> Microsoft.LiveFX.ResourceModel;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"><span style="color: #0000ff">using</span> Microsoft.WindowsLive.Id.Client;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"></pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"><span style="color: #0000ff">namespace</span> MeshDemo
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">{
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">  <span style="color: #808080">/// &lt;summary&gt;</span>
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">  <span style="color: #808080">/// Interaction logic for MainWindow.xaml</span>
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">  <span style="color: #808080">/// &lt;/summary&gt;</span>
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">  <span style="color: #0000ff">public</span> partial <span style="color: #0000ff">class</span> MainWindow : Window
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">  {
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">    <span style="color: #0000ff">readonly</span> LiveOperatingEnvironment loe = <span style="color: #0000ff">new</span> LiveOperatingEnvironment();
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"></pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">    <span style="color: #0000ff">public</span> MainWindow()
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">    {
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">      InitializeComponent();
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">    }
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"></pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> signInButton_Click(<span style="color: #0000ff">object</span> sender, RoutedEventArgs e)
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">    {
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">      signInButton.IsEnabled = <span style="color: #0000ff">false</span>;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"></pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">      var thread = <span style="color: #0000ff">new</span> Thread(
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">        () =&gt;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">        {
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          IdentityManager manager = IdentityManager.CreateInstance(
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            &quot;<span style="color: #8b0000">Sean Timm;thread@gmail.com;Live Mesh Demo</span>&quot;,
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            &quot;<span style="color: #8b0000">Live Mesh Demo</span>&quot;);
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"></pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          <span style="color: #0000ff">string</span> currentUser = Settings.Default.DefaultUserName;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          Identity identity;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          <span style="color: #0000ff">if</span>(String.IsNullOrEmpty(currentUser))
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          {
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            identity = manager.CreateIdentity();
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          }
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          <span style="color: #0000ff">else</span>
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          {
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            identity = manager.CreateIdentity(currentUser);
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          }
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"></pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          <span style="color: #0000ff">if</span>(identity.SavedCredentials ==
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            CredentialType.UserNameAndPassword)
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          {
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            identity.Authenticate(AuthenticationType.Silent);
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          }
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"></pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          <span style="color: #0000ff">if</span>(!identity.IsAuthenticated)
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          {
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            identity.Authenticate();
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            Settings.Default.DefaultUserName = identity.UserName;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            Settings.Default.Save();
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          }
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"></pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          <span style="color: #0000ff">string</span> errorMessage = String.Empty;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          <span style="color: #0000ff">bool</span> isConnected = <span style="color: #0000ff">false</span>;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          <span style="color: #0000ff">if</span>(identity.IsAuthenticated)
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          {
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            <span style="color: #0000ff">string</span> token = identity.GetTicket(
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">              &quot;<span style="color: #8b0000">user-ctp.windows.net</span>&quot;, &quot;<span style="color: #8b0000">MBI_SSL</span>&quot;, <span style="color: #0000ff">true</span>);
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            var uri = <span style="color: #0000ff">new</span> Uri(&quot;<span style="color: #8b0000">https://user-ctp.windows.net</span>&quot;);
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"></pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            <span style="color: #0000ff">try</span>
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            {
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">              loe.Connect(token, AuthenticationTokenType.UserToken, uri,
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">                    <span style="color: #0000ff">new</span> LiveItemAccessOptions(<span style="color: #0000ff">true</span>));
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"></pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">              Action signInComplete = OnSignInComplete;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">              Dispatcher.BeginInvoke(signInComplete);
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">              isConnected = <span style="color: #0000ff">true</span>;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            }
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            <span style="color: #0000ff">catch</span> (Exception ex)
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            {
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">              errorMessage = ex.Message;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            }
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          }
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          <span style="color: #0000ff">else</span>
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          {
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            errorMessage = &quot;<span style="color: #8b0000">Failed to authenticate</span>&quot;;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          }
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"></pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          <span style="color: #0000ff">if</span>(!isConnected)
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          {
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            Action&lt;<span style="color: #0000ff">string</span>&gt; signInFailed = OnSignInFailed;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">            Dispatcher.BeginInvoke(signInFailed, errorMessage);
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          }
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">        });
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"></pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">      thread.SetApartmentState(ApartmentState.STA);
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">      thread.Start();
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">    }
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"></pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> OnSignInComplete()
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">    {
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">      signInButton.Visibility = Visibility.Collapsed;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">    }
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"></pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> OnSignInFailed(<span style="color: #0000ff">string</span> message)
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">    {
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">      signInButton.IsEnabled = <span style="color: #0000ff">true</span>;
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">      <span style="color: #0000ff">if</span>(String.IsNullOrEmpty(message))
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">      {
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">        MessageBox.Show(&quot;<span style="color: #8b0000">Sign in failed for some frustrating reason.</span>&quot;);
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">      }
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">      <span style="color: #0000ff">else</span>
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">      {
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">        MessageBox.Show(String.Format(
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">          &quot;<span style="color: #8b0000">Sign in failed for this frustrating reason: {0}</span>&quot;, message));
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">      }
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">    }
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">  }
</pre>
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">}</pre>
</pre>
<p>Notice I explicitly set the thread to single-threaded apartment.&#160; The Live ID Client SDK COM dependencies require this.</p>
<p>Now that you can connect, it’s time to go forth and make a Mesh of things!</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/03/14/connecting-to-live-mesh-with-the-live-id-client-sdk/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Refactoring the Refactored Switch Statement</title>
		<link>http://elegantcode.com/2009/01/31/refactoring-the-refactored-switch-statement/</link>
		<comments>http://elegantcode.com/2009/01/31/refactoring-the-refactored-switch-statement/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 07:17:00 +0000</pubDate>
		<dc:creator>Sean Timm</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/01/31/refactoring-the-refactored-switch-statement/</guid>
		<description><![CDATA[Semantic Competence and Disquotational Knowledge
In her book, Relevance and Linguistic Meaning: The Semantics and Pragmatics of Discourse Markers, Diane Blakemore speaks, among other things, of incoherent interpretation and contextual assumptions.  While pondering these phrases, I was reminded of Chris Brandsma’s recent article on Refactoring a switch statement.
You see, it’s not that Chris Brandsma is wrong, [...]]]></description>
			<content:encoded><![CDATA[<h2>Semantic Competence and Disquotational Knowledge</h2>
<p>In her book, <a href="http://www.amazon.com/Relevance-Linguistic-Meaning-Pragmatics-Linguistics/dp/0521640075">Relevance and Linguistic Meaning: The Semantics and Pragmatics of Discourse Markers</a>, Diane Blakemore speaks, among other things, of <a href="http://www.amazon.com/phrase/incoherent-interpretation/ref=sip_bod_13">incoherent interpretation</a> and <a href="http://www.amazon.com/phrase/contextual-assumptions/ref=sip_bod_18">contextual assumptions</a>.  While pondering these phrases, I was reminded of <a href="http://elegantcode.com/about/chris-brandsma/">Chris Brandsma</a>’s recent article on <a href="http://elegantcode.com/2009/01/10/refactoring-a-switch-statement/">Refactoring a switch statement</a>.</p>
<p>You see, it’s not that <a href="http://elegantcode.com/2009/01/12/please-tell-me-when-im-wrong-really/">Chris Brandsma is wrong</a>, per se… it’s that I believe that his advice in this matter applies, at best, to a very small number of people who better already understand what they are doing.  For the large majority of developers who still misuse the word “refactoring”, the article merely builds on their disquotational knowledge.</p>
<p><em>Disquotational knowledge</em> describes the concept of knowing something without understanding it.  <strong>There is wealth of this knowledge in our industry</strong>.  For me, it’s more important to firmly establish the 90% case and allow for discovery of the 10% case in time.  To that end…</p>
<h2>Sean Timm’s rules of proper switch statement etiquette:</h2>
<ol>
<li>If your code has multiple switch statements working with a common subset of case instances, the switch must go away.</li>
<li>If you find yourself about to write a switch statement:
<ul>
<li>Stop, and mentally confirm that:
<ul>
<li>You’re not already doing another switch on a subset of these same case instances.</li>
<li>You don’t plan on doing another switch on a subset of these same case instances.</li>
<li>You can’t for the life of you figure out why anyone else would ever do another switch on a subset of these same case instances.</li>
<li>You are willing to get rid of this switch statement when you discover that you actually do need to switch on a subset of these same case instances.</li>
</ul>
</li>
<li>If any of the above are not true, the switch must go away.</li>
</ul>
</li>
<li>If you can’t articulate precisely why a switch must stay, the switch must go away.</li>
<li>If you’re not working in a language that supports polymorphic behavior, ignore all of this advice, and do something like Chris suggested.</li>
</ol>
<h2>A Brief Analysis of the Problem</h2>
<p>Let’s take a quick look at the <a href="http://elegantcode.com/2009/01/10/refactoring-a-switch-statement/">original problem</a>:</p>
<div style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; height: 259px; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;">
<div style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> ReturnSomething(<span style="color: #0000ff">string</span> name, <span style="color: #0000ff">string</span> <span style="color: #0000ff">value</span>)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">switch</span> (name)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>         <span style="color: #0000ff">case</span> <span style="color: #006080">"Chris"</span>:</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>             <span style="color: #0000ff">return</span> _valueProcessor.Chris(<span style="color: #0000ff">value</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>         <span style="color: #0000ff">case</span> <span style="color: #006080">"David"</span>:</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>             <span style="color: #0000ff">return</span> _valueProcessor.David(<span style="color: #0000ff">value</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span>         ...</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">  12:</span> }</pre>
</div>
</div>
<p>Can anyone tell me what will return when you pass “Chris” and another value into this function?  Of course not… we’ve lost all semantic meaning in this code.  We could fix this with something like the following:</p>
<div style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;">
<div style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> ReturnSomething(<span style="color: #0000ff">string</span> name, <span style="color: #0000ff">string</span> <span style="color: #0000ff">value</span>)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">switch</span> (name)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>         <span style="color: #0000ff">case</span> <span style="color: #006080">"Chris"</span>:</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>             <span style="color: #0000ff">return</span> _valueProcessor.PrependLastNameOfChrisToValue(<span style="color: #0000ff">value</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>         <span style="color: #0000ff">case</span> <span style="color: #006080">"David"</span>:</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>             <span style="color: #0000ff">return</span> _valueProcessor.PrependLastNameOfDavidToValue(<span style="color: #0000ff">value</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span>         ...</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">  12:</span> }</pre>
</div>
</div>
<p>Ok, that’s a bit awkward, but we’ll run with it.  Since we wrote this code, we’ve discovered that we need to add some new functionality, so we stick to the code:</p>
<div style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;">
<div style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> ReturnSomethingElse(<span style="color: #0000ff">string</span> name, <span style="color: #0000ff">string</span> <span style="color: #0000ff">value</span>)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">switch</span> (name)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>         <span style="color: #0000ff">case</span> <span style="color: #006080">"Chris"</span>:</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>             <span style="color: #0000ff">return</span> _valueProcessor.LookupNoteToChris(<span style="color: #0000ff">value</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>         <span style="color: #0000ff">case</span> <span style="color: #006080">"David"</span>:</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>             <span style="color: #0000ff">return</span> _valueProcessor.LookupNoteToDavid(<span style="color: #0000ff">value</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span>         ...</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">  12:</span> }</pre>
</div>
</div>
<p>Pretty soon, your interface is starting to look like this:</p>
<div style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;">
<div style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IValueProcessor</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">string</span> PrependLastNameOfChrisToValue(<span style="color: #0000ff">string</span> data);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">string</span> LookupNoteToChris(<span style="color: #0000ff">string</span> noteKey);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">byte</span> TheHandThatFeedsChris();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>     <span style="color: #0000ff">bool</span> AskChrisIfItIsReallyStillAGoodIdeaThatYouAreGoingDownThisPath();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>     <span style="color: #0000ff">void</span> AllKnowledgeOfWhatYouThoughtWasReallyAGoodIdeaThatYouGotFromChris();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span>     <span style="color: #0000ff">string</span> PrependLastNameOfDavidToValue(<span style="color: #0000ff">string</span> data);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span>     <span style="color: #0000ff">string</span> LookupNoteToDavid(<span style="color: #0000ff">string</span> noteKey);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span>     <span style="color: #0000ff">byte</span> TheHandThatFeedsDavid();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">  12:</span>     <span style="color: #0000ff">bool</span> AskDavidIfItIsReallyStillAGoodIdeaThatYouAreGoingDownThisPath();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">  13:</span>     ...</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">  14:</span> }</pre>
</div>
</div>
<h2>Dealing with Code Tumors</h2>
<p>When you’re facing uncontrollable growth like this, it’s time to get in and do some surgery.  It should have been dealt with back when it was easier to operate on because now it has spread throughout your code.</p>
<p>First, we introduce a test because we want to remove this malignant growth without interfering with the rest of the code’s ability to function:</p>
<div style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;">
<div style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> [TestFixture]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> SwitchRefactorDemoTest</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> DoSomethingWithChrisTest()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>         var switchDemo = <span style="color: #0000ff">new</span> SwitchDemo(<span style="color: #0000ff">new</span> ValueProcessor());</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>         var result = switchDemo.ReturnSomething(<span style="color: #006080">"Chris"</span>, <span style="color: #006080">"Hey"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span>         Assert.That(result, Is.EqualTo(<span style="color: #006080">"BrandsmaHey"</span>));</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">  12:</span>     ...</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">  13:</span> }</pre>
</div>
</div>
<p>ValueProcessor is being used directly in this test because we’re attempting to get rid of it (oh, I didn’t mention that?).  Among other things, ValueProcessor is clearly violating the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a>.  This class is doing way too much!  For the purposes of this discussion, I won’t get into details on why this is important, but feel free to add this to your disquotational knowledgebase.</p>
<p>For this example, we’ll be looking at the refactoring known as <a href="http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html">Replace Conditional with Polymorphism</a>.  You may also want to spend some time investigating <a href="http://www.refactoring.com/catalog/replaceConditionalWithVisitor.pdf">Replace Conditional with Visitor</a> as a solid combination of both these techniques can be required at times.</p>
<p>We’re refactoring, so we’ll go step by step and avoid breaking functionality.  We should be able to stop at any time and get back to work on that new defect we just got assigned without worrying about the state of the code.</p>
<p>First, we’ll work with the Chris method of ValueProcessor which currently looks like:</p>
<div style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;">
<div style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ValueProcessor : IValueProcessor</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Chris(<span style="color: #0000ff">string</span> data)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>         <span style="color: #0000ff">return</span> <span style="color: #006080">"Brandsma"</span> + data;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>     ...</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span> }</pre>
</div>
</div>
<p>We do an <a href="http://www.refactoring.com/catalog/extractMethod.html">Extract Method</a> on the logic, and things now look like:</p>
<div style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;">
<div style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Chris(<span style="color: #0000ff">string</span> data)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">return</span> PrependLastNameTo(data);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span> }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> PrependLastNameTo(<span style="color: #0000ff">string</span> data)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>     <span style="color: #0000ff">return</span> <span style="color: #006080">"Brandsma"</span> + data;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span> }</pre>
</div>
</div>
<p>We run the tests, and everything still passes.  This will be our new method name, but we have a lot more work to do before the conditional is gone.  For now, we’ll just <a href="http://www.refactoring.com/catalog/moveMethod.html">Move Method</a> and delegate our call to the new ChrisProcessor class that we create:</p>
<div style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;">
<div style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Chris(<span style="color: #0000ff">string</span> data)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     var processor = <span style="color: #0000ff">new</span> ChrisProcessor();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">return</span> processor.PrependLastNameTo(data);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span> }</pre>
</div>
</div>
<p>We run the tests, and everything still passes.  Now we go ahead and do this for each of the other conditionals.  We run the tests after each step, and everything continues to pass.  At last, all of the logic has been delegated to these new classes, and the code in the functions looks the same other than which processor we’re instantiating.</p>
<p>We change ChrisProcessor to derive from ValueProcessor, add the PrependLastNameTo() method to the IValueProcessor interface and add a default implementation to ValueProcessor that throws an exception:</p>
<div style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;">
<div style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ValueProcessor : IValueProcessor</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">string</span> PrependLastNameTo(<span style="color: #0000ff">string</span> data)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>         <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> System.NotImplementedException();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>     ...</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span> }</pre>
</div>
</div>
<p>This method is currently not called, and as we modify our conditional to call it, we shouldn’t get the exception because each subclass will override the method.  ChrisProcessor is now ready to be fully responsible for its behavior, but we have an interesting situation with this code.</p>
<p>An IValueProcessor implementation is currently being passed in to the SwitchDemo class.  This interface was being used by the switch statement to call into the correct conditional logic, but that can now happen automatically.  Most likely, we aren’t going to need to pass the IValueProcessor in at all (since the whole SwitchDemo class will likely disappear).</p>
<p>For now, we’ll introduce a factory method that instantiates the correct ValueProcessor subclass (yes, I appreciate the irony of introducing a new switch method, but this is temporary and may disappear depending on the external callers… if not, use Chris’ dictionary for this <img src='http://elegantcode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ):</p>
<div style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;">
<div style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">class</span> ValueProcessorFactory</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> IValueProcessor GetValueProcessor(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>         <span style="color: #0000ff">string</span> processorKey,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>         IValueProcessor valueProcessor)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>         <span style="color: #0000ff">switch</span>(processorKey)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>         {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span>             <span style="color: #0000ff">case</span> <span style="color: #006080">"Chris"</span>:</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span>                 <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> ChrisProcessor();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span>             <span style="color: #0000ff">default</span>:</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">  12:</span>                 <span style="color: #0000ff">return</span> valueProcessor;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">  13:</span>         }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">  14:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">  15:</span> }</pre>
</div>
</div>
<p>Note that we’re doing these one at a time, so we start with just replacing the ChrisProcessor and add the others as we go:</p>
<div style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;">
<div style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> ReturnSomething(<span style="color: #0000ff">string</span> name, <span style="color: #0000ff">string</span> <span style="color: #0000ff">value</span>)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     IValueProcessor processor = ValueProcessorFactory.GetValueProcessor(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>         name, _valueProcessor);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">switch</span> (name)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>         <span style="color: #0000ff">case</span> <span style="color: #006080">"Chris"</span>:</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>             <span style="color: #0000ff">return</span> processor.PrependLastNameTo(<span style="color: #0000ff">value</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span>         <span style="color: #0000ff">case</span> <span style="color: #006080">"David"</span>:</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span>              <span style="color: #0000ff">return</span> _valueProcessor.David(<span style="color: #0000ff">value</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span>         ....</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">  12:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">  13:</span> }</pre>
</div>
</div>
<p>Run the tests.  Everything passes.  Do the same thing with each of the other conditionals, and you finally end up with removing the conditional statement altogether:</p>
<div style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;">
<div style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> ReturnSomething(<span style="color: #0000ff">string</span> name, <span style="color: #0000ff">string</span> <span style="color: #0000ff">value</span>)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     IValueProcessor processor = ValueProcessorFactory.GetValueProcessor(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>         name, _valueProcessor);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">return</span> processor.PrependLastNameTo(<span style="color: #0000ff">value</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span> }</pre>
</div>
</div>
<p>At this point, it’s likely that you don’t even need the ReturnSomething method, so you <a href="http://www.refactoring.com/catalog/inlineMethod.html">Inline Method</a> and move the factory instantiation to wherever it makes sense.  Now you go and do the same to all your other switch statements that you need to push into these subclasses.  Lather, rinse, repeat.</p>
<h2>Afterthoughts</h2>
<p>When you’re disciplined about these things, you’ll find yourself realizing you could have taken a different path and gotten to the same place (or perhaps a better place) a lot sooner.  While working through this example, I realized that rather than delegating the PrependLastNameTo method to the new ChrisProcessor subclass when I was getting started, I should have created a ChrisProcessor class that took IValueProcessor in its constructor.  This would have allowed me to call into the old Chris() function in ValueProcessor while working to modify the conditional logic.  In real code where things are more complicated than this, I probably would have needed access to the IValueProcessor beyond its use in the ReturnSomething method, so keeping it around for delegation purposes makes a lot more sense. Perhaps next time… – Sean Timm</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/01/31/refactoring-the-refactored-switch-statement/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
