<?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>Programmer&#039;s Paradox &#187; Programming</title>
	<atom:link href="http://www.programmersparadox.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.programmersparadox.com</link>
	<description></description>
	<lastBuildDate>Wed, 07 Dec 2011 15:18:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PHPUnit getMock Method Signature</title>
		<link>http://www.programmersparadox.com/2011/06/16/phpunit-getmock-method-signature/</link>
		<comments>http://www.programmersparadox.com/2011/06/16/phpunit-getmock-method-signature/#comments</comments>
		<pubDate>Fri, 17 Jun 2011 03:17:27 +0000</pubDate>
		<dc:creator>Mark Mzyk</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[php phpunit mock]]></category>

		<guid isPermaLink="false">http://www.programmersparadox.com/?p=1364</guid>
		<description><![CDATA[In writing unit tests for PHP, I normally use PHPUnit. I frequently use mocks, but forget the params for the getMock call after the first two arguments, as I don&#8217;t use the rest frequently. Looking up the method signature through Google is frustratingly difficult, so here it is for future reference. PHPUnit getMock takes a [...]<p><br/><br/><a href="http://www.programmersparadox.com/2011/06/16/phpunit-getmock-method-signature/">PHPUnit getMock Method Signature</a></p>
]]></description>
			<content:encoded><![CDATA[<p>In writing unit tests for PHP, I normally use <a href="https://github.com/sebastianbergmann/phpunit/">PHPUnit</a>. I frequently use mocks, but forget the params for the getMock call after the first two arguments, as I don&#8217;t use the rest frequently. Looking up the method signature through Google is frustratingly difficult, so here it is for future reference.</p>
<p>PHPUnit getMock takes a possible seven params.</p>
<p>They are, in order:</p>
<ol>
<li>String &#8211; Required &#8211; the class name to mock</li>
<li>Array &#8211; Optional &#8211; the methods to mock</li>
<li>Array &#8211; Optional &#8211; arguments to pass to the mock&#8217;s constructor</li>
<li>String &#8211; Optional &#8211; a class name for the mock</li>
<li>Boolean &#8211; Optional &#8211; disable the call to the original class&#8217; constructor</li>
<li>Boolean &#8211; Optional &#8211; disable the call to the original object&#8217;s clone method</li>
<li>Boolean &#8211; Optional &#8211; disable autoload during the mock object creation</li>
</ol>
<p>The getMock implementation can be found on github, in the phpunit-mock-objects repo, in <a href="https://github.com/sebastianbergmann/phpunit-mock-objects/blob/1.0/PHPUnit/Framework/MockObject/Generator.php">Generator.php</a>.</p>
<p>Documentation on what I just explained can be found in the <a href="http://www.phpunit.de/manual/3.4/en/api.html">PHP 3.4 docs</a> with arduous searching (see Table 22.6 TestCase), but I&#8217;ve not found the same page in the <a href="http://www.phpunit.de/manual/current/en/">3.5 docs</a>.</p>
<p>I&#8217;ve never used beyond the third argument to getMock, so I have no idea how useful the rest are.</p>
<p><br/><br/><a href="http://www.programmersparadox.com/2011/06/16/phpunit-getmock-method-signature/">PHPUnit getMock Method Signature</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.programmersparadox.com/2011/06/16/phpunit-getmock-method-signature/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Randomly Failing PHP Tests</title>
		<link>http://www.programmersparadox.com/2011/04/20/randomly-failing-php-tests/</link>
		<comments>http://www.programmersparadox.com/2011/04/20/randomly-failing-php-tests/#comments</comments>
		<pubDate>Wed, 20 Apr 2011 14:56:52 +0000</pubDate>
		<dc:creator>Mark Mzyk</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phpunit]]></category>

		<guid isPermaLink="false">http://www.programmersparadox.com/?p=1320</guid>
		<description><![CDATA[At work we have several PHPUnit tests that randomly fail. These ghost failures cause a lot of pain. One run the test fails, the next it passes. These tests are slow poison, as they degrade trust in the system. The code was inspected, but nothing could be found wrong. For some reason, on random test [...]<p><br/><br/><a href="http://www.programmersparadox.com/2011/04/20/randomly-failing-php-tests/">Randomly Failing PHP Tests</a></p>
]]></description>
			<content:encoded><![CDATA[<p>At work we have several PHPUnit tests that randomly fail.  These ghost failures cause a lot of pain. One run the test fails, the next it passes. These tests are slow poison, as they degrade trust in the system. The code was inspected, but nothing could be found wrong. For some reason, on random test runs, the arrays being returned would have their elements in a different order, but no one knew why.</p>
<p>The order of the arrays clearly mattered in our tests, so what was causing them to change that we didn&#8217;t expect?</p>
<p><strong>Short version</strong>:</p>
<p>In the tests we were using PHPUnit&#8217;s assert functionality to test that two arrays were equal.  If the ordering between the two arrays is off, even if they contain the same key value pairs, PHPUnit will fail the test.  Comparing arrays in this way normally works, because PHP <em>usually</em> guarantees array order; however, <em>under some circumstances order is not guaranteed</em>. This was the cause of the random failures.  To fix this, we just need to be smarter about how we compare arrays.</p>
<p><strong>Long version</strong>:</p>
<p>In investigating one of the tests that was failing randomly, I saw that it was performing an assertEquals on two arrays.  Sometimes this assert would fail because the ordering in one of the arrays had changed for that test run.</p>
<p>The question that is relevant here is this: does PHP guarantee the order of arrays? </p>
<p>The answer: it depends. </p>
<p><a href="http://php.net/manual/en/language.types.array.php">PHP arrays</a> are implemented under the hood as an ordered map &#8211; what is essentially a <a href="http://download.oracle.com/javase/1.4.2/docs/api/java/util/LinkedHashMap.html">Linked Hash Map</a> in Java. Insertion order is guaranteed. However, <em>PHP breaks this contract for some array functions</em>, such as <a href="http://us.php.net/manual/en/function.array-slice.php">array_slice</a>, which does not guarantee order unless a flag is set.</p>
<p>In the case of the test I investigated, the final array was being generated by an array_slice that was buried in the code. One potential fix is to change the code under test so it does not use array_slice or so the flag to maintain order is set.  The other fix is to make the tests smarter.</p>
<p>I opted to go with option two, making the tests smarter. This can be done by using compare functions such as <a href="http://us.php.net/manual/en/function.array-diff.php">array_diff</a> or <a href="http://us.php.net/manual/en/function.array-diff-assoc.php">array_diff_assoc</a> and then asserting on the output of the compare function instead of asserting for equality between two arrays.  Another option is to sort the output array, so it always has the same order.  Your creativity is the limit on how you solve this.</p>
<p>What does this episode indicate? That it is worth understanding how the language you&#8217;re using operates.  While PHP has more warts than most, all languages have idiosyncratic behavior that it is worth being aware of. The next time you see perplexing behavior, ask yourself why it&#8217;s happening and investigate. The reasoning why the language is behaving as it is might surprise you.</p>
<p><br/><br/><a href="http://www.programmersparadox.com/2011/04/20/randomly-failing-php-tests/">Randomly Failing PHP Tests</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.programmersparadox.com/2011/04/20/randomly-failing-php-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Helpful Erlang Tidbits</title>
		<link>http://www.programmersparadox.com/2011/01/17/helpful-erlang-tidbits/</link>
		<comments>http://www.programmersparadox.com/2011/01/17/helpful-erlang-tidbits/#comments</comments>
		<pubDate>Tue, 18 Jan 2011 02:33:58 +0000</pubDate>
		<dc:creator>Mark Mzyk</dc:creator>
				<category><![CDATA[Languages]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[erlang]]></category>

		<guid isPermaLink="false">http://www.programmersparadox.com/?p=1288</guid>
		<description><![CDATA[I found this information useful while working my way through the Erlang in Practice screencasts. All of these apply in the interactive shell (erl). Find the Erlang version you&#8217;re running: erlang:system_info(otp_release). To clear a variable/binding, so it can be used again: f(variable_to_clear). Show all the running erlang processes: erlang:processes(). Find more useful tips from Joe (such [...]<p><br/><br/><a href="http://www.programmersparadox.com/2011/01/17/helpful-erlang-tidbits/">Helpful Erlang Tidbits</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I found this information useful while working my way through the <a href="http://pragprog.com/screencasts/v-kserl/erlang-in-practice">Erlang in Practice</a> screencasts. All of these apply in the interactive shell (erl).</p>
<p>Find the Erlang version you&#8217;re running:</p>
<p><code>erlang:system_info(otp_release).</code></p>
<p>To clear a variable/binding, so it can be used again:</p>
<p><code>f(variable_to_clear).</code></p>
<p>Show all the running erlang processes:</p>
<p><code>erlang:processes().</code></p>
<p>Find more <a href="http://www.joeandmotorboat.com/2008/07/29/a-couple-erlang-tips/">useful tips from Joe</a> (such as how to ping a node with a dash in the name).</p>
<p><br/><br/><a href="http://www.programmersparadox.com/2011/01/17/helpful-erlang-tidbits/">Helpful Erlang Tidbits</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.programmersparadox.com/2011/01/17/helpful-erlang-tidbits/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Poaching Chicken and Code</title>
		<link>http://www.programmersparadox.com/2010/04/20/poaching-chicken-and-code/</link>
		<comments>http://www.programmersparadox.com/2010/04/20/poaching-chicken-and-code/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 21:35:52 +0000</pubDate>
		<dc:creator>Mark Mzyk</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[cooking]]></category>
		<category><![CDATA[technique]]></category>

		<guid isPermaLink="false">http://www.programmersparadox.com/?p=1167</guid>
		<description><![CDATA[I&#8217;m an avid cook. I love the opportunity to try out new recipes, especially those from another culture. Sometimes my wife is pleasantly surprised by the results, other times, she&#8217;s just surprised. For Christmas, I recieved Rick Bayless&#8216; cookbook Authentic Mexican. My first foray using a recipe from the book involved making chicken enchiladas. To [...]<p><br/><br/><a href="http://www.programmersparadox.com/2010/04/20/poaching-chicken-and-code/">Poaching Chicken and Code</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m an avid cook.  I love the opportunity to try out new recipes, especially those from another culture.  Sometimes my wife is pleasantly surprised by the results, other times, she&#8217;s just surprised.  For Christmas, I recieved <a href="http://www.rickbayless.com/">Rick Bayless</a>&#8216; cookbook <a href="http://www.rickbayless.com/cookbooks/authenticmexican.html">Authentic Mexican</a>.  My first foray using a recipe from the book involved making chicken enchiladas.  To prepare the chicken, Rick instructed that it should be poached.  I had never poached a chicken before.</p>
<p>Poaching isn&#8217;t difficult.  You bring water to a boil, drop in chicken, add spices, ignore.  After the chicken is cooked, pull it out, continue with dish.  I was disappointed in that the spices didn&#8217;t add much, if any, flavor to the chicken; however, they smelled spectacular while the chicken cooked. The chicken was amazingly tender. It almost shredded itself for the enchiladas.  It is difficult to get chicken this tender using other cooking techniques; it would takes a watchful eye and careful hand.</p>
<p>From this experience I added a new skill to my cooking repertoire. I now know poaching&#8217;s strengths and weaknesses.  Had I not been venturous, I wouldn&#8217;t have grown as a cook.  In coding, it&#8217;s the same.  If I never leave my comfort zone, I never learn new techniques.  I can keep writing code using the same methods and same tools, but it&#8217;s akin to frying chicken every night.  It&#8217;s good at first, but eventually you get tired of it and it might kill you.</p>
<p>Spice is the variety of life.  Try a new technique and see what it teaches you.  By knowing how to use an array of tools you have the skills to program in any situation.  It&#8217;s the same as knowing multiple ways to cook chicken:  you can always prepare the chicken, no matter what kitchen you find yourself in, so you never go hungry and never tire of practicing your trade.</p>
<p><br/><br/><a href="http://www.programmersparadox.com/2010/04/20/poaching-chicken-and-code/">Poaching Chicken and Code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.programmersparadox.com/2010/04/20/poaching-chicken-and-code/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Book as Script, Code as Script</title>
		<link>http://www.programmersparadox.com/2009/12/13/book-as-script-code-as-script/</link>
		<comments>http://www.programmersparadox.com/2009/12/13/book-as-script-code-as-script/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 05:18:43 +0000</pubDate>
		<dc:creator>Mark Mzyk</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[audiobooks]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Neil Gaiman]]></category>

		<guid isPermaLink="false">http://www.programmersparadox.com/?p=1087</guid>
		<description><![CDATA[Heard any good books lately? So asks Neil Gaiman on NPR in a story on audiobooks.  It makes for riveting listening, thanks to Gaiman&#8217;s ability to make the mundane fantastic and because he has a mesmerizing British accent that holds me rapt. Gaiman makes the point that audiobooks continue on, strong as ever.  The rise [...]<p><br/><br/><a href="http://www.programmersparadox.com/2009/12/13/book-as-script-code-as-script/">Book as Script, Code as Script</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Heard any good books lately?</p>
<p>So asks <a href="http://www.npr.org/templates/story/story.php?storyId=120769925">Neil Gaiman on NPR</a> in a story on audiobooks.  It makes for riveting listening, thanks to Gaiman&#8217;s ability to make the mundane fantastic and because he has a mesmerizing British accent that holds me rapt.</p>
<p>Gaiman makes the point that audiobooks continue on, strong as ever.  The rise of the iPod has only made them more popular.  Given our information starved yet saturated world it seems the ceiling for audiobooks is limitless &#8211; until the day we can download information directly. Even then, there&#8217;s always a market for a good story.</p>
<p>So what is an audiobook? Gaiman wonders.</p>
<p>According to audio producer Rick Harris: &#8220;Well, my feeling is that it is not a book.&#8221;</p>
<p>&#8220;An audiobook is a separate entity that is absolutely true.  And a novel can be seen as many things, and one of the things it can be seen as is a script for an audio performance.&#8221;</p>
<p>Gaiman sums up: &#8220;An audiobook is its own thing, a unique medium that goes in through the ear, sometimes leaving you sitting in the driveway to find out how the story is going to end.&#8221;</p>
<p>As an audiobook is its own thing &#8211; separate from, but attached to, a book.</p>
<p>So to a program is its own thing &#8211; separate from, but attached to, code.</p>
<p>For what is code, except a script that is read by the compiler/interpreter?  We developers just happen to count on the compiler/interpreter reading the script we give it the same way every time, even though there is nothing that dictates this must happen.</p>
<p>It would be an interesting world to have a compiler/interpreter that put its own spin on the code given it.  To an extent, this does happen now, except in reverse.  You and I can write dialects of the same code and the compiler/interpreter will read it and spit back the same performance, even though it was based on two different scripts.</p>
<p>Code as performance.</p>
<blockquote><p>I grew up in a world where stories were read aloud<br />
- Neil Gaiman</p></blockquote>
<p>What would the world be like if code was read aloud?</p>
<p style="text-align: center;"><a href="http://www.programmersparadox.com/wp-content/uploads/2009/12/audiobook.jpg"><img class="size-full wp-image-1091 alignnone" title="audiobook" src="http://www.programmersparadox.com/wp-content/uploads/2009/12/audiobook.jpg" alt="audiobook" width="500" height="375" /></a></p>
<p style="text-align: center;"><a href="http://www.flickr.com/photos/playfullibrarian/3315024196">http://www.flickr.com/photos/playfullibrarian/3315024196</a> / <a href="http://creativecommons.org/licenses/by/2.0/">CC 2.0</a></p>
<p><br/><br/><a href="http://www.programmersparadox.com/2009/12/13/book-as-script-code-as-script/">Book as Script, Code as Script</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.programmersparadox.com/2009/12/13/book-as-script-code-as-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

