<?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>The Curmudgeoclast &#187; refactoring</title>
	<atom:link href="http://techblog.daveastels.com/tag/refactoring/feed/" rel="self" type="application/rss+xml" />
	<link>http://techblog.daveastels.com</link>
	<description>This sucks, wouldn&#039;t it be cool if...?</description>
	<lastBuildDate>Mon, 18 Jan 2010 22:30:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Turn a decision into a method</title>
		<link>http://techblog.daveastels.com/2006/08/22/turn-a-decision-into-a-method/</link>
		<comments>http://techblog.daveastels.com/2006/08/22/turn-a-decision-into-a-method/#comments</comments>
		<pubDate>Tue, 22 Aug 2006 07:19:00 +0000</pubDate>
		<dc:creator>dastels</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://techblog.daveastels.com/?p=84</guid>
		<description><![CDATA[I was just working on some code that needed to extract some optional data from a hash, using it to set an attribute on a Model object.  If it wasn&#8217;t present in the hash, a default should be used.
I started with something like:
 

if hash.has_key?("Genre")
  genre_name = hash["Genre"]
else
  genre_name = "Undefined"
end

Maybe that could [...]]]></description>
			<content:encoded><![CDATA[<p>I was just working on some code that needed to extract some optional data from a hash, using it to set an attribute on a Model object.  If it wasn&#8217;t present in the hash, a default should be used.</p>
<p>I started with something like:</p>
<p> </p>
<blockquote>
<pre>if hash.has_key?("Genre")
  genre_name = hash["Genre"]
else
  genre_name = "Undefined"
end</pre>
</blockquote>
<p>Maybe that could be simplified some:</p>
<p> </p>
<blockquote>
<pre>genre_name = hash.has_key?("Genre") ? hash["Genre"] : "Undefined"</pre>
</blockquote>
<p>Better.. but it still obscures what the intent of this method is.</p>
<p>I ended up extracting the code out into a separate method, and used a guard clause to make the decision:</p>
<p> </p>
<blockquote>
<pre>genre_name = genre_name_or_default(hash)
...
def genre_name_or_default(hash)
  return hash["Genre"] if hash.has_key("Genre")
  "Undefined"
end</pre>
</blockquote>
<p>Clean, very clear.</p>
<p>I might want to use a default for the hash, but for now that&#8217;s YAGNI, and this works nicely for my purposes.</p>
<p><strong>Addendum</strong></p>
<p>As Jens points out in his comment, I can use hash.fetch(key, default) for this.  In fact that&#8217;s what I&#8217;ve ended up doing in tis case.  However, it is a generally useful approach to extract troublesome or ugly logic into a method of it&#8217;s own.  Quite often being in a method (and not being afraid to have multiple return points) can drastically simplify and/or clarify logic.</p>
<p><strong>Addendum 2</strong></p>
<p>Simon points out that the default behaviour that the default behaviour of Hash.[] is to return nil.  Which in this case will, indeed, mean that the key wasn&#8217;t present.  So</p>
<p> </p>
<blockquote>
<pre>hash["Genre"] or "Undefined"</pre>
</blockquote>
<p>works great.</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.daveastels.com/2006/08/22/turn-a-decision-into-a-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
