<?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>Giordano Scalzo&#039;s Personal Blog &#187; refactor</title>
	<atom:link href="http://giordano.scalzo.biz/tag/refactor/feed/" rel="self" type="application/rss+xml" />
	<link>http://giordano.scalzo.biz</link>
	<description>Just another useless weblog</description>
	<lastBuildDate>Fri, 09 Jul 2010 09:41:02 +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>Extreme Refactoring or Is Guard clause Considered harmful?</title>
		<link>http://giordano.scalzo.biz/2009/09/18/extreme-refactoring-or-is-guard-clause-considered-harmful/</link>
		<comments>http://giordano.scalzo.biz/2009/09/18/extreme-refactoring-or-is-guard-clause-considered-harmful/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 14:56:34 +0000</pubDate>
		<dc:creator>giordano scalzo</dc:creator>
				<category><![CDATA[agile]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[refactor]]></category>

		<guid isPermaLink="false">http://giordano.scalzo.biz/?p=112</guid>
		<description><![CDATA[During the JDave try, the focus was on Bdd side of the exercise, but, nonetheless, the StringTemplater&#8217;s code was yelling &#8220;Refactor me! Refactor me!&#8221; at me  .
Actually, that I like least, it&#8217;s the Guard Clause, I used to split a marker in key-value form:

private String name(String[] pair) {
		if (pair.length != 2)
			return "";
		return "$" + [...]]]></description>
			<content:encoded><![CDATA[<p>During the <a href="http://giordano.scalzo.biz/2009/09/17/using-jdave-a-quick-introduction-to-specs-framework/">JDave try</a>, the focus was on Bdd side of the exercise, but, nonetheless, the StringTemplater&#8217;s code was yelling &#8220;<em>Refactor me! Refactor me!</em>&#8221; at me <img src='http://giordano.scalzo.biz/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p>Actually, that I like least, it&#8217;s the <a href="http://www.c2.com/cgi/wiki?GuardClause">Guard Clause</a>, I used to split a marker in key-value form:</p>
<pre class="brush: java">
private String name(String[] pair) {
		if (pair.length != 2)
			return "";
		return "$" + pair[0].trim();
	}
</pre>
<p>Usually I like that pattern, it&#8217;s a well know <a href="http://www.refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html"> way to avoid nested conditional</a>, but <a href="http://www.antiifcampaign.com/">Anti-If Campaign</a>, promoted by <a href="http://cirillosscrapbook.wordpress.com/">Francesco Cirillo</a> made me think.</p>
<p>The first try didn&#8217;t satisfied me:</p>
<pre class="brush: java">
private String name(String[] pair) {
		return  (pair.length != 2) ? "" : "$" + pair[0].trim();
	}
</pre>
<p>Indeed, I believe it&#8217;s more cryptic and ugly than the original one.</p>
<p>Looking the code more carefully, I saw another thing: I was violating the principle &#8220;<em>Ask, don&#8217;t tell</em>&#8220;, handling some data that could be tied together.</p>
<p>Creating the new class, I thought another thing:<br />
&#8220;<em>Why am I using the &#8216;<strong>IF</strong>&#8216;</em>?&#8221;<br />
I did for avoid a runtime exception&#8230; but is an exception evil?<br />
It isn&#8217;t if we can handle it!</p>
<p>So at the end this is my final product:</p>
<pre class="brush: java">
package biz.scalzo.kata.stringtemplater.jdave;

import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class StringTemplater {

	public String replace(String stringToReplace) {
		return replace(stringToReplace, "");
	}

	public String replace(String stringToReplace, String markers) {
		return replaceEmptyMarkers(replace(stringToReplace, initialIterator(markers)));
	}

	private String replaceEmptyMarkers(String stringToReplace) {
		return stringToReplace.replaceAll("\\$\\w+", "");
	}

	private Iterator<String> initialIterator(String markers) {
		return Arrays.asList(markers.split(",")).iterator();
	}

	private String replace(String stringToReplace, Iterator<String> markers) {
		try {
			return replaceMarker(stringToReplace, markers);
		} catch (NoSuchElementException e) {
			return stringToReplace;
		}
	}

	private String replaceMarker(String stringToReplace,
			Iterator<String> markers) {
		Pair pair = new Pair(markers.next());
		String newString = stringToReplace.replace(pair.name, pair.value);
		return replace(newString, markers);
	}

	private class Pair {
		private String name;
		private String value;

		private Pair(String nameAndValue) {
			init(nameAndValue.split(":"));
		}

		private void init(String[] nameAndValue) {
			try {
				name = "$" + nameAndValue[0].trim();
				value = nameAndValue[1].trim();
			} catch (IndexOutOfBoundsException e) {
				name = "";
				value = "";
			}
		}
	}
}
</pre>
<p>I think a I could refactor more, but I was enough satisfied: I reached my goal to avoid <strong>IF</strong> and I&#8217;ve start to think the exception in different way.</p>

<!-- start wp-tags-to-technorati 1.01 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/eclipse' rel='tag' target='_self'>eclipse</a>, <a class='technorati-link' href='http://technorati.com/tag/java' rel='tag' target='_self'>java</a>, <a class='technorati-link' href='http://technorati.com/tag/refactor' rel='tag' target='_self'>refactor</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://giordano.scalzo.biz/2009/09/18/extreme-refactoring-or-is-guard-clause-considered-harmful/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
