<?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>Transient Technology &#187; immutable</title>
	<atom:link href="http://martinaharris.com/tag/immutable/feed/" rel="self" type="application/rss+xml" />
	<link>http://martinaharris.com</link>
	<description>Next time you look it might be gone</description>
	<lastBuildDate>Wed, 25 Apr 2012 09:52:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>When you can&#8217;t avoid an out parameter</title>
		<link>http://martinaharris.com/2009/12/out-parameter/</link>
		<comments>http://martinaharris.com/2009/12/out-parameter/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 05:00:05 +0000</pubDate>
		<dc:creator>Martin Harris</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[software quality]]></category>
		<category><![CDATA[builder]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[immutable]]></category>
		<category><![CDATA[parameter]]></category>
		<category><![CDATA[quality]]></category>

		<guid isPermaLink="false">http://martinaharris.com/?p=473</guid>
		<description><![CDATA[We all know its a good idea to avoid using out variables in Java. In almost every case the code can be split up to avoid the situation. Its bad semantics because when reading the code its not obvious which &#8230; <a href="http://martinaharris.com/2009/12/out-parameter/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>We all know its a good idea to avoid using <em>out</em> variables in Java.  In almost every case the code can be split up to avoid the situation.  Its bad semantics because when reading the code its not obvious which object passed, if any will get mutated.  So the reader is forced to open the method to find out what it does. In <a title="Clean Code Book Reference" href="http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1259702833&amp;sr=1-1" target="_blank">Robert C Martin&#8217;s Clean Code Book</a> he calls this forcing a double take, and suggests some ways to avoid the problem.  Rarely you find a case where you can&#8217;t avoid it, or to be more accurate, avoiding it leads to worse problems.</p>
<p>Take a look at this interface.  If I find this populate method, I am likely to have to open implementations.  I don&#8217;t know if it populates one map from the other or both maps.</p>
<pre class="brush: java; gutter: false; wrap-lines: true">import java.util.Map;

public interface NastyPopulator {

void populate(Map&lt;String, String&gt; driver, Map&lt;String, String&gt; driverHolder);

}</pre>
<p>The problem is the semantics of map are that you can read from maps or you can write to them.  I think this is a slightly better approach below.</p>
<p>I have used ImmutableMap.Builder.  The semantics of this are that it is used to build things.  I will call build() on the object later to get a map.  Next its an ImmutableMap.  So not only am I declaring that the two parameters are likely to be used to build something, that thing will ultimately be Immutable so in effect the signature declares that I am not expected to make modifications later.  Finally the method name is unambiguous buildMaps, which pretty much says what its going to do.</p>
<pre class="brush: java; gutter: false; wrap-lines: true">import com.google.common.collect.ImmutableMap;

public interface LessNastyPopulator {

void buildMaps(ImmutableMap.Builder&lt;String, String&gt;  driver, ImmutableMap.Builder&lt;String, String&gt; driverHolder);

}</pre>
<p>Feels like a stronger declaration to me.  If ImmutableMap is awkward you could define your own static builder and pass that in, at least your still declaring up front, that this method is  builds the two maps.  If the method is reading from one map and building others, how about passing an iterator and a builder?</p>
<div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Fout-parameter%2F&amp;title=When+you+can%27t+avoid+an+out+parameter+" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Fout-parameter%2F&amp;title=When+you+can%27t+avoid+an+out+parameter+" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Fout-parameter%2F&amp;title=When+you+can%27t+avoid+an+out+parameter+" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Fout-parameter%2F&amp;headline=When+you+can%27t+avoid+an+out+parameter+" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=When+you+can%27t+avoid+an+out+parameter+&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Fout-parameter%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=When+you+can%27t+avoid+an+out+parameter+&amp;u=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Fout-parameter%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=When+you+can%27t+avoid+an+out+parameter+&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Fout-parameter%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=When+you+can%27t+avoid+an+out+parameter+&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Fout-parameter%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=When+you+can%27t+avoid+an+out+parameter+&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Fout-parameter%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Fout-parameter%2F&amp;title=When+you+can%27t+avoid+an+out+parameter+&amp;summary=&amp;source=" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Fout-parameter%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Fout-parameter%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Fout-parameter%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div><div class="dzone_button" style="float: right; margin-left: 5px;">
<script type="text/javascript">
var dzone_url = 'http://martinaharris.com/2009/12/out-parameter/';
var dzone_title = 'When you can&#8217;t avoid an out parameter';
var dzone_blurb = '';
var dzone_style = '2';
</script>
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
</div>]]></content:encoded>
			<wfw:commentRss>http://martinaharris.com/2009/12/out-parameter/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Refactoring to Google Collections &#8211; ImmutableList  for simple code</title>
		<link>http://martinaharris.com/2009/10/google-collection-refactor-simplifies-implementation/</link>
		<comments>http://martinaharris.com/2009/10/google-collection-refactor-simplifies-implementation/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 14:37:13 +0000</pubDate>
		<dc:creator>Martin Harris</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[immutable]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://martinaharris.com/?p=54</guid>
		<description><![CDATA[Google collections are excellent in multi-threaded applications, so I thought I would re-factor some code to use them. <a href="http://martinaharris.com/2009/10/google-collection-refactor-simplifies-implementation/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I was reading this excellent post on <a href="http://blog.jayway.com/2009/10/22/google-collections/" target="_blank">google collections by Sune Simonsen</a> and decided to re-factor some code of mine.  I have an immutable results object used in a multi-threaded application.  I wanted to feel the difference using the google ImmutableList.  My existing class provides immutability in a very similar way to the google classes.  Static methods are used to instantiate the object.  An array is used to back a list collection.  Defensive copies are made to protect the immutability of the object.  Moving to <a href="http://code.google.com/p/google-collections/" target="_blank">google collections</a> is easy and makes my implementation simple whilst reducing test overhead.</p>
<p><span id="more-54"></span></p>
<h3>Steps taken to refactor</h3>
<p>There was actually very little to do.</p>
<p><strong>Step 1, add a maven dependency</strong></p>
<pre class="brush: xml; gutter: false">
<dependency>
            <groupId>com.google.collections</groupId>
            <artifactId>google-collections</artifactId>
            <version>1.0-rc3</version>
            <type>jar</type>
</dependency>
</pre>
<p><strong>Step 2, replace the array with an ImmutableList</strong></p>
<pre class="brush: java; light: true">
private static final LineResult[] EMPTY_LINERESULT_ARRAY = new LineResult[0];
private LineResult[] lineResults = EMPTY_LINERESULT_ARRAY;
</pre>
<p>could become:</p>
<pre class="brush: java; light: true">
private List&lt;LineResult&gt;  lineResults = ImmutableList.of();
</pre>
<p>In the end I used ImmutableList to clearly my intent.  The usage is internal to the class so there is no advantage in using the List interface.</p>
<pre class="brush: java; light: true">
private ImmutableListst&lt;LineResult&gt;  lineResults = ImmutableList.of();
</pre>
<p><strong>Step 3, The constructor and static factory methods are simplified</strong><br />
We are no longer using an array to back the list, so the protective copy can be moved from the constructor into the static method.  This reduces the window of vulnerability you are exposed to before capturing the data held in the list.  This is a direct result of the google class providing a static factory method.  Nb.  Its a shame that the standard collections don&#8217;t provide static factory methods.</p>
<p>Prior to the changes the protective copy was done in construction like this.</p>
<pre class="brush: java; light: true">
 this.lineResults = lineResults.toArray(EMPTY_LINERESULT_ARRAY);
</pre>
<p>This result is a much simpler constructor.</p>
<pre class="brush: java; light: true; highlight: 3">
private ResultsFileSearch(File fileFound, ImmutableList&lt;LineResult&gt; lineResults) {
        this(fileFound);
        this.lineResults = lineResults;
}
</pre>
<p>My newInstance method makes use of the ImmutableList.copyOf static factory method resulting in a very clean conversion of List into Immutable list.  Note that generics is inferring the type.</p>
<pre class="brush: java; light: true; highlight: 4">
public static ResultsFileSearch newInstance(File fileFound
                 , List&lt;LineResult&gt; lineResults) {
        return new ResultsFileSearch(fileFound
                        ,ImmutableList.copyOf(lineResults));
}
</pre>
<p><strong>Step 4, the getLineResults method no longer has to deal with the array or conversion.</strong><br />
The original array backed object had to deal with returning either an empty list or conversion to an unmodifiable list.  There are two things I did not like about this when I wrote the original.</p>
<ul>
<li>The code has to check the array resulting in some conditional logic.  This increases the test overhead slightly.</li>
<li>The use of Collections.unmodifiableList(Arrays.asList(lineResults)); is very ugly and slow.</li>
</ul>
<p>This was the original, <em>note the subtle bug when returning an empty list</em>.</p>
<pre class="brush: java; light: true">
 public List&lt;LineResult&gt; getLineResults() {

        if ((this.lineResults.length == 0)) {
            return Collections.emptyList();
        } else {
            return Collections.unmodifiableList(Arrays.asList(lineResults));
        }
        return this.lineResults;
}
</pre>
<p>I hope you agree its a much simpler version using the google code.  As we are now using the Immutable implementation, we don&#8217;t have to worry about exposing the reference.</p>
<pre class="brush: java; light: true">
public List&lt;LineResult&gt; getLineResults() {
        return this.lineResults;
}
</pre>
<h3>Conclusions</h3>
<p>Using the google collections makes my implementation much cleaner.  Several complexities are given over to the google collections classes.  Moreover testing of my class is simpler.  The changes are contained within the class.  None of the method signatures change, which has two consequences.  On the plus side the re-factor is contained within this class.  On the negative side, any code modifying the list will raise an UnsupportedOperationException which was no different to my original version.  The construction of my class might be slightly slower, the memory overhead slightly bigger now that we are not using the array.  Counter to this, the getLineResults method is quicker as it now only returns a reference.  In my program this is more important as architecturally it uses bounded queues to hold these objects for processing by threads.  So although its using a little extra memory, its constrained.  The performance gain is worth it.</p>
<p>I recommend using the com.google.common.collect package but adhere to the <a href="http://code.google.com/p/google-collections/wiki/Faq" target="_blank">caveats</a> i.e don&#8217;t expose the types in an API etc.</p>
<div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F&amp;title=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F&amp;title=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F&amp;title=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F&amp;headline=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code&amp;u=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F&amp;title=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code&amp;summary=&amp;source=" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div><div class="dzone_button" style="float: right; margin-left: 5px;">
<script type="text/javascript">
var dzone_url = 'http://martinaharris.com/2009/10/google-collection-refactor-simplifies-implementation/';
var dzone_title = 'Refactoring to Google Collections &#8211; ImmutableList  for simple code';
var dzone_blurb = '';
var dzone_style = '2';
</script>
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
</div>]]></content:encoded>
			<wfw:commentRss>http://martinaharris.com/2009/10/google-collection-refactor-simplifies-implementation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

