<?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>code-diesel &#187; refactoring</title>
	<atom:link href="http://www.codediesel.com/tag/refactoring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Refactoring 3: Replace Temp with Query</title>
		<link>http://www.codediesel.com/software/refactoring-3-replace-temp-with-query/</link>
		<comments>http://www.codediesel.com/software/refactoring-3-replace-temp-with-query/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 10:52:52 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[software]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=963</guid>
		<description><![CDATA[refactoring by replacing a temp variable with a query]]></description>
			<content:encoded><![CDATA[<p>Temporary variables are a integral part of any code. But a splattering of the same all over can make your code hard to understand or modify. <em>Replace temp with query</em> is a refactoring method where you replace temp variable expressions with methods. This method is often also required before you use the <a target="_blank" href="http://www.codediesel.com/refactoring/refactoring-2-extract-method/">Extract Method</a> refactoring.<br />
<span id="more-963"></span><br />
<strong>A simple example</strong><br />
Take for example the following code with a <em>Item</em> class that exposes a single function <em>get_price</em>. &#8216;get_price&#8217; returns the total amount of the items. I&#8217;ve hard coded the quantity and price variables to simplify the code so that we can focus on the essential element. It contains two temp variables &#8211; $base_price and  $discount_factor &#8211; they are a perfect candidate for refactoring as they also do not modify any other objects.</p>

<div class="wp_codebox"><table><tr id="p9631"><td class="code" id="p963code1"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Item <span style="color: #009900;">&#123;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_quantity</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_item_price</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">29</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get_price<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
          <span style="color: #000088;">$base_price</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_quantity <span style="color: #339933;">*</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_item_price<span style="color: #339933;">;</span>
          <span style="color: #000088;">$discount_factor</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$base_price</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #000088;">$discount_factor</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.95</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #000088;">$discount_factor</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.98</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
&nbsp;
          <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$base_price</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$discount_factor</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$item</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Item<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_price</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now all we have to do is convert the temp variables right side expression into methods and use the same in <em>get_price</em>. To avoid introducing errors during refactoring we convert one temp variable at a time and test it before proceeding further. First we convert <em>$base_price</em> temp variable to a function.</p>

<div class="wp_codebox"><table><tr id="p9632"><td class="code" id="p963code2"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Item <span style="color: #009900;">&#123;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_quantity</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_item_price</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">29</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> base_price<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_quantity <span style="color: #339933;">*</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_item_price<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get_price<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$base_price</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">base_price</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000088;">$discount_factor</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$base_price</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #000088;">$discount_factor</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.95</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #000088;">$discount_factor</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.98</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
&nbsp;
          <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$base_price</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$discount_factor</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Next we convert the <em>$discount_factor</em> variable expression to a function.</p>

<div class="wp_codebox"><table><tr id="p9633"><td class="code" id="p963code3"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Item <span style="color: #009900;">&#123;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_quantity</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_item_price</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">29</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> base_price<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_quantity <span style="color: #339933;">*</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_item_price<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> discount_factor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">base_price</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span>
               <span style="color: #000000; font-weight: bold;">return</span> <span style="color:#800080;">0.95</span><span style="color: #339933;">;</span>
          <span style="color: #000000; font-weight: bold;">else</span>
               <span style="color: #000000; font-weight: bold;">return</span> <span style="color:#800080;">0.98</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get_price<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$base_price</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">base_price</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000088;">$discount_factor</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">discount_factor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$base_price</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$discount_factor</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Finally we eliminate the temp variables in the <em>get_price</em> function.</p>

<div class="wp_codebox"><table><tr id="p9634"><td class="code" id="p963code4"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get_price<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">base_price</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">discount_factor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Using <em>Replace Temp with Query</em> is easier when the temp variables are assigned only once in a block of code, and also if they do not cause any side effects i.e the temp variables statement does not modify any object.<br />
Note that in the above example before refactoring the $base_price and  $discount_factor variables were assigned to only once each. Multiple assignments can make it a little trickier to use the current refactoring method.</p>
<p><strong>Conclusion</strong><br />
This seems a long way to go to eliminate temporary variables, but besides clean code the other main advantage of using the above method is that now the <em>base_price</em> and the <em>discount_factor</em> methods are available to other members of the class to use.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/software/refactoring-3-replace-temp-with-query/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Refactoring 2: Extract Method</title>
		<link>http://www.codediesel.com/software/refactoring-2-extract-method/</link>
		<comments>http://www.codediesel.com/software/refactoring-2-extract-method/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 04:12:04 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[software]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=168</guid>
		<description><![CDATA[The &#8216;Extract Method&#8217; is one of the most common refactorings you will ever do. It is also one you will frequently see implemented on the Refactor tool menu on various IDE&#8217;s. What this method basically does is to take a group of related code and convert it to a function with a appropriate name that [...]]]></description>
			<content:encoded><![CDATA[<p>The &#8216;Extract Method&#8217; is one of the most common refactorings you will ever do. It is also  one you will frequently see implemented on the Refactor tool menu on various IDE&#8217;s. </p>
<p>What this method basically does is to take a group of related code and convert it to a function with a appropriate name that easily explains the purpose of the code. You can also use &#8216;Extract Method&#8217; if one of your methods  is too long or requires copious amount of comments to explain its purpose.<br />
<span id="more-168"></span><br />
Take for example the following code from a recent project. The code converts a hotel room price to points depending on the currency. The code by itself doesn&#8217;t do much to explain what it does.</p>
<p><em>Note: In view of the recent constructive comments given by readers I&#8217;ve changed the code to more correctly reflect the concept of the Extract method. Its people like you that always keep me alert. Thanks guys.</em></p>

<div class="wp_codebox"><table><tr id="p1687"><td class="code" id="p168code7"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> getRoomPoints<span style="color: #009900;">&#40;</span><span style="color: #000088;">$roomPrice</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/* $forex is an array containing exchange rates */</span>
    <span style="color: #000088;">$forex</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;gbp&quot;</span><span style="color: #339933;">=&gt;</span><span style="color:#800080;">1.2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;eur&quot;</span><span style="color: #339933;">=&gt;</span><span style="color:#800080;">.75</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;usd&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$_factor</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$currency_code</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GBP&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$currency_code</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;GBP&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$_factor</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">/</span><span style="color: #000088;">$forex</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'gbp'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                     <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;USD&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$_factor</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
                     <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;EUR&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$_factor</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">/</span><span style="color: #000088;">$forex</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'eur'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                     <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$_factor</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
                     <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$roomPrice</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$_factor</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$room_points</span> <span style="color: #339933;">=</span> getRoomPoints<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_service_hotel</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'AvailableRoom'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Price'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The block of code that does the actual conversion can be extracted and converted into a function with an appropriate name as given below which now makes the intention of the code clear.</p>

<div class="wp_codebox"><table><tr id="p1688"><td class="code" id="p168code8"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> getRoomPoints<span style="color: #009900;">&#40;</span><span style="color: #000088;">$roomPrice</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$currency</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_service_hotel</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Currency'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'code'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$_factor</span> <span style="color: #339933;">=</span>  getConversionRateFactor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$currency</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$roomPrice</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$_factor</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> getConversionRateFactor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$currency_code</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$_factor</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$forex</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;gbp&quot;</span><span style="color: #339933;">=&gt;</span><span style="color:#800080;">1.2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;eur&quot;</span><span style="color: #339933;">=&gt;</span><span style="color:#800080;">.75</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;usd&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$currency_code</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;GBP&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$_factor</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">/</span><span style="color: #000088;">$forex</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'gbp'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                     <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;USD&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$_factor</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
                     <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;EUR&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$_factor</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">/</span><span style="color: #000088;">$forex</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'eur'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                     <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$_factor</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
                     <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$_factor</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000088;">$room_points</span> <span style="color: #339933;">=</span> getRoomPoints<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_service_hotel</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'AvailableRoom'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Price'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/software/refactoring-2-extract-method/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Refactoring 1: Consolidating Conditional Expressions</title>
		<link>http://www.codediesel.com/software/refactoring-1-consolidate-conditional-expression/</link>
		<comments>http://www.codediesel.com/software/refactoring-1-consolidate-conditional-expression/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 12:19:17 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[software]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=154</guid>
		<description><![CDATA[As said in the previous post, I&#8217;m kick-starting the refactoring series with the &#8216;Consolidate Conditional Expression&#8217; pattern. Bear in mind that the examples that will be given are deliberately made simple to keep the refactoring ideas in focus. So lets start with our first pattern. Consolidate Conditional Expressions Many times you see a group of [...]]]></description>
			<content:encoded><![CDATA[<p>As said in the previous post, I&#8217;m kick-starting the refactoring series with the &#8216;Consolidate Conditional Expression&#8217; pattern. Bear in mind that the examples that will be given are deliberately made simple to keep the refactoring ideas in focus. So lets start with our first pattern.</p>
<p><b>Consolidate Conditional Expressions</b><br />
Many times you see a group of conditionals where the returned values are the same. To make the code cleaner you can group the conditionals together using the &#8216;&#038;&#038;&#8217; or the &#8216;||&#8217; operators and then extract the code into a separate function. This also has the added benefit that you can reuse the extracted method in other places where the required conditional goes. Before using this pattern make sure that the grouped conditionals do not have any side-effects; i.e they do not change any variables.</p>
<p>A simple example is shown below:</p>
<p>Before refactoring:</p>

<div class="wp_codebox"><table><tr id="p15411"><td class="code" id="p154code11"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Security <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_isHttps</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_isAdmin</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_totalLoginsPerDay</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">19</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">.</span>
	<span style="color: #339933;">.</span>
	<span style="color: #339933;">.</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> can_upload_file<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_isHttps <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_isAdmin <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_totalLoginsPerDay <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$login</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Security<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$login</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">can_upload_file</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>After refactoring:</p>

<div class="wp_codebox"><table><tr id="p15412"><td class="code" id="p154code12"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Security <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_isHttps</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_isAdmin</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_totalLoginsPerDay</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">199</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">.</span>
	<span style="color: #339933;">.</span>
	<span style="color: #339933;">.</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> can_upload_file<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">check_security_status</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> check_security_status<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_isHttps <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">||</span> 
		   <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_isAdmin <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">||</span> 
		   <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_totalLoginsPerDay <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">else</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$login</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Security<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$login</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">can_upload_file</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/software/refactoring-1-consolidate-conditional-expression/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Refactoring: An introduction for PHP programmers</title>
		<link>http://www.codediesel.com/software/introduction-to-refactoring-in-php/</link>
		<comments>http://www.codediesel.com/software/introduction-to-refactoring-in-php/#comments</comments>
		<pubDate>Thu, 25 Dec 2008 12:43:45 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[software]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=144</guid>
		<description><![CDATA[In the coming year I&#8217;ll be starting a series on Refactoring. Refactoring is one of those programming ideas besides Design Patterns and Unit Testing which has made an order of magnitude improvement in my productivity. Although refactoring may not be a &#8216;Silver Bullet&#8217; for your programming woes, it is a good tool to add to [...]]]></description>
			<content:encoded><![CDATA[<p>In the coming year I&#8217;ll be starting a series on Refactoring. Refactoring is one of those programming ideas besides Design Patterns and Unit Testing which has made an order of magnitude improvement in my productivity. Although refactoring may not be a &#8216;Silver Bullet&#8217; for your programming woes, it is a good tool to add to your mental toolbox.</p>
<p>Refactoring is a process of changing code to make it more understandable and structured without changing the code functionality or introducing additional bugs. Informally it is known as &#8216;cleaning up code&#8217;. Refactoring is based on the tenet that the written code is more important to the human reader than it is to the machine. Many programmers write code with the idea that once the program is complete and working the code will not be touched again. But this seldom happens. Features need to be added, bug issues need to be fixed. This may happen tomorrow or months from now; wherein you need to really remember what a particular function did. Comments in the code may be useful, but not much if the code written is not differentiable from the half eaten spaghetti bowl sitting beside.</p>
<p>Code refactoring is not to be confused with code optimization. Code optimization is a completely different ball game, where the goal is to run the program as fast as possible by using as few CPU cycles as you can. The focus during optimization is to speed up the program, if it impedes code understandability then so be it. On the contrary refactoring may at times make your program slower, albeit most of the times by a small factor. A sample code adapted from the wonderful <a href="http://www.amazon.com/Refactoring-Improving-Existing-Addison-Wesley-Technology/dp/0201485672/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1230215398&amp;sr=8-1" target="+blank">book</a> is shown below:</p>

<div class="wp_codebox"><table><tr id="p14415"><td class="code" id="p144code15"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">// Before refactoring </span>
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$receiptDate</span> <span style="color: #339933;">&lt;</span> SUMMER_START <span style="color: #339933;">||</span> <span style="color: #000088;">$receiptDate</span> <span style="color: #339933;">&gt;</span> SUMMER_END<span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$charge</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$quantity</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$winter_rate</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$winter_service_charge</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">else</span>
    <span style="color: #000088;">$charge</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$quantity</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$summer_rate</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p14416"><td class="code" id="p144code16"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* After refactoring. The statements in the conditionals   
    have been refactored to functions. */</span> 
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>notSummer<span style="color: #009900;">&#40;</span><span style="color: #000088;">$receiptDate</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$charge</span> <span style="color: #339933;">=</span> winterCharge<span style="color: #009900;">&#40;</span><span style="color: #000088;">$quantity</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">else</span>
    <span style="color: #000088;">$charge</span> <span style="color: #339933;">=</span> summerCharge<span style="color: #009900;">&#40;</span><span style="color: #000088;">$quantity</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Many people refactor the code after the programming part is complete, many refactor as they go along. The important part is to test your code after refactoring. Unit tests are an indispensable part of this. Keep this maxim in mind &#8211; Always Test After Refactoring.</p>
<p>I&#8217;m planning to post one refactoring pattern every week or two. Although I could condense all the patterns in a lengthy post and be done with, I will resist that temptation. I want you to savour each pattern slowly so that you could remember and understand each of them like the back of your hand. There are more than fifty patterns catalogued by <a title="martin fowler" href="http://en.wikipedia.org/wiki/Martin_Fowler" target="_blank">Fowler</a> and others so it will help if we take it slowly. Most people after seeing the complete list of patterns become overwhelmed and confused and only remember a small percentage of them. Many patterns I&#8217;ll be posting will be self explanatory so I&#8217;ll do away with elaborate explanation. Refactoring is a habit and like all good programming habits it transcends the shifting landscape of technologies and languages.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/software/introduction-to-refactoring-in-php/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

