<?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>Agile at Work &#187; Refactoring</title>
	<atom:link href="http://www.agileatwork.com/tag/refactoring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.agileatwork.com</link>
	<description>by Michael Valenty</description>
	<lastBuildDate>Sat, 10 Sep 2011 14:35:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Refactoring C# Style</title>
		<link>http://www.agileatwork.com/refactoring-c-style/</link>
		<comments>http://www.agileatwork.com/refactoring-c-style/#comments</comments>
		<pubDate>Sat, 27 Nov 2010 17:51:00 +0000</pubDate>
		<dc:creator>Michael Valenty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Lambda]]></category>
		<category><![CDATA[Refactoring]]></category>

		<guid isPermaLink="false">http://www.agileatwork.com/?p=524</guid>
		<description><![CDATA[Take a look a this function.

[UnitOfWork]
public virtual void Upload(DocumentDto dto)
{
&#160;&#160;&#160; var entity = new Document().Merge(dto);
&#160;
&#160;&#160;&#160; using (var stream = new MemoryStream(dto.Data))
&#160;&#160;&#160; {
&#160;&#160;&#160;&#160;&#160;&#160;&#160; repository.Save(entity, stream);
&#160;&#160;&#160; }
}
&#160;

After doing some performance profiling, it quickly popped up as the top offender. Why? Because it’s holding a database transaction open while saving a file. In order to fix it, we [...]]]></description>
			<content:encoded><![CDATA[<p>Take a look a this function.</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px">[<span style="color: #2b91af">UnitOfWork</span>]</pre>
<pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">void</span> Upload(<span style="color: #2b91af">DocumentDto</span> dto)</pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">var</span> entity = <span style="color: blue">new</span> <span style="color: #2b91af">Document</span>().Merge(dto);</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">using</span> (<span style="color: blue">var</span> stream = <span style="color: blue">new</span> <span style="color: #2b91af">MemoryStream</span>(dto.Data))</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; repository.Save(entity, stream);</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">}</pre>
<pre style="margin: 0px">&#160;</pre>
</div>
<p>After doing some performance profiling, it quickly popped up as the top offender. Why? Because it’s holding a database transaction open while saving a file. In order to fix it, we have to ditch our <font size="2" face="Courier New">UnitOfWork</font> aspect and implement a finer-grained transaction. Basically what needs to happen is saving the entity and saving the file need to be separate operations so we can commit the transaction as soon as the entity is saved. And since saving the file could fail, we might have to clean up an orphaned file entity.</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">void</span> Upload(<span style="color: #2b91af">DocumentDto</span> dto)</pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">var</span> entity = <span style="color: blue">new</span> <span style="color: #2b91af">Document</span>().Merge(dto);</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; SaveEntity(entity);</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">try</span></pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; SaveFile(entity, dto.Data);</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">catch</span></pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; TryDeleteEntity(entity);</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">throw</span>;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">}</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px"><span style="color: blue">private</span> <span style="color: blue">void</span> SaveEntity(<span style="color: #2b91af">Document</span> entity)</pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; unitOfWork.Start();</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">try</span></pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; repository.Save(entity);</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; unitOfWork.Commit();</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">catch</span></pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; unitOfWork.Rollback();</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">throw</span>;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">}</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px"><span style="color: blue">private</span> <span style="color: blue">void</span> SaveFile(<span style="color: #2b91af">Document</span> entity, <span style="color: blue">byte</span>[] data)</pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">using</span> (<span style="color: blue">var</span> stream = <span style="color: blue">new</span> <span style="color: #2b91af">MemoryStream</span>(data))</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; repository.Save(entity, stream);</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">}</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px"><span style="color: blue">private</span> <span style="color: blue">void</span> TryDeleteEntity(<span style="color: #2b91af">Document</span> entity)</pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; unitOfWork.Start();</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">try</span></pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; repository.Delete(entity);</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; unitOfWork.Commit();</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">catch</span></pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; unitOfWork.Rollback();</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">}</pre>
<pre style="margin: 0px">&#160;</pre>
</div>
<p>That wasn’t too bad except that the number of lines of code exploded and we have a few private methods in our service that only deal with plumbing. It would be nice to push them into the framework. Since C# is awesome, we can use a combination of delegates and extension methods to do that.</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">void </span>Upload(<span style="color: #2b91af">DocumentDto</span> dto)</pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">var</span> entity = <span style="color: blue">new</span> <span style="color: #2b91af">Document</span>().Merge(dto);</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; unitOfWork.Execute(() =&gt; repository.Save(entity));</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">try</span></pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; repository.SaveFile(entity, dto.Data);</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">catch</span></pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; unitOfWork.TryExecute(() =&gt; repository.Delete(entity));</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">throw</span>;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">}</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">class</span> <span style="color: #2b91af">DocumentRepositoryExtensions</span></pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">void</span> SaveFile(<span style="color: blue">this</span> <span style="color: #2b91af">IDocumentRepository</span> repository, <span style="color: #2b91af">Document</span> document, <span style="color: blue">byte</span>[] data)</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">using</span> (<span style="color: blue">var</span> stream = <span style="color: blue">new</span> <span style="color: #2b91af">MemoryStream</span>(data))</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; repository.SaveFile(document, stream);</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">}</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px"><span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">class</span> <span style="color: #2b91af">UnitOfWorkExtensions</span></pre>
<pre style="margin: 0px">{</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">void</span> Execute(<span style="color: blue">this</span> <span style="color: #2b91af">IUnitOfWork</span> unitOfWork, <span style="color: #2b91af">Action</span> action)</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; unitOfWork.Start();</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">try</span></pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; action.Invoke();</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; unitOfWork.Commit();</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">catch</span></pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; unitOfWork.Rollback();</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">throw</span>;</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;</pre>
<pre style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">static</span> <span style="color: blue">void</span> TryExecute(<span style="color: blue">this</span> <span style="color: #2b91af">IUnitOfWork</span> unitOfWork, <span style="color: #2b91af">Action</span> action)</pre>
<pre style="margin: 0px">&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; unitOfWork.Start();</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">try</span></pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; action.Invoke();</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; unitOfWork.Commit();</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">catch</span></pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; unitOfWork.Rollback();</pre>
<pre style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">&#160;&#160;&#160; }</pre>
<pre style="margin: 0px">}</pre>
<pre style="margin: 0px">&#160;</pre>
</div>
<p>Now we can move the extension methods into the framework and out of our service. In a less awesome language we could define these convenience methods on the <font size="2" face="Courier New">IUnitOfWork</font> interface and implement them in an abstract base class, but <a href="http://www.agileatwork.com/inheritance-is-evil-the-story-of-the-epic-fail-of-dataannotationsmodelbinder/">inheritance is evil</a> and it’s a tradeoff we don’t have to make in C#.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.agileatwork.com/refactoring-c-style/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rafactoring: Introduce Parameter Object</title>
		<link>http://www.agileatwork.com/rafactoring-introduce-parameter-object/</link>
		<comments>http://www.agileatwork.com/rafactoring-introduce-parameter-object/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 23:59:06 +0000</pubDate>
		<dc:creator>Michael Valenty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Introduce Parameter Object]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Single Responsibility Principle]]></category>

		<guid isPermaLink="false">http://www.agileatwork.com/?p=412</guid>
		<description><![CDATA[Take a look at this function:
public UserIndexViewModel GetIndexViewModel(int pageSize, int pageNumber) {
    var model = new UserIndexViewModel();

    var allUsers = Session.CreateCriteria&#60;User&#62;();

    model.Total = CriteriaTransformer
        .TransformToRowCount(allUsers)
        .UniqueResult&#60;int&#62;();

    model.Users = allUsers
 [...]]]></description>
			<content:encoded><![CDATA[<p>Take a look at this function:</p>
<pre class="prettyprint"><code>public UserIndexViewModel GetIndexViewModel(int pageSize, int pageNumber) {
    var model = new UserIndexViewModel();

    var allUsers = Session.CreateCriteria&lt;User&gt;();

    model.Total = CriteriaTransformer
        .TransformToRowCount(allUsers)
        .UniqueResult&lt;int&gt;();

    model.Users = allUsers
        .SetFirstResult(pageSize * (pageNumber - 1) + 1)
        .SetMaxResults(pageSize)
        .AddOrder&lt;User&gt;(u =&gt; u.LastName, Order.Asc)
        .List&lt;User&gt;();

    return model;
}</code></pre>
<p>There isn’t much going on here, but it could be better. I&#8217;m looking at the arguments <font size="2" face="Courier New">pageSize</font> and <font size="2" face="Courier New">pageNumber</font>. Notice how they both start with the prefix “page”. That’s a smell. The common prefix means the arguments are related, so let’s apply the <em>Introduce Parameter Object</em> refactoring and see what happens.</p>
<pre class="prettyprint"><code>public class Page {
    private readonly int number;
    private readonly int size;

    public Page(int number, int size) {
        this.number = number;
        this.size = size;
    }

    public int Size {
        get { return size; }
    }

    public int Number {
        get { return number; }
    }

    public int FirstResult {
        get { return size * (number - 1) + 1; }
    }
}</code></pre>
<p>What did we accomplish?</p>
<ul>
<li>Creating the parameter object gave us a place to put the logic of calculating the first result of the current page. This logic would otherwise be scattered around the application and even though it’s simple, it’s error prone.</li>
<li>The logic of calculating the first result is testable.</li>
</ul>
<pre class="prettyprint"><code>[Test]
public void Should_calculate_first_result_of_current_page() {
    Assert.That(new Page(1, 20).FirstResult, Is.EqualTo(1));
    Assert.That(new Page(2, 20).FirstResult, Is.EqualTo(21));
    Assert.That(new Page(3, 20).FirstResult, Is.EqualTo(41));
}</code></pre>
<p>It’s not rocket science, but it still deserves a test. Our other option is spinning up <a href="http://www.asp.net/Downloads/archived/cassini/">Cassini</a> and clicking around to make sure we got it right. That’s just silly. A little piece of me dies each time I press F5 to run a manual test.</p>
<p>Anyway, there is this thing that happens with single responsibility classes. Because their purpose is crisp, it draws out robustness. In this case, I&#8217;m thinking about what we should we do if we get zero or a negative value for page number. I’m a fail fast kind of guy, so I’d probably go with a guard clause.</li>
</p>
<pre class="prettyprint"><code>public Page(int number, int size) {
    GuardAgainstLessThanOne(number);
    ...
}</code></pre>
<p>But you could silently correct it if that&#8217;s how you roll:</p>
<pre class="prettyprint"><code>public int Number {
    get { return Math.Max(1, number); }
}</code></pre>
<p>The point is that this is an obvious scenario to consider when looking at such a special purpose object. All too often though, bits of logic are mixed together like 32 kindergarteners sitting next to each other on a rug during a lesson. The room is one fart or tickle away from erupting into total kaos. It&#8217;s roughly half teaching and half crowd control, only without the rubber bullets and pepper spray. Not surprisingly, overcrowded classrooms don&#8217;t work that well and over-ambitous functions don&#8217;t work that well either. Each little bit of logic needs some one on one with you. Give it a chance to be all it can be by pulling it aside and getting to know it a little better. You might be surprised.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.agileatwork.com/rafactoring-introduce-parameter-object/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Domain Driven Design in the Small</title>
		<link>http://www.agileatwork.com/domain-driven-design-in-the-small/</link>
		<comments>http://www.agileatwork.com/domain-driven-design-in-the-small/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 05:20:41 +0000</pubDate>
		<dc:creator>Michael Valenty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Decorator Pattern]]></category>
		<category><![CDATA[Domain Driven Design]]></category>
		<category><![CDATA[Refactoring]]></category>

		<guid isPermaLink="false">http://www.agileatwork.com/?p=180</guid>
		<description><![CDATA[A few months ago we built a Magento extension to send orders to a product supplier via soap for payment and fulfillment along with affiliate tracking. As part of the process, a contact record was created in the affiliate&#8217;s CRM account.
&#160; 
Recently, the stake holders came up with a twist that went something like this: [...]]]></description>
			<content:encoded><![CDATA[<p>A few months ago we built a <a href="http://www.magentocommerce.com/">Magento</a> extension to send orders to a product supplier via soap for payment and fulfillment along with affiliate tracking. As part of the process, a contact record was created in the affiliate&#8217;s CRM account.</p>
<p>&#160;<img title="SubmitYourIdea" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="240" alt="SubmitYourIdea" src="http://www.agileatwork.com/wp-content/uploads/2009/08/SubmitYourIdea1.jpg" width="240" border="0" /> </p>
<p>Recently, the stake holders came up with a twist that went something like this: If the order contains a gift card, add the contact to a specific folder in the CRM application. No big deal, we had a nicely abstracted <font face="Courier New" size="2">OrderGateway</font> interface and I was already envisioning a quick addition to the existing decorator chain.</p>
<pre class="csharpcode"><span class="kwrd">class</span> OrderWithGiftCardGateway <span class="kwrd">extends</span> OrderGatewayDecorator
{
    ...

    <span class="kwrd">public function</span> createOrder(CreateOrderRequest $order)
    {
        <span class="kwrd">if</span> ($this-&gt;containsGiftCard($order))
        {
            $this-&gt;addContactToFolder($order);
        }

        <span class="kwrd">return parent</span>::createOrder($order);
    }
}</pre>
<p>I had a few minutiae questions like what happens with duplicates, etc. It took me nearly an hour to track down the right person and get real answers. During the conversation, a subtle comment was made that I almost missed.</p>
<p><em>Stake holder</em>: We should check with the product supplier to make sure the gift card sku I made up isn’t for a real product.</p>
<p><em>Me</em>: Say what?</p>
<p><em>Stake holder</em>: The gift card is not fulfilled by the product supplier, it’s fulfilled by the affiliate.</p>
<p><em>Me</em>: %$@!&amp;, I’m glad we had this conversation.</p>
<p>We talked about what it meant for the the affiliate to fulfill the product and basically the folder stuff was okay, but I recommended we remove the fake sku from the order before sending it through.</p>
<pre class="csharpcode"><span class="kwrd">public function</span> createOrder(CreateOrderRequest $order)
{
    <span class="kwrd">if</span> ($this-&gt;containsGiftCard($order))
    {
        $this-&gt;addContactToFolder($order);
        $this-&gt;removeGiftCardFromOrder($order);
    }

    <span class="kwrd">return parent</span>::createOrder($order);
}</pre>
<p>I didn’t have a buddy to pair with so I just grabbed Keith for a minute at the end of the day to walk through things. I recapped the stake holder discussion and we looked through the code. He pointed out I was missing the concept of fulfillment and that was hard earned knowledge that would be lost!</p>
<pre class="csharpcode"><span class="kwrd">public function</span> createOrder(CreateOrderRequest $order)
{
    <span class="kwrd">if</span> ($this-&gt;containsGiftCard($order))
    {
        $this-&gt;sendToAffiliateForFulfillment($order);
        $this-&gt;removeGiftCardFromOrder($order);
    }

    <span class="kwrd">return parent</span>::createOrder($order);
}</pre>
<blockquote>
<p>That was huge. – Paris Hilton</p>
</blockquote>
<p><object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/3nGAk_mo6Rw&amp;hl=en&amp;fs=1&amp;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/3nGAk_mo6Rw&amp;hl=en&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object></p>
<p>Why was that huge? Because it changed the conversation. Right away we thought of important requirements like this should be in a transaction with the order and the template email the affiliate gets should include the customer’s address, etc.</p>
<p>It tells an important story for the next guy looking at the code and it changes the role of the programmer from code monkey to business partner. Maybe you think I’m crazy, but this stuff matters to me.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.agileatwork.com/domain-driven-design-in-the-small/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Martinizing is Not Refactoring</title>
		<link>http://www.agileatwork.com/martinizing-is-not-refactoring/</link>
		<comments>http://www.agileatwork.com/martinizing-is-not-refactoring/#comments</comments>
		<pubDate>Sun, 24 May 2009 20:02:50 +0000</pubDate>
		<dc:creator>Michael Valenty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Open Closed Principle]]></category>
		<category><![CDATA[Refactoring]]></category>

		<guid isPermaLink="false">http://blog.agileatwork.com/martinizing-is-not-refactoring/</guid>
		<description><![CDATA[A friend of mine, Keith, used the term martinizing (as in Uncle Bob) for the process of cleaning code. The term has taken on a very specific meaning and it’s worth a few words.
Martinizing is similar to refactoring in that it does not change the observable behavior of the code, but the goal is different. [...]]]></description>
			<content:encoded><![CDATA[<p>A friend of mine, Keith, used the term <em>martinizing</em> (as in <a href="http://www.objectmentor.com/omTeam/martin_r.html">Uncle Bob</a>) for the process of <a href="http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882">cleaning code</a>. The term has taken on a very specific meaning and it’s worth a few words.
<p>Martinizing is similar to refactoring in that it does not change the observable behavior of the code, but the goal is different. When I refactor, I am changing the design, usually in an effort to add a new feature in an <a href="http://c2.com/cgi/wiki?OpenClosedPrinciple">open closed</a> manner.</p>
<p>When I martinize, I am telling a story. The most important story being the desired behavior, but also a story of the hard earned knowledge acquired along the way. If I spend hours distilling some business concept. I want to leave a trail for the next guy to understand that a simple property assignment or conditional statement isn’t so simple. And of course the perfect way to punctuate that message is with a well written unit test.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.agileatwork.com/martinizing-is-not-refactoring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

