<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Signs your PHP needs refactoring</title>
	<atom:link href="http://soledadpenades.com/2007/06/05/signs-your-php-needs-refactoring/feed/" rel="self" type="application/rss+xml" />
	<link>http://soledadpenades.com/2007/06/05/signs-your-php-needs-refactoring/</link>
	<description>repeat 4[fd 100 rt 90]</description>
	<lastBuildDate>Mon, 30 Aug 2010 18:41:27 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Dave Doolin</title>
		<link>http://soledadpenades.com/2007/06/05/signs-your-php-needs-refactoring/#comment-51902</link>
		<dc:creator>Dave Doolin</dc:creator>
		<pubDate>Wed, 03 Jun 2009 18:57:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.soledadpenades.com/2007/06/05/signs-your-php-needs-refactoring/#comment-51902</guid>
		<description>I&#039;m working through reverse engineering my first php application right now.  The code is... interesting...

I was prepared to accept something like &quot;php is different&quot; but after really digging in, no, php is just the same as every other programming language.  

Part of it is php makes it easy to write a lot &quot;working&quot; code without knowing how anything actually works.  Effective refactoring means you have to understand stuff like &quot;scope.&quot;  Boring.</description>
		<content:encoded><![CDATA[<p>I&#8217;m working through reverse engineering my first php application right now.  The code is&#8230; interesting&#8230;</p>
<p>I was prepared to accept something like &#8220;php is different&#8221; but after really digging in, no, php is just the same as every other programming language.  </p>
<p>Part of it is php makes it easy to write a lot &#8220;working&#8221; code without knowing how anything actually works.  Effective refactoring means you have to understand stuff like &#8220;scope.&#8221;  Boring.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sole</title>
		<link>http://soledadpenades.com/2007/06/05/signs-your-php-needs-refactoring/#comment-49056</link>
		<dc:creator>sole</dc:creator>
		<pubDate>Wed, 05 Mar 2008 19:34:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.soledadpenades.com/2007/06/05/signs-your-php-needs-refactoring/#comment-49056</guid>
		<description>O-M-G

I&#039;m surprised you&#039;ve survived - you&#039;re a tough one!!!!!

The people which had written that ... shouldn&#039;t have set their fingers in the keyboard ever!

this makes for a true daily WTF, you should send it to them :-)</description>
		<content:encoded><![CDATA[<p>O-M-G</p>
<p>I&#8217;m surprised you&#8217;ve survived &#8211; you&#8217;re a tough one!!!!!</p>
<p>The people which had written that &#8230; shouldn&#8217;t have set their fingers in the keyboard ever!</p>
<p>this makes for a true daily WTF, you should send it to them :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: RE-coder</title>
		<link>http://soledadpenades.com/2007/06/05/signs-your-php-needs-refactoring/#comment-49055</link>
		<dc:creator>RE-coder</dc:creator>
		<pubDate>Wed, 05 Mar 2008 19:03:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.soledadpenades.com/2007/06/05/signs-your-php-needs-refactoring/#comment-49055</guid>
		<description>You missed a really obvious one, probably because it&#039;s SO obvious - but it&#039;s been a major headache in the million-line application I&#039;m refactoring now. (OK, that was a &lt;i&gt;little&lt;/i&gt; bit of an exaggeration; I didn&#039;t actually start with a million lines, only 931,327. Close enough.) 

Far, FAR worse than 300-line switch statements are 1000+ line nested if/if/elseif/else blocks. Try this one on for size (a real example taken from production code, just greatly simplified for clarity):

&lt;code lang=&quot;php&quot;&gt;
if ($a) {
     if($b) { } // 200+ lines
     else if ($c) { } // 100+ lines
     else doDefault();
} else if ($i) {
     if($j) // 300+ lines
     else if($c) { } // 100+ lines
     else if ($k) { 
          if($l) { } // 250+ lines
          else if($m) // 75 lines
     } 
} else if ($w) {
     if($x) // { } 200+ lines
     else if ($y) { } // 150 lines
}
else { 
    doDefault();
}
if($nothing_has_been_done_yet) { doDefault(); }
&lt;/code&gt;

I tried to make the major problem there as obvious as possible. Condition
$c actually equated to &#039;user does not have permission to view this page&#039;.
However, there were certain conditions under which forbidden users were
inadvertently allowed in; for instance, when ($i &amp;&amp; $j) equated to true,
or when ($w &amp;&amp; !$x &amp;&amp; !$y). This was a definite bug, one that had never
before been identified. And it would&#039;ve been impossible find and fix
within the 1000+ lines of code without refactoring it.

My rewrite of that code ended up something like the following:

&lt;code lang=&quot;php&quot;&gt;
switch($foo) {
    case &#039;c&#039;: displayAccessForbiddenPage(); exit;
    case &#039;b&#039;: doB(); break;
    case &#039;j&#039;: doJ(); break;
    case &#039;k&#039;: doK($l,$m); break;
    case &#039;x&#039;: doX(); break;
    case &#039;y&#039;: doY(); break;
    default: doDefault();
}
&lt;/code&gt;

I think it&#039;s inarguable that the refactored code is far easier to read, understand, debug and modify than the original version.

Please pass the Excedrin, sole. Thank God I&#039;m almost done with this beastie. I just fervently wish that the people who&#039;d written this application had read your article before ever setting fingers to keyboard.</description>
		<content:encoded><![CDATA[<p>You missed a really obvious one, probably because it&#8217;s SO obvious &#8211; but it&#8217;s been a major headache in the million-line application I&#8217;m refactoring now. (OK, that was a <i>little</i> bit of an exaggeration; I didn&#8217;t actually start with a million lines, only 931,327. Close enough.) </p>
<p>Far, FAR worse than 300-line switch statements are 1000+ line nested if/if/elseif/else blocks. Try this one on for size (a real example taken from production code, just greatly simplified for clarity):</p>
<div class="syhi_block"><code><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// 200+ lines</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// 100+ lines</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">else</span> doDefault<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// 300+ lines</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// 100+ lines</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$l</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// 250+ lines</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$m</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// 75 lines</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&#125;</span> <br />
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$w</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// { } 200+ lines</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// 150 lines</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; doDefault<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nothing_has_been_done_yet</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> doDefault<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></code></div>
<p>I tried to make the major problem there as obvious as possible. Condition<br />
$c actually equated to &#8216;user does not have permission to view this page&#8217;.<br />
However, there were certain conditions under which forbidden users were<br />
inadvertently allowed in; for instance, when ($i &amp;&amp; $j) equated to true,<br />
or when ($w &amp;&amp; !$x &amp;&amp; !$y). This was a definite bug, one that had never<br />
before been identified. And it would&#8217;ve been impossible find and fix<br />
within the 1000+ lines of code without refactoring it.</p>
<p>My rewrite of that code ended up something like the following:</p>
<div class="syhi_block"><code><span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$foo</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'c'</span><span style="color: #339933;">:</span> displayAccessForbiddenPage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'b'</span><span style="color: #339933;">:</span> doB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'j'</span><span style="color: #339933;">:</span> doJ<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'k'</span><span style="color: #339933;">:</span> doK<span style="color: #009900;">&#40;</span><span style="color: #000088;">$l</span><span style="color: #339933;">,</span><span style="color: #000088;">$m</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'x'</span><span style="color: #339933;">:</span> doX<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'y'</span><span style="color: #339933;">:</span> doY<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span> doDefault<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></code></div>
<p>I think it&#8217;s inarguable that the refactored code is far easier to read, understand, debug and modify than the original version.</p>
<p>Please pass the Excedrin, sole. Thank God I&#8217;m almost done with this beastie. I just fervently wish that the people who&#8217;d written this application had read your article before ever setting fingers to keyboard.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ve</title>
		<link>http://soledadpenades.com/2007/06/05/signs-your-php-needs-refactoring/#comment-45530</link>
		<dc:creator>Ve</dc:creator>
		<pubDate>Sat, 13 Oct 2007 10:54:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.soledadpenades.com/2007/06/05/signs-your-php-needs-refactoring/#comment-45530</guid>
		<description>Great work! In particular, I&#039;m glad you mentioned the &quot;not-so-obvious&quot; and &quot;not-so-widely-discussed&quot; (hence, easy to forget) ones - &quot;Everything&#039;s an array&quot; and especially &quot;You have to modify too many files&quot;. Also, I second the advice on having E_NOTICE turned on while developing.</description>
		<content:encoded><![CDATA[<p>Great work! In particular, I&#8217;m glad you mentioned the &#8220;not-so-obvious&#8221; and &#8220;not-so-widely-discussed&#8221; (hence, easy to forget) ones &#8211; &#8220;Everything&#8217;s an array&#8221; and especially &#8220;You have to modify too many files&#8221;. Also, I second the advice on having E_NOTICE turned on while developing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jan Pieter Kunst</title>
		<link>http://soledadpenades.com/2007/06/05/signs-your-php-needs-refactoring/#comment-45503</link>
		<dc:creator>Jan Pieter Kunst</dc:creator>
		<pubDate>Fri, 12 Oct 2007 18:52:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.soledadpenades.com/2007/06/05/signs-your-php-needs-refactoring/#comment-45503</guid>
		<description>Cups wrote: &quot;Still, one nagging question that I haven&#039;t chased down to my full satisfaction - why is a Constant not a Global then?&quot;

A constant is global, but it&#039;s also immutable. So there is no need to worry about &quot;keeping track of who&#039;s modifying what&quot;.</description>
		<content:encoded><![CDATA[<p>Cups wrote: &#8220;Still, one nagging question that I haven&#8217;t chased down to my full satisfaction &#8211; why is a Constant not a Global then?&#8221;</p>
<p>A constant is global, but it&#8217;s also immutable. So there is no need to worry about &#8220;keeping track of who&#8217;s modifying what&#8221;.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
