<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
 
  <title>TheDailyRefactoring.com Blog</title>
  <subtitle>TheDailyRefactoring is a psuedo-news site that reports daily on refactorings that happen in the world</subtitle>
  
  <link href="http://www.thedailyrefactoring.com/" />
  <updated>2008-10-24T12:02:21-04:00</updated>
  <author>
    <name />
  </author>
  <id>http://http://www.thedailyrefactoring.com/</id>
  
  <link rel="self" href="http://feeds.feedburner.com/thedailyrefactoringcom" type="application/atom+xml" /><feedburner:browserFriendly></feedburner:browserFriendly><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><entry>
    <title>Consolidate Duplicate Delegation (new)</title>
    <link href="http://www.thedailyrefactoring.com/articles/2008/10/14/consolidate-duplicate-delegation.html" />
    <id>tag:http://www.thedailyrefactoring.com,2008-10-14:1224010873</id>
    <updated>Tue Oct 14 15:01:13 -0400 2008</updated>
    <content type="html">&lt;h2&gt;Consolidate Duplicate Delegation (rails)&lt;/h2&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Group alike delegated attributes and make them into one statement&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p class="published_by"&gt;
	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;By COREY GRUSDEN and SANDRO TURRIATE&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Published: 03:01 PM EDT&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;

&lt;/p&gt;


	&lt;p&gt;This bears some resemblance to &lt;strong&gt;Replace Inheritance With Delegation&lt;/strong&gt; and is for Rails-only.  This can also be considered a Rails-idiom.  In Rails, calls can be delegated to an object by doing the following:&lt;/p&gt;



&lt;div class="CodeRay"&gt;&lt;pre&gt;
&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;SubjectMatterExpert&lt;/span&gt;
  delegate &lt;span class="sy"&gt;:awards&lt;/span&gt;, &lt;span class="sy"&gt;:to&lt;/span&gt; =&amp;gt; &lt;span class="sy"&gt;:profile&lt;/span&gt;
  delegate &lt;span class="sy"&gt;:recommendations&lt;/span&gt;, &lt;span class="sy"&gt;:to&lt;/span&gt; =&amp;gt; &lt;span class="sy"&gt;:profile&lt;/span&gt;
  delegate &lt;span class="sy"&gt;:address&lt;/span&gt;, &lt;span class="sy"&gt;:to&lt;/span&gt; =&amp;gt; &lt;span class="sy"&gt;:address&lt;/span&gt;
  delegate &lt;span class="sy"&gt;:grades&lt;/span&gt;, &lt;span class="sy"&gt;:to&lt;/span&gt; =&amp;gt; &lt;span class="sy"&gt;:profile&lt;/span&gt;
  delegate &lt;span class="sy"&gt;:schools_attended&lt;/span&gt;, &lt;span class="sy"&gt;:to&lt;/span&gt; =&amp;gt; &lt;span class="sy"&gt;:profile&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


	&lt;p&gt;Most of the items above are delegated to the &lt;em&gt;profile&lt;/em&gt; attribute.  This is probably due to copy-and-pasting lines to &amp;#8220;save time&amp;#8221;. With Consolidate Duplicate Delegation we&amp;#8217;re aiming to clean up some of those lines to remove duplication by utilizing the language.&lt;/p&gt;


	&lt;p&gt;First group the alike delegations together:&lt;/p&gt;



&lt;div class="CodeRay"&gt;&lt;pre&gt;
&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;SubjectMatterExpert&lt;/span&gt;
  delegate &lt;span class="sy"&gt;:address&lt;/span&gt;, &lt;span class="sy"&gt;:to&lt;/span&gt; =&amp;gt; &lt;span class="sy"&gt;:address&lt;/span&gt;

  &lt;span class="c"&gt;# all these calls going to profile&lt;/span&gt;
  delegate &lt;span class="sy"&gt;:awards&lt;/span&gt;, &lt;span class="sy"&gt;:to&lt;/span&gt; =&amp;gt; &lt;span class="sy"&gt;:profile&lt;/span&gt;
  delegate &lt;span class="sy"&gt;:recommendations&lt;/span&gt;, &lt;span class="sy"&gt;:to&lt;/span&gt; =&amp;gt; &lt;span class="sy"&gt;:profile&lt;/span&gt;
  delegate &lt;span class="sy"&gt;:grades&lt;/span&gt;, &lt;span class="sy"&gt;:to&lt;/span&gt; =&amp;gt; &lt;span class="sy"&gt;:profile&lt;/span&gt;
  delegate &lt;span class="sy"&gt;:schools_attended&lt;/span&gt;, &lt;span class="sy"&gt;:to&lt;/span&gt; =&amp;gt; &lt;span class="sy"&gt;:profile&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


	&lt;p&gt;Next, put them all inline and have them all goto &lt;em&gt;profile&lt;/em&gt;:&lt;/p&gt;



&lt;div class="CodeRay"&gt;&lt;pre&gt;
&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;SubjectMatterExpert&lt;/span&gt;
  delegate &lt;span class="sy"&gt;:address&lt;/span&gt;, &lt;span class="sy"&gt;:to&lt;/span&gt; =&amp;gt; &lt;span class="sy"&gt;:address&lt;/span&gt;

  delegate &lt;span class="sy"&gt;:awards&lt;/span&gt;, 
           &lt;span class="sy"&gt;:recommendations&lt;/span&gt;, 
           &lt;span class="sy"&gt;:grades&lt;/span&gt;, 
           &lt;span class="sy"&gt;:schools_attended&lt;/span&gt;, &lt;span class="sy"&gt;:to&lt;/span&gt; =&amp;gt; &lt;span class="sy"&gt;:profile&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;</content>
  </entry>
  
  <entry>
    <title>Pull Up Method</title>
    <link href="http://www.thedailyrefactoring.com/articles/2008/10/13/ruby-pull-up-method.html" />
    <id>tag:http://www.thedailyrefactoring.com,2008-10-13:1223927129</id>
    <updated>Mon Oct 13 15:45:29 -0400 2008</updated>
    <content type="html">&lt;h2&gt;Pull-Up Method (ruby)&lt;/h2&gt;


	&lt;p class="published_by"&gt;
	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;By COREY GRUSDEN and SANDRO TURRIATE&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Published: 03:45 PM EDT&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;

&lt;/p&gt;


&lt;div class="container"&gt;
  &lt;div class="span-2"&gt;
  &lt;img src="/images/2008_10_13_1.jpg" alt="" /&gt;
  &lt;/div&gt;
  &lt;div class="span-4"&gt;
With the concept of &lt;a href="http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model"&gt;Skinny Controller, Fat Model&lt;/a&gt; a lot can be missed when moving data around into your models.  A really good indicator of logic that can be put into the parent class is if you have a method that is changing &lt;em&gt;self&lt;/em&gt; variables.
  &lt;/div&gt;
&lt;/div&gt;


&lt;div class="CodeRay"&gt;&lt;pre&gt;
&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;SubjectMatterExpert&lt;/span&gt; &amp;lt; &lt;span class="co"&gt;User&lt;/span&gt;
 include &lt;span class="co"&gt;HashCodeCreatorModule&lt;/span&gt;
 &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;make_activation_code&lt;/span&gt;
   &lt;span class="pc"&gt;self&lt;/span&gt;.deleted_at = &lt;span class="pc"&gt;nil&lt;/span&gt;
   &lt;span class="pc"&gt;self&lt;/span&gt;.activation_code ||= make_hash_code
 &lt;span class="r"&gt;end&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;

&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;Administrator&lt;/span&gt; &amp;lt; &lt;span class="co"&gt;User&lt;/span&gt;
 include &lt;span class="co"&gt;HashCodeCreatorModule&lt;/span&gt;
 &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;make_activation_code&lt;/span&gt;
   &lt;span class="pc"&gt;self&lt;/span&gt;.deleted_at = &lt;span class="pc"&gt;nil&lt;/span&gt;
   &lt;span class="pc"&gt;self&lt;/span&gt;.activation_code ||= make_hash_code
 &lt;span class="r"&gt;end&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


	&lt;p&gt;We were able to reduce the code in the 2 classes &lt;em&gt;SubjectMatterExpert&lt;/em&gt; and &lt;em&gt;Administrator&lt;/em&gt; by using the &lt;strong&gt;Pull-Up Method&lt;/strong&gt; to move the methods into the parent class &lt;em&gt;User&lt;/em&gt;.&lt;/p&gt;



&lt;div class="CodeRay"&gt;&lt;pre&gt;
&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;SubjectMatterExpert&lt;/span&gt; &amp;lt; &lt;span class="co"&gt;User&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;

&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;Administrator&lt;/span&gt; &amp;lt; &lt;span class="co"&gt;User&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;

&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;User&lt;/span&gt;
 include &lt;span class="co"&gt;HashCodeCreatorModule&lt;/span&gt;
 &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;make_activation_code&lt;/span&gt;
   &lt;span class="pc"&gt;self&lt;/span&gt;.deleted_at = &lt;span class="pc"&gt;nil&lt;/span&gt;
   &lt;span class="pc"&gt;self&lt;/span&gt;.activation_code ||= make_hash_code
 &lt;span class="r"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


	&lt;p&gt;Now our code still works in our tests:&lt;/p&gt;



&lt;div class="CodeRay"&gt;&lt;pre&gt;
it &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;should create an activation code&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;do&lt;/span&gt;
  admin = Factory(&lt;span class="sy"&gt;:administrator&lt;/span&gt;)
  admin.make_activation_code
  admin.activation_code.should_not be_empty
&lt;span class="r"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


	&lt;p&gt;Warning: Luckily this method was very simple to Pull-Up. We ran across another method that we wanted to Pull-Up, but it had calls to a State-Machine event. So pay attention as to what is going on in the method.&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <title>Consolidate Conditional Expression</title>
    <link href="http://www.thedailyrefactoring.com/articles/2008/10/10/consolidate-conditional-expression-simple.html" />
    <id>tag:http://www.thedailyrefactoring.com,2008-10-10:1223673252</id>
    <updated>Fri Oct 10 17:14:12 -0400 2008</updated>
    <content type="html">&lt;h2&gt;Consolidate Conditional Expression (Simple)&lt;/h2&gt;


	&lt;p class="published_by"&gt;
	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;By COREY GRUSDEN and SANDRO TURRIATE&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Published: 05:14 PM EDT&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;

&lt;/p&gt;


	&lt;p&gt;While trying to figure out the logic that was going on in a very important method, we decided to make an attempt on refactoring some of the expressions into
something we could actually grok.&lt;/p&gt;


	&lt;p&gt;The following method was the culprit (yes, I realize the names are bad, but obfuscation is king):&lt;/p&gt;



&lt;div class="CodeRay"&gt;&lt;pre&gt;
&lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;password_required?&lt;/span&gt;
   &lt;span class="r"&gt;if&lt;/span&gt; !new_special_sme? &amp;amp;&amp;amp; (from_intl_expert_profile? || recently_forgot_password? || ((new_record? || password_reset_code.blank?) &lt;span class="r"&gt;and&lt;/span&gt; special_sme?))
     &lt;span class="r"&gt;return&lt;/span&gt; &lt;span class="pc"&gt;false&lt;/span&gt;
   &lt;span class="r"&gt;elsif&lt;/span&gt; !password_reset_code.blank? &lt;span class="r"&gt;and&lt;/span&gt; ((special_sme? &lt;span class="r"&gt;and&lt;/span&gt; terms_accepted?) &lt;span class="r"&gt;or&lt;/span&gt; new_special_sme?)
     &lt;span class="r"&gt;return&lt;/span&gt; &lt;span class="pc"&gt;true&lt;/span&gt;
   &lt;span class="r"&gt;else&lt;/span&gt;
     &lt;span class="r"&gt;return&lt;/span&gt; !special_sme? &amp;amp;&amp;amp; user_password_required?
   &lt;span class="r"&gt;end&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


	&lt;p&gt;The line that we&amp;#8217;re concerned about currently is the last &lt;em&gt;return&lt;/em&gt;:&lt;/p&gt;



&lt;div class="CodeRay"&gt;&lt;pre&gt;
   &lt;span class="r"&gt;else&lt;/span&gt;
     &lt;span class="r"&gt;return&lt;/span&gt; !special_sme? &amp;amp;&amp;amp; user_password_required?
   &lt;span class="r"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


	&lt;p&gt;We extract it out into it&amp;#8217;s own method:&lt;/p&gt;



&lt;div class="CodeRay"&gt;&lt;pre&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;not_special_sme_and_requires_password&lt;/span&gt;
    !special_sme? &amp;amp;&amp;amp; user_password_required?
  &lt;span class="r"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


	&lt;p&gt;And replace the conditional statement with the method we just made:&lt;/p&gt;



&lt;div class="CodeRay"&gt;&lt;pre&gt;
&lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;password_required?&lt;/span&gt;
   &lt;span class="r"&gt;if&lt;/span&gt; !new_special_sme? &amp;amp;&amp;amp; (from_intl_expert_profile? || recently_forgot_password? || ((new_record? || password_reset_code.blank?) &lt;span class="r"&gt;and&lt;/span&gt; special_sme?))
     &lt;span class="r"&gt;return&lt;/span&gt; &lt;span class="pc"&gt;false&lt;/span&gt;
   &lt;span class="r"&gt;elsif&lt;/span&gt; !password_reset_code.blank? &lt;span class="r"&gt;and&lt;/span&gt; ((special_sme? &lt;span class="r"&gt;and&lt;/span&gt; terms_accepted?) &lt;span class="r"&gt;or&lt;/span&gt; new_special_sme?)
     &lt;span class="r"&gt;return&lt;/span&gt; &lt;span class="pc"&gt;true&lt;/span&gt;
   &lt;span class="r"&gt;else&lt;/span&gt;
     &lt;span class="r"&gt;return&lt;/span&gt; not_special_sme_and_requires_password
   &lt;span class="r"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


	&lt;p&gt;With that small refactoring, we were able to replace quite a few statements in other areas of our code to clean it up and put the logic in one spot.&lt;/p&gt;</content>
  </entry>
  
  <entry>
    <title>Replace Loop with Closure Collection Method</title>
    <link href="http://www.thedailyrefactoring.com/articles/2008/10/07/clean-up-flash-messages-replace-loop-with-closure.html" />
    <id>tag:http://www.thedailyrefactoring.com,2008-10-07:1223404201</id>
    <updated>Tue Oct 07 14:30:01 -0400 2008</updated>
    <content type="html">&lt;h2&gt;Cleaning up Flash Messages in our Views&lt;/h2&gt;


	&lt;blockquote&gt;
		&lt;p&gt;From Ruby Refactoring: Replacing a Loop with Closure Collection Method&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p class="published_by"&gt;
	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;By COREY GRUSDEN, SANDRO TURRIATE, and ZACH INGLIS&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Published: 02:30 PM EDT&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;

&lt;/p&gt;


	&lt;p&gt;We had conditional code that was appearing in multiple views and getting rid of it was a high priority to us since duplicated code is known as a &lt;a href="http://en.wikipedia.org/wiki/Code_smell"&gt;Code Smell&lt;/a&gt; and does more harm than good.  So we decided to move the code into a &lt;em&gt;Module&lt;/em&gt; and call the method from there.&lt;/p&gt;



&lt;div class="CodeRay"&gt;&lt;pre&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;render_flashes&lt;/span&gt;
    flashes = &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
    [&lt;span class="sy"&gt;:notice&lt;/span&gt;, &lt;span class="sy"&gt;:error&lt;/span&gt;].each &lt;span class="r"&gt;do&lt;/span&gt; |type|
      &lt;span class="r"&gt;unless&lt;/span&gt; flash[type].nil?
        flashes += content_tag(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, flash[type], &lt;span class="sy"&gt;:id&lt;/span&gt; =&amp;gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;flash_&lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;type&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="sy"&gt;:class&lt;/span&gt; =&amp;gt; type)
      &lt;span class="r"&gt;end&lt;/span&gt;
    &lt;span class="r"&gt;end&lt;/span&gt;
    flashes
  &lt;span class="r"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


	&lt;p&gt;We then realized that we could use &lt;strong&gt;Replacing a Loop with Closure Collection Method&lt;/strong&gt; to get rid of the local variables as well as utilize the power of Ruby&amp;#8217;s enumeration closures.&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;The &lt;em&gt;.collect&lt;/em&gt; method will build an array from what&amp;#8217;s returned from the block instead of concatenating a string from &lt;em&gt;content_tag&lt;/em&gt; &lt;/li&gt;
		&lt;li&gt;Removing the &lt;em&gt;flash[type].nil?&lt;/em&gt; is possible since enumerating values in the hash will not return anything if values are &lt;em&gt;nil&lt;/em&gt;&lt;/li&gt;
		&lt;li&gt;When the code is output to the view, it will be nicely formatted underneath since we can call &lt;em&gt;.join&lt;/em&gt; on the array that is created from &lt;em&gt;.collect&lt;/em&gt;&lt;/li&gt;
	&lt;/ol&gt;



&lt;div class="CodeRay"&gt;&lt;pre&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;render_flashes&lt;/span&gt;
    flash.collect &lt;span class="r"&gt;do&lt;/span&gt; |key, message|
      content_tag &lt;span class="sy"&gt;:div&lt;/span&gt;, message, &lt;span class="sy"&gt;:id&lt;/span&gt; =&amp;gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;flash_&lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;key&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="sy"&gt;:class&lt;/span&gt; =&amp;gt; key
    &lt;span class="r"&gt;end&lt;/span&gt;.join(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;\n&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;)
  &lt;span class="r"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;</content>
  </entry>
  
  <entry>
    <title>Extract Method</title>
    <link href="http://www.thedailyrefactoring.com/articles/2008/10/07/extract-method-render-flashes.html" />
    <id>tag:http://www.thedailyrefactoring.com,2008-10-07:1223389722</id>
    <updated>Tue Oct 07 10:28:42 -0400 2008</updated>
    <content type="html">&lt;h2&gt;Removing the duplication of Flash Messages&lt;/h2&gt;


	&lt;blockquote&gt;
		&lt;p&gt;From Refactoring: Extract Method&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p class="published_by"&gt;
By COREY GRUSDEN, SANDRO TURRIATE
&lt;br/&gt;
Published: 10:17 AM EDT&lt;/p&gt;


	&lt;p&gt;&lt;img src="/images/2008_10_7_1.jpg" alt="" /&gt;
We had the following conditional code that was appearing in multiple views, so we wanted to cut down on the duplication and be able to modify the code in one spot.&lt;/p&gt;


	&lt;p&gt;We used Extract Method on the following code:&lt;/p&gt;


&lt;div class="span-12 prepend-2"&gt;

&lt;div class="CodeRay"&gt;&lt;pre&gt;
  - &lt;span class="r"&gt;if&lt;/span&gt; flash[&lt;span class="sy"&gt;:notice&lt;/span&gt;]
    .notice
      = flash[&lt;span class="sy"&gt;:notice&lt;/span&gt;]
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;

	&lt;p&gt;and moved it into a module:&lt;/p&gt;



&lt;div class="CodeRay"&gt;&lt;pre&gt;
  &lt;span class="r"&gt;module&lt;/span&gt; &lt;span class="cl"&gt;ViewHelper&lt;/span&gt;
    &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;render_flashes&lt;/span&gt;
      flashes = &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
      [&lt;span class="sy"&gt;:notice&lt;/span&gt;, &lt;span class="sy"&gt;:error&lt;/span&gt;].each &lt;span class="r"&gt;do&lt;/span&gt; |type|
        &lt;span class="r"&gt;unless&lt;/span&gt; flash[type].nil?
          flashes += content_tag(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, flash[type], &lt;span class="sy"&gt;:id&lt;/span&gt; =&amp;gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;flash_&lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;type&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="sy"&gt;:class&lt;/span&gt; =&amp;gt; type)
        &lt;span class="r"&gt;end&lt;/span&gt;
      &lt;span class="r"&gt;end&lt;/span&gt;
      flashes
    &lt;span class="r"&gt;end&lt;/span&gt;
  &lt;span class="r"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


	&lt;p&gt;Looks like a lot of extra functionality that we added when we used extract method, right? It is.  But now we can replace the code in our views after we mixin our new module into the class that is rendering our view and make changes in one spot instead of across multiple views.&lt;/p&gt;



&lt;div class="CodeRay"&gt;&lt;pre&gt;
  = render_flashes
&lt;/pre&gt;&lt;/div&gt;</content>
  </entry>
  
</feed>
