<?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/"
		xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
	xmlns:media="http://search.yahoo.com/mrss/"
>

<channel>
	<title>Frank Carver&#039;s Punch Barrel &#187; array</title>
	<atom:link href="http://blog.punchbarrel.com/tag/array/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.punchbarrel.com</link>
	<description>Frank Carver&#039;s musings about software and life</description>
	<lastBuildDate>Thu, 08 Dec 2011 22:58:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<copyright>Copyright &#xA9; Frank Carver&#039;s Punch Barrel 2010 </copyright>
	<managingEditor>frank.carver@googlemail.com (Frank Carver&#039;s Punch Barrel)</managingEditor>
	<webMaster>frank.carver@googlemail.com (Frank Carver&#039;s Punch Barrel)</webMaster>
	<ttl>1440</ttl>
	<image>
		<url>http://punchbarrel.com/images/punchbarrel-144.jpg</url>
		<title>Frank Carver&#039;s Punch Barrel</title>
		<link>http://blog.punchbarrel.com</link>
		<width>144</width>
		<height>144</height>
	</image>
	<itunes:subtitle></itunes:subtitle>
	<itunes:summary>Frank Carver&#039;s musings about software and life</itunes:summary>
	<itunes:keywords></itunes:keywords>
	<itunes:category text="Society &#38; Culture" />
	<itunes:author>Frank Carver&#039;s Punch Barrel</itunes:author>
	<itunes:owner>
		<itunes:name>Frank Carver&#039;s Punch Barrel</itunes:name>
		<itunes:email>frank.carver@googlemail.com</itunes:email>
	</itunes:owner>
	<itunes:block>no</itunes:block>
	<itunes:explicit>no</itunes:explicit>
	<itunes:image href="http://punchbarrel.com/images/punchbarrel-144.jpg" />
		<item>
		<title>Never return Null Arrays &#8211; really?</title>
		<link>http://blog.punchbarrel.com/2008/10/16/never-return-null-arrays-really/</link>
		<comments>http://blog.punchbarrel.com/2008/10/16/never-return-null-arrays-really/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 09:33:17 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[Information]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[collection]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[null]]></category>
		<category><![CDATA[partridge]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://blog.punchbarrel.com/?p=681</guid>
		<description><![CDATA[Scott Selikoff recently posted that we should &#8220;never return null arrays&#8221;. I&#8217;m not sure I entirely agree. Both the tone of the article and the comments so far seem to be in agreement, but I am still not so sure. I&#8217;ll skip the terminology issue for the moment, just note that an array in Java [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.selikoff.net/blog/">Scott Selikoff</a> recently posted that we should &#8220;never return null arrays&#8221;. I&#8217;m not sure I entirely agree. Both the tone of the article and the comments so far seem to be in agreement, but I am still not so sure.</p>
<p>I&#8217;ll skip the terminology issue for the moment, just note that an array in Java is different, in syntax, interface and implementation, from a Collection. Scott&#8217;s article seems to discuss Collections rather than arrays.</p>
<p>As far as I am concerned, there is a qualitative distinction between a Collection which exists, but is empty, and a collection which simply does not exist (i.e is null). I&#8217;ll grant that in many situations they should result in the same action or output; but not always.</p>
<p>Imagine parsing XML documents. Consider the following three cases:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;result<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/result<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;result<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;stuff</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/result<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;result<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;stuff<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/stuff<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/result<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>I suggest that first and second cases are different. It&#8217;s relatively easy to imagine that some software should behave differently in the case where the &#8220;stuff&#8221; block is not present at all, and in the case where it is present, but empty. A simplistic example might be that in the second and third cases a &#8220;Stuff&#8221; heading is rendered on a web page, but not in the first one.</p>
<p>If that abstract example is not enough to convince, consider the more practical example of an &#8220;all you can eat&#8221; buffet restaurant (pause for a Homer Simpson &#8220;<em>mmm &#8230; buffet</em>&#8220;). The way many of these places work is that when you arrive you pay a flat fee and receive an empty plate. You may then take that plate to the food and fill it with whatever you like. If you do not have a plate, you have not paid, and so can neither physically nor contractually fill it with food.</p>
<p>In this example the plate represents the collection. The state of having no (null) plate is very different from having an (empty) plate. The restaurant staff would be wise to know the difference.</p>
<p>Just for fun, here&#8217;s some java to illustrate:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Diner <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> Wallet wallet<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Collection</span> plate<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> Diner<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    wallet <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Wallet<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> pay<span style="color: #009900;">&#40;</span>Cashier cashier<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Money fee <span style="color: #339933;">=</span> wallet.<span style="color: #006633;">extract</span><span style="color: #009900;">&#40;</span>cashier.<span style="color: #006633;">getFee</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: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">plate</span> <span style="color: #339933;">=</span> cashier.<span style="color: #006633;">payForPlate</span><span style="color: #009900;">&#40;</span>fee<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: #000066; font-weight: bold;">void</span> selectFoodItem<span style="color: #009900;">&#40;</span>Buffet buffet, <span style="color: #003399;">Object</span> item<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>buffet.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span>item<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      buffet.<span style="color: #006633;">grab</span><span style="color: #009900;">&#40;</span>item, plate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Buffet <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> contains<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> item<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// the ultimate buffet :)</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> grab<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> item, <span style="color: #003399;">Collection</span> plate<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: #000066; font-weight: bold;">null</span> <span style="color: #339933;">==</span> plate<span style="color: #009900;">&#41;</span> 
      <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> RestaurantException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;This is not a soup kitchen! Pay first or hit the road&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>plate.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I see you brought your big plate, Alan&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    plate.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>item<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The bottom line is that while the <a href="http://www.cs.oberlin.edu/~jwalker/nullObjPattern/">Null Object pattern</a> is a very useful pattern, it should not be used indiscriminately. Just like any pattern, really.</p>
<p><a href="http://www.selikoff.net/blog/2008/10/15/never-return-null-arrays/">Never return Null Arrays! | Down Home Country Coding With Scott Selikoff and Friends</a></p>
<p>I look forward to more thought-provoking posts from Scott and Jeanne!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.punchbarrel.com/2008/10/16/never-return-null-arrays-really/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>mockito 1.5 and spying on objects</title>
		<link>http://blog.punchbarrel.com/2008/09/19/mockito-15-and-spying-on-objects/</link>
		<comments>http://blog.punchbarrel.com/2008/09/19/mockito-15-and-spying-on-objects/#comments</comments>
		<pubDate>Fri, 19 Sep 2008 10:51:34 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[Information]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[mockito]]></category>
		<category><![CDATA[spy]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://blog.punchbarrel.com/?p=465</guid>
		<description><![CDATA[Mockito is a Java Mock objects framework which seems more usable than either of the main contenders (JMock and EasyMock). The project has just released a new version which now allows the attachment of a mock &#8220;overlay&#8221; to an existing object. This has been on my &#8220;evaluate&#8221; stack for months, but (as Harry Pynn pointed [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://code.google.com/p/mockito/">Mockito</a> is a Java Mock objects framework which seems more usable than either of the main contenders (<a href="http://www.jmock.org/">JMock</a> and <a href="http://www.easymock.org/">EasyMock</a>). The project has just released a new version which now allows the attachment of a mock &#8220;overlay&#8221; to an existing object. </p>
<p>This has been on my &#8220;evaluate&#8221; stack for months, but (as Harry Pynn pointed out when <a href="http://wevouchfor.org/users/Frank+Carver">he &#8220;vouched for&#8221; me</a>) I tend to be more comfortable with &#8220;classic&#8221; TDD and don&#8217;t usually find much use for Mock Objects.</p>
<p>I have had a quick play with the examples of use on the Mockito site with mixed success. many obvious cases work well, but there are also some gaps in the way it works. For example, consider this small example test:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #006633;">spy</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PlaypenTest <span style="color: #000000; font-weight: bold;">extends</span> TestCase <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testMockito<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">List</span> real <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		real.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;first&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		real.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;second&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">List</span> ml <span style="color: #339933;">=</span> spy<span style="color: #009900;">&#40;</span>real<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		stub<span style="color: #009900;">&#40;</span>ml.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toReturn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;huh&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;huh&quot;</span>, ml.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;second&quot;</span>, ml.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It works correctly. However, the following idiomatic and functionally similar simplification throws a complicated internal exception in the &#8220;spy&#8221; method.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #006633;">spy</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PlaypenTest <span style="color: #000000; font-weight: bold;">extends</span> TestCase <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testMockito<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">List</span> real <span style="color: #339933;">=</span> <span style="color: #003399;">Arrays</span>.<span style="color: #006633;">asList</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;first&quot;</span>, <span style="color: #0000ff;">&quot;second&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">List</span> ml <span style="color: #339933;">=</span> spy<span style="color: #009900;">&#40;</span>real<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		stub<span style="color: #009900;">&#40;</span>ml.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toReturn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;huh&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;huh&quot;</span>, ml.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;second&quot;</span>, ml.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Maybe I&#8217;ll get around to using it for something. I&#8217;m still not sure.</p>
<p><a href="http://code.google.com/p/mockito/">mockito &#8211; Google Code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.punchbarrel.com/2008/09/19/mockito-15-and-spying-on-objects/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.960 seconds -->

