<?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>The Pragmatic Craftsman &#187; immutability</title>
	<atom:link href="http://pragmaticcraftsman.com/tag/immutability/feed/" rel="self" type="application/rss+xml" />
	<link>http://pragmaticcraftsman.com</link>
	<description>Simplicity from complexity : by Stanley Kubasek</description>
	<lastBuildDate>Fri, 18 Nov 2011 13:36:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Make fields final, objects immutable</title>
		<link>http://pragmaticcraftsman.com/2009/12/final-fields-immutable-objects/</link>
		<comments>http://pragmaticcraftsman.com/2009/12/final-fields-immutable-objects/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 15:25:19 +0000</pubDate>
		<dc:creator>Stanley</dc:creator>
				<category><![CDATA[Better Coder]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[abstraction]]></category>
		<category><![CDATA[immutability]]></category>

		<guid isPermaLink="false">http://pragmaticcraftsman.com/?p=301</guid>
		<description><![CDATA[Just as it is a good practice to make all fields private unless they need greater visibility, it is a good practice to make all fields final unless they need to be mutable. &#8211;Brian Goetz in Java Concurrency in Practice (page 48) Are you following these fundamental principles? Just so we&#8217;re on the same page, [...]]]></description>
			<content:encoded><![CDATA[<table class="tbQuote">
<tr>
<td class="tbQuoteImageTd">
<div>
					<img src="http://images.kubasek.com/quote/quote.gif" />
				</div>
</td>
<td>
<div class="quoteText">
				Just as it is a good practice to make all fields private unless they need greater visibility, it is a good practice to make all fields final unless they need to be mutable.			</div>
<div class="quoteCopy">
				<span class="quoteAuthor"> &#8211;Brian Goetz </span><br />
				in Java Concurrency in Practice (page 48)			</div>
</td>
</tr>
</table>
<p>Are you following these fundamental principles?</p>
<p>Just so we&#8217;re on the same page, an immutable object is on whose state cannot be changed after construction.</p>
<p>This view is supported by Joshua Bloch in Effective Java (2nd). Item 13 states: &#8220;Minimize the accessibility of classes and members.&#8221;</p>
<table class="tbQuote">
<tr>
<td class="tbQuoteImageTd">
<div>
					<img src="http://images.kubasek.com/quote/quote.gif" />
				</div>
</td>
<td>
<div class="quoteText">
				The single most important factor that distinguishes a well-designed module from a poorly designed one is the degree to which the module hides its internal data and other implementation details from other modules. 			</div>
<div class="quoteCopy">
				<span class="quoteAuthor"> &#8211;Joshua Bloch </span>
							</div>
</td>
</tr>
</table>
<p>He goes on to say some fundamental principles.</p>
<table class="tbQuote">
<tr>
<td class="tbQuoteImageTd">
<div>
					<img src="http://images.kubasek.com/quote/quote.gif" />
				</div>
</td>
<td>
<div class="quoteText">
				A well designed module hides <strong>all </strong>of its implementation details, cleanly separating its API from its implementation. Modules then communicate only through their APIs and are oblivious to each others&#8217; inner workings.			</div>
<div class="quoteCopy">
				<span class="quoteAuthor"> &#8211;Joshua Bloch </span><br />
				Effective Java (2nd), page 67			</div>
</td>
</tr>
</table>
<p>These are really the basics of information hiding or encapsulation. Basics of OOP programming! It&#8217;s a good idea to learn these well.</p>
<p>Think twice before making any fields public. Hold it. No, don&#8217;t make it public! Think twice before making it any other than private!</p>
<p>But to make sure you only expose what is absolutely needed requires some thought. You need your own judgment and experience.</p>
<p>As for the second part, item 15 states: &#8220;Minimize mutability.&#8221;</p>
<p>I think this one is a little harder to justify and not so obvious. But Bloch has some very good arguments. Together with Goetz, they convinced me that I should utilize immutability more often when I&#8217;m designing classes.</p>
<table class="tbQuote">
<tr>
<td class="tbQuoteImageTd">
<div>
					<img src="http://images.kubasek.com/quote/quote.gif" />
				</div>
</td>
<td>
<div class="quoteText">
				Immutable classes are easier to design, implement, and use than mutable classes. They are less prone to error and are more secure.			</div>
<div class="quoteCopy">
				<span class="quoteAuthor"> &#8211;Joshua Bloch </span>
							</div>
</td>
</tr>
</table>
<p>Convinced!</p>
<p>But how? Follow these 5 steps (as per Bloch).</p>
<p>1. Don&#8217;t provide any methods that modify the object&#8217;s state.<br />
2. Ensure the class can&#8217;t be extended.<br />
3. Make all fields final.<br />
4. Make all fields private.<br />
5. Ensure exclusive access to any mutable components.</p>
<p>Goets says it a little bit differently.</p>
<p>An object is <strong>immutable</strong> if:<br />
  &#8211; Its state cannot be modified after construction;<br />
  &#8211; All its fields are final;<br />
  &#8211; It is properly constructed (the this reference does not escape during construction)<br />
Immutable objects pack a lot of goodies in them.</p>
<p>One more time. Tell me <strong>what are the benefits of immutable objects</strong>!</p>
<p>They are simple to understand.<br />
They are thread safe.<br />
They require no synchronization.</p>
<p>Let&#8217;s end with a great summary note from Bloch, <strong>&#8220;Classes should be immutable unless there&#8217;s a very good reason to make t hem mutable.&#8221; </strong></p>
<p>But.</p>
<p>If&#8230;</p>
<p><strong>&#8220;If a class cannot be made immutable, limit its mutablity as much as possible.&#8221;</strong></p>
<p>Strong statements.</p>
<p>Learn these design principles!</p>
<p>All in all, I recommend reading Item 15 from the Effective Java book. And re-read a few times until it becomes part of your design logic.</p>
<p>It&#8217;s all about getting better.</p>
<p>It&#8217;s all about improving.</p>
<p>It&#8217;s good to learn from the pros. Everybody needs a little guidance. I know I do. (These 2 books are excellent! <a href="http://pragmaticcraftsman.com/category/books-i-recommend/">Recommended.</a>)</p>
<p><a href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fpragmaticcraftsman.com%2F2009%2F12%2Ffinal-fields-immutable-objects%2F&amp;linkname=Make%20fields%20final%2C%20objects%20immutable" title="Facebook" rel="nofollow" target="_blank"><img src="http://pragmaticcraftsman.kubasek.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a> <a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fpragmaticcraftsman.com%2F2009%2F12%2Ffinal-fields-immutable-objects%2F&amp;linkname=Make%20fields%20final%2C%20objects%20immutable" title="Twitter" rel="nofollow" target="_blank"><img src="http://pragmaticcraftsman.kubasek.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/google_buzz?linkurl=http%3A%2F%2Fpragmaticcraftsman.com%2F2009%2F12%2Ffinal-fields-immutable-objects%2F&amp;linkname=Make%20fields%20final%2C%20objects%20immutable" title="Google Buzz" rel="nofollow" target="_blank"><img src="http://pragmaticcraftsman.kubasek.com/wp-content/plugins/add-to-any/icons/google_buzz.png" width="16" height="16" alt="Google Buzz"/></a> <a href="http://www.addtoany.com/add_to/dzone?linkurl=http%3A%2F%2Fpragmaticcraftsman.com%2F2009%2F12%2Ffinal-fields-immutable-objects%2F&amp;linkname=Make%20fields%20final%2C%20objects%20immutable" title="DZone" rel="nofollow" target="_blank"><img src="http://pragmaticcraftsman.kubasek.com/wp-content/plugins/add-to-any/icons/dzone.png" width="16" height="16" alt="DZone"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fpragmaticcraftsman.com%2F2009%2F12%2Ffinal-fields-immutable-objects%2F&amp;linkname=Make%20fields%20final%2C%20objects%20immutable" title="Delicious" rel="nofollow" target="_blank"><img src="http://pragmaticcraftsman.kubasek.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/email?linkurl=http%3A%2F%2Fpragmaticcraftsman.com%2F2009%2F12%2Ffinal-fields-immutable-objects%2F&amp;linkname=Make%20fields%20final%2C%20objects%20immutable" title="Email" rel="nofollow" target="_blank"><img src="http://pragmaticcraftsman.kubasek.com/wp-content/plugins/add-to-any/icons/email.png" width="16" height="16" alt="Email"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save">Share/Bookmark</a> </p>]]></content:encoded>
			<wfw:commentRss>http://pragmaticcraftsman.com/2009/12/final-fields-immutable-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/


Served from: pragmaticcraftsman.com @ 2012-05-22 01:35:57 -->
