<?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>Professional PHP &#187; refactoring</title>
	<atom:link href="http://www.procata.com/blog/archives/tag/refactoring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.procata.com/blog</link>
	<description>PHP Programming, Web Development, PHP Advocacy and PHP Best Practices.</description>
	<lastBuildDate>Fri, 10 Dec 2010 17:23:30 +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>Let Your Properties be Properties</title>
		<link>http://www.procata.com/blog/archives/2007/05/08/let-your-properties-be-properties/</link>
		<comments>http://www.procata.com/blog/archives/2007/05/08/let-your-properties-be-properties/#comments</comments>
		<pubDate>Tue, 08 May 2007 17:41:51 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Design]]></category>
		<category><![CDATA[php-patterns]]></category>
		<category><![CDATA[properties]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://www.procata.com/blog/archives/2007/05/08/let-your-properties-be-properties/</guid>
		<description><![CDATA[There is a coding pattern that I see (and have used) in PHP code that defines generic methods on a class for setting and getting properties.
&#160;
function set&#40;$name, $value&#41;;
function get&#40;$name&#41;;
&#160;Google code search for examples
Some times there are some ancillary methods to deal with unsetting, checking for existence, setting via an array, or dealing with references in [...]]]></description>
			<content:encoded><![CDATA[<p>There is a coding pattern that I see (and have used) in PHP code that defines generic methods on a class for setting and getting properties.<br />
<pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> set<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$name</span>, <span style="color: #0000ff;">$value</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">function</span> get<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$name</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre><a href="http://www.google.com/codesearch?hl=en&#038;lr=&#038;q=lang%3Aphp+function%5Cs%2Bset%5Cw*%5Cs*%5C%28%5Cs*%5C%24name%5Cs*%2C%5Cs*%26%3F%5Cs*%5C%24value%5Cs*%5C%29&#038;btnG=Search">Google code search for examples</a></p>
<p>Some times there are some ancillary methods to deal with unsetting, checking for existence, setting via an array, or dealing with references in PHP 4.  They can really clutter up the definition of a class.  That&#8217;s not good.  All this code is fairly standard, too, but it gets duplicated on every class that does this.  That&#8217;s not good, either.</p>
<p>Oh, I&#8217;ll solve this problem by making a base class, some may say.  Wrong.  This a very feeble reason to spend your one shot at inheritance.  Trust me, I know, I&#8217;ve done it.</p>
<p>I think the idea is to make the class extensible.  But PHP is really ok with just setting new properties on a class.<br />
<pre class="php">&nbsp;
<span style="color: #0000ff;">$obj</span>-&gt;<span style="color: #006600;">foo</span> = <span style="color: #ff0000;">'bar'</span>;
&nbsp;</pre><br />
So why not just do this?</p>
<p>Another variation of this pattern is to use setXXX($name, $value) or setYYY($name, $value) methods.  This happens alot with &#8220;options&#8221; or &#8220;vars&#8221; or &#8220;properties.&#8221;  It also happens on request wrapper classes.  To me this looks like there is an object here just begging to get out for each XXX and YYY.<br />
<pre class="php">&nbsp;
<span style="color: #0000ff;">$obj</span>-&gt;<span style="color: #006600;">xxx</span>-&gt;<span style="color: #006600;">prop</span> = <span style="color: #ff0000;">'foo'</span>;
<span style="color: #0000ff;">$obj</span>-&gt;<span style="color: #006600;">yyy</span>-&gt;<span style="color: #006600;">prop</span> = <span style="color: #ff0000;">'foo'</span>;
<span style="color: #0000ff;">$obj</span>-&gt;<span style="color: #006600;">zzz</span>-&gt;<span style="color: #006600;">prop</span> = <span style="color: #ff0000;">'foo'</span>;
&nbsp;</pre><br />
This eliminates a slew of property manipulation methods and leaves the original class free to implement its true purpose.  Methods of the form getXXX($name) and setXXX($name, $value) should be the solution of last resort.  </p>
<p>Since I&#8217;ve started eliminating these in my own code in favor of direct properties, intermediate objects or __set and __get, I feel I&#8217;ve seen nothing but positive results.  Try it.  You may like it, too.  Let your properties be properties.</p>
<p><b>UPDATE:</b><br />
From reading the comments, I think there was some confusion about what I meant in this post.  I am not talking about using naked properties instead of accessor methods.  I&#8217;m not talking about accessor methods at all in this post.  A specific accessor method, such as<br />
<pre class="php"><span style="color: #0000ff;">$obj</span>-&gt;<span style="color: #006600;">getFoo</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></pre><br />
where the name of the property is part of the method name is very different than<br />
<pre class="php"><span style="color: #0000ff;">$obj</span>-&gt;<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'foo'</span><span style="color: #66cc66;">&#41;</span>;</pre><br />
Where you pass the name of the property as a string parameter.  Its only the latter pattern, where the property name is an actual parameter to the method, that I am talking about in this post and that I think should generally be refactored.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.procata.com/blog/archives/2007/05/08/let-your-properties-be-properties/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Bad Code Smells in WACT, a refactoring review</title>
		<link>http://www.procata.com/blog/archives/2004/10/18/bad-code-smells-in-wact-a-refactoring-review/</link>
		<comments>http://www.procata.com/blog/archives/2004/10/18/bad-code-smells-in-wact-a-refactoring-review/#comments</comments>
		<pubDate>Mon, 18 Oct 2004 21:32:04 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://www.procata.com/blog/archives/2004/10/18/bad-code-smells-in-wact-a-refactoring-review/</guid>
		<description><![CDATA[This post shows some bad design elements in WACT and discusses how to improve them.  It lists some related bad code smells in the WACT template compiler.  It is a non-trivial example of refactoring in an established application and an illustration of OO design issues.
related files:
treebuilder.inc.php
parserstate.inc.php
tagjudge.inc.php
tagdictionary.inc.php
input.tag.php
The TreeBuilder and ComponentParsingState classes may need to [...]]]></description>
			<content:encoded><![CDATA[<p>This post shows some bad design elements in <a href="http://sourceforge.net/projects/wact/">WACT</a> and discusses how to improve them.  It lists some related bad code smells in the WACT template compiler.  It is a non-trivial example of refactoring in an established application and an illustration of OO design issues.</p>
<p>related files:<br />
<a href="http://cvs.sourceforge.net/viewcvs.py/wact/wact/framework/template/compiler/treebuilder.inc.php?rev=1.45&#038;view=auto">treebuilder.inc.php</a><br />
<a href="http://cvs.sourceforge.net/viewcvs.py/wact/wact/framework/template/compiler/parserstate.inc.php?rev=1.35&#038;view=auto">parserstate.inc.php</a><br />
<a href="http://cvs.sourceforge.net/viewcvs.py/wact/wact/framework/template/compiler/tagjudge.inc.php?rev=1.8&#038;view=auto">tagjudge.inc.php</a><br />
<a href="http://cvs.sourceforge.net/viewcvs.py/wact/wact/framework/template/compiler/tagdictionary.inc.php?rev=1.9&#038;view=auto">tagdictionary.inc.php</a><br />
<a href="http://cvs.sourceforge.net/viewcvs.py/wact/wact/framework/template/tags/form/input.tag.php?rev=1.18&#038;view=auto">input.tag.php</a></p>
<p>The TreeBuilder and ComponentParsingState classes may need to have code re-distributed between the two. </p>
<p>The TreeBuilder class performs two distinct tasks: building the tree (Managing the Component and ParentComponent fields) and constructing the component.  This may be an example of a <strong>Large Class</strong> code smell.  It is certainly an example of poor class <a href="http://c2.com/cgi/wiki?CouplingAndCohesion">cohesion</a>.</p>
<p>The openBranch method of TreeBuilder has the job of both constructing the component and adding it to the tree.  Breaking this method into two distinct functions would clarify the code and remove a <strong>Long Parameter List</strong> smell from openBranch.</p>
<p>The openBranch and closeBranch methods of TreeBuilder have boolean parameters.  This makes these calls to TreeBuilder from the ComponentParsingState more confusing.  (more on <a href="http://www.sitepoint.com/forums/showpost.php?p=913525&#038;postcount=10">refactoring parameters</a> and boolean parameters).</p>
<p>The closeBranch method performs two tasks. setting the hasClosingTag field is in this method for convenience.  It is really a part of the construction of the component and probably belongs closer to openBranch.  (conveniently eliminating the need for having a boolean parameter in closeBranch).</p>
<p>The Component construction methods on TreeBuilder pass along a Locator object through a deep calling tree.  The Locator object is a field on the ComponentParsingState class.    The many methods on TreeBuilder for constructing a component that are called from ComponentParsingState may be an example of the <strong>Feature Envy</strong> code smell.   There seems to be some excessive  <a href="http://c2.com/cgi/wiki?CouplingAndCohesion">coupling</a> between ComponentParsingState and TreeBuilder.  Perhaps the construction methods really belong to ComponentParsingState, not TreeBuilder?</p>
<p>The startElement, endElement, and emptyElement methods of ComponentParsingState show an example of the <strong>Innappropriate Intimacy</strong> smell by manipulating the component field of the TreeBuilder class ($this->TreeBuilder->Component->).  They do this as part of constructing the component.  This is probably further evidence that the component should be constructed inside the ComponentParsingState class and passed as a whole unit into the openBranch method of the TreeBuilder class for addition into the tree.  Another example of excessive coupling.</p>
<p>The isServerTagComponentTag method of the TagJudge class instantiates a dummy component to examine a local variable containing information about the component and then throws the component away.  The TagInfo records in the TagDictionary are supposed contain metadata about each component.  The information returned by isServerTagComponentTag on the Component class should be moved into the TagInfo meta-data.  Everytime isServerTagComponentTag is called, a TagInfo object should instead be available to call, thus elminating the isServerTagComponentTag method from the TagJudge Class.  Eliminating the unnecessary object instantiation will probably result in  a tiny performance improvement.</p>
<p>The calling of the isComponent method on TagJudge from ComponentParsingState is always a two stage process.  First isComponent is called on TagJudge, then getTagInfo is called on TagDictionary.  That combined with the call to getTagInfo inside the implementation of isComponent and the TagDictionary field of TagJudge, used only in isComponent, suggests that perhaps the isComponent method is really a sophisticated find method for tag information and should be a combined findComponent method on the TagDictionary class that returns NULL if no appropriate TagInfo class can be found.</p>
<p>Right now, the two stage component finding process in ComponentParsingState means that there is a one to one relationship between HTML tags and compile time component tags.  However, the <strong>Switch Statement</strong> code smell in the InputTag suggests that a one to one relationship is not correct, and that we really need a one to many relationship.  The code cannot support a one to many relationship as long as the finding of a TagInfo object is broken up into two stages and not encapsulated into the same place.</p>
<p>The fact that both methods on TagJudge can be moved to other classes, suggests that TagJudge is an example of the <strong>Lazy Class</strong> code smell.  The TagJudge class can probably be eliminated.</p>
<p>The InputTagInfo class shows the <strong>Data Class</strong> code smell.  instead of defining a unique class for each tag to be registered, a common TagInfo class should be available.  An instance of this class could then be initialized and registered.  Then the common TagInfo class would be available to move behavior to in further refactoring efforts.  Additionally, this would allow the information in the TagDictionary to be loaded from configuration files, instead of requiring tag files to be included, a slow and unncessary process.</p>
<p>These refactoring issues are related to three WACT feature requests.  Each of these three requests should largely be a change to the TagDictionary class and the (nonexistent) TagInfo class.  The fact that theses features require broad changes across the template compiler is an example of the <strong>Shotgun Surgery</strong> code smell and an example of poor <a href="http://c2.com/cgi/wiki?EncapsulationDefinition">encapsulation</a> around the TagInfo and TagDictionary classes.</p>
<p><a href="http://sourceforge.net/tracker/index.php?func=detail&#038;aid=911634&#038;group_id=85372&#038;atid=575987">Allow multiple components per tag</a><br />
<a href="http://sourceforge.net/tracker/index.php?func=detail&#038;aid=910997&#038;group_id=85372&#038;atid=575987">Convert TagInfo to XML based format</a><br />
<a href="http://sourceforge.net/tracker/index.php?func=detail&#038;aid=910988&#038;group_id=85372&#038;atid=575987">Lazy loading for Compiler Components</a></p>
<p>Note that implementing the lazy loading will have a significant impact on compilation times, illustrating that fully refactored normalized code is easier to performance tune.</p>
<p>Anyone else see any additional code smells here?  Anyone up for a non-trivial refactoring learning exercise?  I hope to be able to eventually post the refactored versions of these files.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.procata.com/blog/archives/2004/10/18/bad-code-smells-in-wact-a-refactoring-review/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>PHP Coding Standards</title>
		<link>http://www.procata.com/blog/archives/2004/09/24/php-coding-standards/</link>
		<comments>http://www.procata.com/blog/archives/2004/09/24/php-coding-standards/#comments</comments>
		<pubDate>Fri, 24 Sep 2004 19:03:07 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Design]]></category>
		<category><![CDATA[coding-standards]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://www.procata.com/blog/archives/2004/09/24/php-coding-standards/</guid>
		<description><![CDATA[I have to disagree with Paul Jones on coding standards:

The thing about defining a coding style standard is that there is no objective means by which to judge one style as â€œbetterâ€ or â€œmore-rightâ€ than another.

I can think of several objective criteria for judging coding standards practices off the top of my head:

Does the practice [...]]]></description>
			<content:encoded><![CDATA[<p>I have to disagree with Paul Jones <a href="http://paul-m-jones.com/blog/index.php?p=34">on coding standards</a>:</p>
<blockquote><p>
The thing about defining a coding style standard is that there is no objective means by which to judge one style as â€œbetterâ€ or â€œmore-rightâ€ than another.
</p></blockquote>
<p>I can think of several objective criteria for judging coding standards practices off the top of my head:</p>
<ul>
<li>Does the practice allow more code to be fit on the screen/page at one time (braces on the same line)</li>
<li>Does the code use white space to emphasize when things should be separate (spaces after commas)</li>
<li>Can the code written to the standard be viewed in multiple media and multiple editors (spaces not tabs, limited line lengths)</li>
<li>Does the practice conserve keystrokes (tabs not spaces, no spaces after commas <img src='http://www.procata.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> )</li>
<li>Does the practice facilitate copy and pasting (single statement of code per line)</li>
<li>Does the practice require extra work to maintain the code (java docs and formatted comment headers are harder to edit beceause of the prefix, aligning equals signs, etc)</li>
</ul>
<p>Obviously some practices can cause conflicts between criteria.  In this case, readability should triumph over key strokes.  Practices that require require editing unrelated lines during maintenance should be avoided.</p>
<p>One thing that I have found about refactoring is that no code smell is too small to fix.  Having your code in refactored normal form and the process of getting it there by performing small, seemingly unimportant refactorings can reveal the major refactorings that are really worth it.  Sometimes the major changes that need to be made are obscured by minor code smells.</p>
<p>Coding styles perform a similar function. Having your code in a &#8220;normalized&#8221; form frees the mind to consider other aspects of the code.  Poorly or inconsistently styled code can obscure refactorings.  With a good coding standard, the benefits outweigh having to re-train yourself out of a few habits.  I agree with Paul&#8217;s conclusion here.</p>
<p>Fortunately, the <a href="http://pear.sourceforge.net/en/standards.php">PEAR standards</a> has made the right decisions regarding these criteria with a couple of minor exceptions and many omissions.</p>
<p>The exceptions as I see them:</p>
<ul>
<li><a href="http://pear.sourceforge.net/en/standards.funcdef.php">aligning equal signs</a> should be discouraged.</li>
<li>The braces policy for <a href="http://pear.sourceforge.net/en/standards.funcdef.php">functions</a> should match that of <a href="http://pear.sourceforge.net/en/standards.control.php">control structures</a></li>
<li><a href="http://pear.sourceforge.net/en/standards.naming.php">embedding inheritance heirarchy into the class name</a> is bad</li>
</ul>
<p><strong>UPDATE:</strong> Looks I am not the only one who thinks <a href="http://blog.colorstudy.com/ianb/weblog/2004/09/22.html#P158">bad style is a bad smell</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.procata.com/blog/archives/2004/09/24/php-coding-standards/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

