<?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>toniBlog &#187; refactoring</title>
	<atom:link href="http://www.the-arm.com/tag/refactoring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.the-arm.com</link>
	<description>A weblog about Methodologies for Development</description>
	<lastBuildDate>Tue, 22 Jun 2010 08:01:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>On the anti-if campaign, the double dispatch service locator example</title>
		<link>http://www.the-arm.com/2008/02/on-the-anti-if-campaign-the-double-dispatch-service-locator-example/</link>
		<comments>http://www.the-arm.com/2008/02/on-the-anti-if-campaign-the-double-dispatch-service-locator-example/#comments</comments>
		<pubDate>Mon, 04 Feb 2008 15:46:39 +0000</pubDate>
		<dc:creator>toni</dc:creator>
				<category><![CDATA[anti-if]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://blog.java2me.org/2008/02/04/on-the-anti-if-campaign-the-double-dispatch-service-locator-example/</guid>
		<description><![CDATA[I had a good number of jokes and questions since the announcement of the anti-if campaign. Do you want to remove all these if? What&#8217;s wrong with the ifs? So, it&#8217;s better to clarify something. First of all it&#8217;s not about removing every single if, it&#8217;s about having a more flexible, maintainable and readable code. [...]]]></description>
			<content:encoded><![CDATA[<p>I had a good number of jokes and questions since the announcement of the <a href="http://brainscrum.wordpress.com/2007/11/26/the-anti-if-campaign/">anti-if campaign</a>.</p>
<p>Do you want to remove all these if? What&#8217;s wrong with the ifs?</p>
<p>So, it&#8217;s better to clarify something.</p>
<p>First of all it&#8217;s not about removing every single if, it&#8217;s about having a more flexible, maintainable and readable code.</p>
<p>A long case (or if then else) statement can be difficult to read and follow, maintenance can be painful.</p>
<p>The first category of if to get of rid of, the most immediate it&#8217;s the type based if.</p>
<p>There is clear: something is wrong, the responsibility is not where the if is but in the class itself.</p>
<p>So, an example.</p>
<p>On a project (in a Domain Driven Architecture) we had a fairly long &#8220;if type then, else if type then, etc&#8230;&#8221;</p>
<p>The if chain was on different Tasks Types in a workflow engine.</p>
<p>Let&#8217;s imagine that the workflow is an engine for a touch-less car wash system.</p>
<p>So you have, for example:</p>
<p>InsertCoinTask<br />
2-StepHeatedPreSoakTask<br />
HighPressureTouchFreeWashTask<br />
Tire&amp;WheelCleanerTask<br />
HighPressureRinseTask and so on.</p>
<p>Every task depends on the completion of a previous one and might need a run time configuration.</p>
<p>So, the code was something like this:</p>
<p><code>if (task is InsertCoinTask) </code></p>
<p><code>{ Configure(task as InsertCoinTask) } </code></p>
<p><code>else if (task is StepHeatedPreSoakTask) { </code></p>
<p><code>Configure(task as StepHeatedPreSoakTask) }</code><br />
<code>...</code><br />
Inside of a class called TaskServiceConfigurator, were for example a Configure method was implemented like:</p>
<p><code>private Configure(Tire&amp;WheelCleanerTask task)</code><br />
<code>{</code><br />
<code>task.TirePosition = touchFreeWashTask.LastCarPosition;</code><br />
<code>}</code><br />
The different configure methods inside that class (then private!) were setting previous tasks settings, values and so on.</p>
<p>So, pain points:</p>
<p>- The long if: you can easily forget to configure when adding your task to the workflow to add it there</p>
<p>- Private configuration methods: difficult to test, not only the configuration but also when testing the task itself</p>
<p>- Public setters on the tasks: since each task is configured inside the TaskServiceConfigurator all the setting came from there</p>
<p>Solution to this has been a double dispatch.</p>
<p>We added on the Task Interface a method Configure with the ITaskServiceConfigurator as a parameter, like this:</p>
<p><code>ITask </code><br />
<code>{</code><br />
<code>Configure(ITaskServiceConfigurator configurator)</code><br />
<code>}</code></p>
<p>Tire&amp;WheelCleanerTask is an ITask and the Configure implementation might look like:</p>
<p><code>Configure(ITaskServiceConfigurator configurator)</code><br />
<code>{</code><br />
<code>tirePosition = configurator.LastCarPosition;</code><br />
<code>...</code><br />
<code>}</code></p>
<p>The above example probably is too stupid, but the key point is that the LastCarPosition depends on other task (performed or not) it&#8217;s really a run-time configuration, a real time dependency between tasks.</p>
<p>I found easier to test the tasks, passing a stubbed configuration and the configuration itself had a definitely better code coverage.</p>
<p>Any better solution to this code redesign is more than welcome.</p>
<p>(Task names are taken from this page: http://www.autec-carwash.com/ <img src='http://www.the-arm.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> )</p>
]]></content:encoded>
			<wfw:commentRss>http://www.the-arm.com/2008/02/on-the-anti-if-campaign-the-double-dispatch-service-locator-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
