<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Strongly-Typing Your Domain Values</title>
	<atom:link href="http://www.xerxesb.com/2008/strongly-typing-your-domain-values/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xerxesb.com/2008/strongly-typing-your-domain-values/</link>
	<description>&#039;Cause there are some things you just *can&#039;t* get from a book...</description>
	<lastBuildDate>Fri, 12 Nov 2010 21:27:27 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
	<item>
		<title>By: David James</title>
		<link>http://www.xerxesb.com/2008/strongly-typing-your-domain-values/comment-page-1/#comment-961</link>
		<dc:creator>David James</dc:creator>
		<pubDate>Mon, 21 Apr 2008 15:26:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.xerxesb.no-ip.org/?p=87#comment-961</guid>
		<description>We found a related refactoring to this recently which I thought was really cool.

Say you want to add a new status called Imported for issues which are newly added, but came from some other database instead of being added by hand.

So you change your code from:

if (newStatus == IssueStatus.New)
{
  // send a &quot;new issue created&quot; email
}


To:

if (newStatus == IssueStatus.New &#124;&#124; newStatus == IssueStatus.Imported)
{
  // send a &quot;new issue created&quot; email
}



Then you find some other place in the code that also needs to change:

if (newStatus == IssueStatus.New)
{
  // create a record in the issue billing system
}

...becomes:

if (newStatus == IssueStatus.New &#124;&#124; newStatus == IssueStatus.Imported)
{
  // create a record in the issue billing system
}


That conditional expression for the if statements is duplicate code!

So if you&#039;ve used a strong type IssueStatus, instead of a string, you can easily add a property to your IssueStatus class:

public bool IsNewlyCreated { get; }
// make it true for New and Imported, false otherwise


Then refactor to:

if (newStatus.IsNewlyCreated)
{
  // whatever
}

Suddenly your if statements become much more expressive about _why_ the code block should be run. If we choose the property names well, then the code is not just saying &quot;if status is X or Y&quot;, it&#039;s telling you the business meaning behind it. And if we choose the property names well, we should be able to come along in future and add a new status called Z, set its properties appropriately, and existing code will already understand it.</description>
		<content:encoded><![CDATA[<p>We found a related refactoring to this recently which I thought was really cool.</p>
<p>Say you want to add a new status called Imported for issues which are newly added, but came from some other database instead of being added by hand.</p>
<p>So you change your code from:</p>
<p>if (newStatus == IssueStatus.New)<br />
{<br />
  // send a &#8220;new issue created&#8221; email<br />
}</p>
<p>To:</p>
<p>if (newStatus == IssueStatus.New || newStatus == IssueStatus.Imported)<br />
{<br />
  // send a &#8220;new issue created&#8221; email<br />
}</p>
<p>Then you find some other place in the code that also needs to change:</p>
<p>if (newStatus == IssueStatus.New)<br />
{<br />
  // create a record in the issue billing system<br />
}</p>
<p>&#8230;becomes:</p>
<p>if (newStatus == IssueStatus.New || newStatus == IssueStatus.Imported)<br />
{<br />
  // create a record in the issue billing system<br />
}</p>
<p>That conditional expression for the if statements is duplicate code!</p>
<p>So if you&#8217;ve used a strong type IssueStatus, instead of a string, you can easily add a property to your IssueStatus class:</p>
<p>public bool IsNewlyCreated { get; }<br />
// make it true for New and Imported, false otherwise</p>
<p>Then refactor to:</p>
<p>if (newStatus.IsNewlyCreated)<br />
{<br />
  // whatever<br />
}</p>
<p>Suddenly your if statements become much more expressive about _why_ the code block should be run. If we choose the property names well, then the code is not just saying &#8220;if status is X or Y&#8221;, it&#8217;s telling you the business meaning behind it. And if we choose the property names well, we should be able to come along in future and add a new status called Z, set its properties appropriately, and existing code will already understand it.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 2/6 queries in 0.008 seconds using disk: basic
Object Caching 237/241 objects using disk: basic

Served from: www.xerxesb.com @ 2012-02-09 00:04:09 -->
