<?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>cakebaker &#187; refactoring</title>
	<atom:link href="http://cakebaker.42dh.com/tags/refactoring/feed/" rel="self" type="application/rss+xml" />
	<link>http://cakebaker.42dh.com</link>
	<description>baking cakes with CakePHP</description>
	<lastBuildDate>Mon, 19 Jul 2010 14:23:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Enabling submit button if text entered</title>
		<link>http://cakebaker.42dh.com/2009/01/13/enabling-submit-button-if-text-entered/</link>
		<comments>http://cakebaker.42dh.com/2009/01/13/enabling-submit-button-if-text-entered/#comments</comments>
		<pubDate>Tue, 13 Jan 2009 17:11:30 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[jquery]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/?p=1061</guid>
		<description><![CDATA[While looking at some jQuery code I wrote a while ago I noticed the following snippet: $('#username').keyup(function() { if ($('#username').val() != '') { $('#submit').removeAttr('disabled'); } else { $('#submit').attr('disabled', 'disabled'); } }); It ensures that the submit button is only enabled if the &#8220;username&#8221; input field is not empty (you can see such a behavior on [...]]]></description>
			<content:encoded><![CDATA[<p>While looking at some <a href="http://jquery.com">jQuery</a> code I wrote a while ago I noticed the following snippet:</p>
<pre>
<code>$('#username').keyup(function() {
    if ($('#username').val() != '') {
        $('#submit').removeAttr('disabled');
    } else {
        $('#submit').attr('disabled', 'disabled');
    }
});</code>
</pre>
<p>It ensures that the submit button is only enabled if the &#8220;username&#8221; input field is not empty (you can see such a behavior on twitter for example). </p>
<p>However, the snippet from above can be rewritten in a shorter and more elegant way:</p>
<pre>
<code>$('#username').keyup(function() {
    $('#submit').attr('disabled', !$('#username').val()); 
});</code>
</pre>
<p>Maybe this is helpful for someone out there ;-)</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2009/01/13/enabling-submit-button-if-text-entered/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Using custom asserts for better readability</title>
		<link>http://cakebaker.42dh.com/2008/12/03/using-custom-asserts-for-better-readability/</link>
		<comments>http://cakebaker.42dh.com/2008/12/03/using-custom-asserts-for-better-readability/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 08:34:21 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[refactoring]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/?p=984</guid>
		<description><![CDATA[Recently, I wrote the following test case for a BooleanValidator class (a class which ensures a value is of the type &#8220;boolean&#8221;): class BooleanValidatorTest extends CakeTestCase { private $validator = null; public function setUp() { $this-&#62;validator = new BooleanValidator(); } public function testValidate() { $this-&#62;assertIdentical(true, $this-&#62;validator-&#62;validate(true)); $this-&#62;assertIdentical(true, $this-&#62;validator-&#62;validate(false)); $this-&#62;assertTrue(is_string($this-&#62;validator-&#62;validate(0))); $this-&#62;assertTrue(is_string($this-&#62;validator-&#62;validate(1))); } } The code is [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I wrote the following test case for a BooleanValidator class (a class which ensures a value is of the type &#8220;boolean&#8221;):</p>
<pre>
<code>class BooleanValidatorTest extends CakeTestCase {
    private $validator = null;
	
    public function setUp() {
        $this-&gt;validator = new BooleanValidator();
    }
	
    public function testValidate() {
        $this-&gt;assertIdentical(true, $this-&gt;validator-&gt;validate(true));
        $this-&gt;assertIdentical(true, $this-&gt;validator-&gt;validate(false));
        $this-&gt;assertTrue(is_string($this-&gt;validator-&gt;validate(0)));
        $this-&gt;assertTrue(is_string($this-&gt;validator-&gt;validate(1)));
    }
}</code>
</pre>
<p>The code is quite trivial, though it is not that easy to see what I try to express with this code. And so we have to improve its readability. </p>
<p>For this purpose we will write two custom asserts. Custom asserts are nothing special, they are normal methods. But for some reason it seems like people think they always have to use the assert methods provided by the testing framework. </p>
<p>Let&#8217;s have a look at the test method above. What is verified in the first two lines with assertIdentical()? It is simple: it is verified the validation process recognizes the provided data as valid. So, we have the first custom assert method: assertValid(). </p>
<p>Now you might think the second custom assert method will be assertInvalid(). This would be a good name if the validation process would return false if a value doesn&#8217;t validate. Though, in this specific case a validation error is returned, and so a better name for the second assert method is: assertValidationError(). </p>
<p>With this, we can refactor the example from above to:</p>
<pre>
<code>class BooleanValidatorTest extends CakeTestCase {
    private $validator = null;
	
    public function setUp() {
        $this-&gt;validator = new BooleanValidator();
    }
	
    public function testValidate() {
        $this-&gt;assertValid($this-&gt;validator-&gt;validate(true));
        $this-&gt;assertValid($this-&gt;validator-&gt;validate(false));
        $this-&gt;assertValidationError($this-&gt;validator-&gt;validate(0));
        $this-&gt;assertValidationError($this-&gt;validator-&gt;validate(1));
    }

    private function assertValid($validationResult) {
        $this-&gt;assertIdentical(true, $validationResult);
    }
	
    private function assertValidationError($validationResult) {
        $this-&gt;assertTrue(is_string($validationResult));
    }
}</code>
</pre>
<p>The result is now much easier to read (at least in my opinion), and as a nice side effect of this refactoring we also have eliminated some duplicate code. </p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2008/12/03/using-custom-asserts-for-better-readability/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A use case for the &#8220;extract method&#8221; refactoring</title>
		<link>http://cakebaker.42dh.com/2008/07/16/a-use-case-for-the-extract-method-refactoring/</link>
		<comments>http://cakebaker.42dh.com/2008/07/16/a-use-case-for-the-extract-method-refactoring/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 16:45:29 +0000</pubDate>
		<dc:creator>cakebaker</dc:creator>
				<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://cakebaker.42dh.com/?p=625</guid>
		<description><![CDATA[While refactoring I noticed the following pattern: public function doSomething() { $aVariable = ''; if (X) { $aVariable = 'x'; } else { $aVariable = 'y'; } if ($this-&#62;isSomething()) { // ... } else { $anotherVariable = $aVariable . 'z'; // ... } } In this example the variable $aVariable is defined at the top [...]]]></description>
			<content:encoded><![CDATA[<p>While refactoring I noticed the following pattern:</p>
<pre>
public function doSomething() {
    $aVariable = '';

    if (X) {
        $aVariable = 'x';
    } else {
        $aVariable = 'y';
    }

    if ($this-&gt;isSomething()) {
        // ...
    } else {
        $anotherVariable = $aVariable . 'z';
        // ...
    }
}
</pre>
<p>In this example the variable $aVariable is defined at the top of the method, but it is only used in the &#8220;else&#8221; case of the second &#8220;if&#8221; statement. So this means the definition of $aVariable happens too early, at a time when it is not clear whether $aVariable is even needed. To fix it, we can move the first &#8220;if&#8221; block to the &#8220;else&#8221; case of the second &#8220;if&#8221;:</p>
<pre>
public function doSomething() {
    if ($this-&gt;isSomething()) {
        // ...
    } else {
        $aVariable = '';

        if (X) {
            $aVariable = 'x';
        } else {
            $aVariable = 'y';
        }

        $anotherVariable = $aVariable . 'z';
        // ...
    }
}
</pre>
<p>That&#8217;s better. But we are not finished yet. If you look at the code, the definition of $aVariable is very dominant, and can distract the reader of the code from the method&#8217;s purpose. To improve it, we could use the ternary operator:</p>
<pre>
$aVariable = X ? 'x' : 'y';
</pre>
<p>Using the ternary operator is sometimes seen as bad practice (see for example the <a href="https://trac.cakephp.org/wiki/Development/CodingStandards#TernaryOperator">coding standard for CakePHP</a>). So it is better to apply the <a href="http://www.refactoring.com/catalog/extractMethod.html">Extract Method</a> refactoring:</p>
<pre>
public function doSomething() {
    if ($this-&gt;isSomething()) {
        // ...
    } else {
        $anotherVariable = $this-&gt;getAVariable() . 'z';
        // ...
    }
}

private function getAVariable() {
   $aVariable = '';

    if (X) {
        $aVariable = 'x';
    } else {
        $aVariable = 'y';
    }

    return $aVariable;
}
</pre>
<p>With that, the &#8220;doSomething&#8221; method has become easier to read and understand. </p>
<p>Happy refactoring :)</p>
]]></content:encoded>
			<wfw:commentRss>http://cakebaker.42dh.com/2008/07/16/a-use-case-for-the-extract-method-refactoring/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>
