<?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>cwash into software</title>
	<atom:link href="http://cwash.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://cwash.org</link>
	<description>+= construction + craftsmanship;</description>
	<lastBuildDate>Thu, 29 Sep 2011 04:23:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.2</generator>
		<item>
		<title>Transactions, Part 2 &#8211; Writing Data Reliably</title>
		<link>http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=transactions-part-2-writing-data-reliably</link>
		<comments>http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 14:58:00 +0000</pubDate>
		<dc:creator>Chris Wash</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cwash.org/?p=465</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/";</script>In Part 1, we covered the basic concept of a transaction, what the ACID properties are and how they specify transactional behavior, and why you might use transactions. This time, we&#8217;ll talk more specifically about what it&#8217;s like to use a transaction, and how the contracts it provides allow us to write data in a [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><p>
<blockquote>In <a href="http://cwash.org/2010/07/08/transactions-part-1/">Part 1</a>, we covered the basic concept of a transaction, what the ACID properties are and how they specify transactional behavior, and why you might use transactions.  This time, we&#8217;ll talk more specifically about what it&#8217;s like to use a transaction, and how the contracts it provides allow us to write data in a reliable fashion.</p></blockquote>
<h3>Programming With Transactional Systems</h3>
<h4>Commit and Rollback</h4>
<p>Transactions either <em>commit</em> or <em>rollback</em>.  Until a transaction is committed, it is assumed none of the work it has done is saved.  Once a commit signal is issued by the application (either through an API call, or by a container), this work is actually performed by the transaction processing engine that&#8217;s being used.  If for some reason the commit fails due to an error scenario, or some other runtime exception occurs, we assume that the integrity of the transaction has been compromised and the transaction <em>rolls back</em>, removing any work done up to that point.  When a rollback occurs, the transaction must be resubmitted.<span id="more-465"></span></p>
<p>At its most basic, rollback is simply an undo mechanism, used to revert all changes back to the original state before the transaction began.  To do this, we need to keep a copy of the original state before any changes, and if we need to rollback, we must reinstate the previous copy.  Since there is always the possibility of multiple transactions running, concurrency control comes into play &#8211; we want to rollback to the last committed state, not just the state before beginning a single transaction.  For example, consider this scenario: Alice and Bob both need to submit changes to a source code file under version control, and Alice and Bob must be able to turn in (commit) their changes at the same time in order to meet a deadline.  The source code management system must ensure both transactions are consistent &#8211; that the data is recorded properly, but also make sure that the transactions are durable &#8211; that Alice&#8217;s changes (that were committed first) were not overwritten by Bob&#8217;s changes.  If you had to implement this behavior that&#8217;s provided by the SCM system, how would you do it?</p>
<p>One way to think about the core of transaction processing engines is the Memento Pattern &#8211; as you perform actions within a unit of work that manipulate your application state, the transaction processor keeps track of sets of state changes and what the most recently committed state was.  This way, when Bob goes to commit his work (after Alice), the SCM system can tell that the state of the resource he was working on has changed since he started his work, and can take steps to remedy the situation.  This is a simplified view of what is going on: the steps to remedy Bob&#8217;s situation are not always straightforward.  Also, providing this transactional behavior through network protocols, in a distributed environment, across different types of transactional resources, etc. all introduce complexity.  But all complexity and bugs aside, the essence of transactions, that is, commit and rollback, should retain the simple semantics we outlined above.</p>
<h4>Transactional API Code</h4>
<p>We&#8217;ve talked a lot about transactions so far, but haven&#8217;t seen much in the way of code.  To a large extent, we&#8217;d like to make transactions configurable, for many of the same reasons we tend to make other parts of our code configurable.  But before going too deep into how to configure your code for transactions, let&#8217;s look at how you can control transactional APIs through code (&#8220;by hand&#8221;), first.  Boilerplate code for working with a transactional resource often looks like this:
</p>
<pre class="brush: java; highlight: [5-16];">
public class TestProcess {

	// ...

	public void process() {
		// open connection to resource
		// create transaction object
		// call begin on transaction object
		try {
			// perform interaction with resource
			// call commit on transaction object
		} catch (Exception e) {
			// call rollback on transaction object
		} finally {
			// call close on connection object
		}
	}
}
</pre>
<h4>Problem: Transactional API code mixed in Application Code</h4>
<p>Imagine writing code without using any transactions at all, that is, without any of the <strong>rollback semantics</strong> that we talked about above.&nbsp;&nbsp;If the code is to work correctly in all scenarios, it needs to include a substantial amount of cleanup code. &nbsp;Cleanup code and retry code, in general, is messy code.  There are a lot of corner cases and expected failures that could occur, not to mention unexpected failures/error scenarios that need to be sorted out.  The unassuming rookie programmer usually ignores these cases up front, and tackles adding error handling as he or she discovers the errors. This can make maintenance significantly more difficult because all business logic devolves into the&nbsp;<em>arrow antipattern:</em> becoming littered with if-else statements that inhibit readability and stifle the ability to refactor to a more suitable design. &nbsp;While this is not a big deal in prototyping or non-critical scenarios, when we need to count on data integrity, it&#8217;s not an adequate approach.</p>
<p>Calling transactional APIs around your business logic can dramatically reduce the complexity of your cleanup code by providing rollback semantics. &nbsp;In other words, when it is determined an error scenario has occurred and the transaction cannot be committed, we rollback, or restore the application to a previously consistent state. &nbsp;This often means &#8220;undo-ing&#8221; pending work, but is often implemented by queuing up work to be done and only making those changes when commit is called. &nbsp; Still, code that interacts directly with a transaction manager or monitor API leaves something to be desired in terms of maintainability. &nbsp;Applications constrained to be implemented by working with Transactional APIs tend to make them inherently procedural, which can be more difficult to test and maintain.  Because your business logic is surrounded by another API, often this makes code harder to read as well.  For these reasons, we favor using declarative transaction management in cases where your application demands implementing more than a trivial amount of transaction code.</p>
<h3>Declarative Transaction Management</h3>
<h4>Transaction Attributes</h4>
<p>
<strong>Transaction attributes</strong> allow us to declare how the container should handle interfacing with transaction APIs on our behalf, eliminating a lot of the boilerplate transaction API code and messy cleanup code altogether, allowing it to be specified and live in configuration metadata.  I asked a <a href="http://stackoverflow.com/questions/1759554/examples-or-uses-cases-to-explain-ejb-transaction-attributes" target="_new">question on Stackoverflow</a> that challenged the community to come up with a good metaphor for how these worked.  I would encourage you to read the link to the OpenEJB description, the examples/use cases/metaphors there, and add your own.</p>
<h4>Transaction Attributes in Programming Models</h4>
<p>System.Transactions in .NET, and EJB3 in JEE5+ provide transaction attributes that allow for defining the way your application code should interact with the underlying transactional resource.  See the references section below for more information on each of these models.</p>
<h3>Advanced Transaction Concepts and Terminology</h3>
<h4>Distrubuted (XA) Transactions</h4>
<p>When committing work across two or more different transactional resources, we use a &#8220;distributed&#8221; transaction, also known as an  <em>XA</em> transaction. (XA stands for the industry-standard XA interface defined by the X/Open group.)  In Java, support for distributed transactions is provided by libraries that implement the Java Transaction API (JTA) &#8211; most often you&#8217;ll find these libraries included in the runtime of JavaEE application servers.  N.B. that use of JTA does not necessarily imply that you are using distributed transactions &#8211; it simply provides a space for you to define your own &#8220;global&#8221; transactions, that may include XA and/or &#8220;local&#8221; transactions.</p>
<p>XA  transactions are much more complicated because in order for the ACID properties of all resources to be guaranteed, each system must implement a &#8220;Two-Phase Commit&#8221; protocol that ensures transactional consistency across <strong>all</strong> resources.  Support for this protocol is often included in more robust versions of drivers or connectors and used only when needed, as they introduce complexity and overhead that degrades performance.  It&#8217;s beyond the scope of this article to describe this protocol, but the reader is encouraged to investigate how two-phase commit works if it is in use in his or her environment.</p>
<p>Local transactions do not support the XA protocol &#8211; the transactional semantics are only guaranteed to work against a single resource.  Let&#8217;s look at an example that will contrast the two models.  Consider an application that must write some information to a database and also enqueue a message to a message queue (for example, enqueuing a message that triggers a confirmation e-mail to be sent).  If we are using local transactions, we must ensure that the message is not enqueued after the data has been committed to the database.  This sounds like an easy enough request to accommodate, and may to lead you to use only local transactions.  If there is a problem writing the data to the database, no e-mail will ever be sent, because the global transaction (containing the database updates and message enqueue) will rollback before it ever tries to send the email.  But what if the enqueue of the message fails?  Without using distributed transactions, there is no way to &#8220;uncommit&#8221; the database changes.  There are certainly other strategies to mitigate this problem without using a distributed transaction, but they involve writing cleanup and retry code.  If both the writing of the data to the database and the enqueue of the message are part of a global, <em>distributed</em> transaction, then any problem enqueuing the message will force the entire transaction to rollback, in which case the client will need to resubmit the entire transaction again.  Obviously there are pros and cons to using either approach, but this example should give you a taste of when and why you would choose to use a distributed transaction.  Distributed transactions are useful when data integrity is paramount, but are more difficult to configure and become challenging as the ability to scale and handle highly concurrent transactions increases.</p>
<h4>Compensating Transactions</h4>
<p>When no rollback semantic is available to us, we often find ourselves in a situation where we&#8217;d like to issue a transaction that will &#8220;undo&#8221; a specific transaction that was already previously committed.  This is the idea behind <em>compensating</em> transactions.  They&#8217;re to transactions what anti-particles are to elementary particles in physics; one will &#8220;cancel&#8221; or &#8220;equal out&#8221; the other, the net overall effect being to compensating the state of the system.  To use compensating transactions, for each transaction type you define the steps that will perform the opposite work to reverse a committed transaction.  Compensating transactions are often used in lieu of distributed transactions, but they can also be used to undo an entire distributed transaction that has been committed.  They&#8217;re especially useful for transactions that are very long-lived, for instance, as part of a long business process, but can suffer from the same data integrity issues that they are used to address &#8211; by their very nature they are themselves &#8220;cleanup/retry&#8221; code.  The idea of a compensation transaction is used heavily in the BPM/service orchestration space, where several remote service calls must be glued together into an overall unit of work.  In these cases, compensation transactions can be vital to keeping the orchestration logic in a well-known state, especially when you don&#8217;t have control over the transaction demarcation or a way to enlist in a transaction for systems that you are integrating with.  We won&#8217;t talk much more about them, as the mechanisms that provide this behavior are not standardized, and could be the topic of a series of blog articles all by themselves.</p>
<h3>Up Next</h3>
<ul>
<li>Transaction Isolation</li>
<li>Concurrency Control</li>
</ul>
<h3>References</h3>
<ul>
<li>http://openejb.apache.org/3.0/transaction-annotations.html</li>
<li>http://www.simple-talk.com/dotnet/.net-framework/.net-2.0-transaction-model/</li>
<li>http://en.wikipedia.org/wiki/Two-phase_commit_protocol</li>
</ul>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+2+-+Writing+Data+Reliably&amp;link=http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/&amp;notes=In%20Part%201%2C%20we%20covered%20the%20basic%20concept%20of%20a%20transaction%2C%20what%20the%20ACID%20properties%20are%20and%20how%20they%20specify%20transactional%20behavior%2C%20and%20why%20you%20might%20use%20transactions.%20%20This%20time%2C%20we%27ll%20talk%20more%20specifically%20about%20what%20it%27s%20like%20to%20use%20a%20transaction%2C%20and%20how%20the%20contracts%20it%20provides%20allow%20us%20to%20wr&amp;short_link=http://b2l.me/act4t5&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+2+-+Writing+Data+Reliably&amp;link=http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/&amp;notes=In%20Part%201%2C%20we%20covered%20the%20basic%20concept%20of%20a%20transaction%2C%20what%20the%20ACID%20properties%20are%20and%20how%20they%20specify%20transactional%20behavior%2C%20and%20why%20you%20might%20use%20transactions.%20%20This%20time%2C%20we%27ll%20talk%20more%20specifically%20about%20what%20it%27s%20like%20to%20use%20a%20transaction%2C%20and%20how%20the%20contracts%20it%20provides%20allow%20us%20to%20wr&amp;short_link=http://b2l.me/act4t5&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+2+-+Writing+Data+Reliably&amp;link=http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/&amp;notes=In%20Part%201%2C%20we%20covered%20the%20basic%20concept%20of%20a%20transaction%2C%20what%20the%20ACID%20properties%20are%20and%20how%20they%20specify%20transactional%20behavior%2C%20and%20why%20you%20might%20use%20transactions.%20%20This%20time%2C%20we%27ll%20talk%20more%20specifically%20about%20what%20it%27s%20like%20to%20use%20a%20transaction%2C%20and%20how%20the%20contracts%20it%20provides%20allow%20us%20to%20wr&amp;short_link=http://b2l.me/act4t5&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=24&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+2+-+Writing+Data+Reliably&amp;link=http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/&amp;notes=In%20Part%201%2C%20we%20covered%20the%20basic%20concept%20of%20a%20transaction%2C%20what%20the%20ACID%20properties%20are%20and%20how%20they%20specify%20transactional%20behavior%2C%20and%20why%20you%20might%20use%20transactions.%20%20This%20time%2C%20we%27ll%20talk%20more%20specifically%20about%20what%20it%27s%20like%20to%20use%20a%20transaction%2C%20and%20how%20the%20contracts%20it%20provides%20allow%20us%20to%20wr&amp;short_link=http://b2l.me/act4t5&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-gmail">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+2+-+Writing+Data+Reliably&amp;link=http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/&amp;notes=In%20Part%201%2C%20we%20covered%20the%20basic%20concept%20of%20a%20transaction%2C%20what%20the%20ACID%20properties%20are%20and%20how%20they%20specify%20transactional%20behavior%2C%20and%20why%20you%20might%20use%20transactions.%20%20This%20time%2C%20we%27ll%20talk%20more%20specifically%20about%20what%20it%27s%20like%20to%20use%20a%20transaction%2C%20and%20how%20the%20contracts%20it%20provides%20allow%20us%20to%20wr&amp;short_link=http://b2l.me/act4t5&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=52&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Email this via Gmail">Email this via Gmail</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+2+-+Writing+Data+Reliably&amp;link=http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/&amp;notes=In%20Part%201%2C%20we%20covered%20the%20basic%20concept%20of%20a%20transaction%2C%20what%20the%20ACID%20properties%20are%20and%20how%20they%20specify%20transactional%20behavior%2C%20and%20why%20you%20might%20use%20transactions.%20%20This%20time%2C%20we%27ll%20talk%20more%20specifically%20about%20what%20it%27s%20like%20to%20use%20a%20transaction%2C%20and%20how%20the%20contracts%20it%20provides%20allow%20us%20to%20wr&amp;short_link=http://b2l.me/act4t5&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=74&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+2+-+Writing+Data+Reliably&amp;link=http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/&amp;notes=In%20Part%201%2C%20we%20covered%20the%20basic%20concept%20of%20a%20transaction%2C%20what%20the%20ACID%20properties%20are%20and%20how%20they%20specify%20transactional%20behavior%2C%20and%20why%20you%20might%20use%20transactions.%20%20This%20time%2C%20we%27ll%20talk%20more%20specifically%20about%20what%20it%27s%20like%20to%20use%20a%20transaction%2C%20and%20how%20the%20contracts%20it%20provides%20allow%20us%20to%20wr&amp;short_link=http://b2l.me/act4t5&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+2+-+Writing+Data+Reliably&amp;link=http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/&amp;notes=In%20Part%201%2C%20we%20covered%20the%20basic%20concept%20of%20a%20transaction%2C%20what%20the%20ACID%20properties%20are%20and%20how%20they%20specify%20transactional%20behavior%2C%20and%20why%20you%20might%20use%20transactions.%20%20This%20time%2C%20we%27ll%20talk%20more%20specifically%20about%20what%20it%27s%20like%20to%20use%20a%20transaction%2C%20and%20how%20the%20contracts%20it%20provides%20allow%20us%20to%20wr&amp;short_link=http://b2l.me/act4t5&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=207&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+2+-+Writing+Data+Reliably&amp;link=http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/&amp;notes=In%20Part%201%2C%20we%20covered%20the%20basic%20concept%20of%20a%20transaction%2C%20what%20the%20ACID%20properties%20are%20and%20how%20they%20specify%20transactional%20behavior%2C%20and%20why%20you%20might%20use%20transactions.%20%20This%20time%2C%20we%27ll%20talk%20more%20specifically%20about%20what%20it%27s%20like%20to%20use%20a%20transaction%2C%20and%20how%20the%20contracts%20it%20provides%20allow%20us%20to%20wr&amp;short_link=http://b2l.me/act4t5&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+2+-+Writing+Data+Reliably&amp;link=http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/&amp;notes=In%20Part%201%2C%20we%20covered%20the%20basic%20concept%20of%20a%20transaction%2C%20what%20the%20ACID%20properties%20are%20and%20how%20they%20specify%20transactional%20behavior%2C%20and%20why%20you%20might%20use%20transactions.%20%20This%20time%2C%20we%27ll%20talk%20more%20specifically%20about%20what%20it%27s%20like%20to%20use%20a%20transaction%2C%20and%20how%20the%20contracts%20it%20provides%20allow%20us%20to%20wr&amp;short_link=http://b2l.me/act4t5&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=6&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+2+-+Writing+Data+Reliably&amp;link=http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/&amp;notes=In%20Part%201%2C%20we%20covered%20the%20basic%20concept%20of%20a%20transaction%2C%20what%20the%20ACID%20properties%20are%20and%20how%20they%20specify%20transactional%20behavior%2C%20and%20why%20you%20might%20use%20transactions.%20%20This%20time%2C%20we%27ll%20talk%20more%20specifically%20about%20what%20it%27s%20like%20to%20use%20a%20transaction%2C%20and%20how%20the%20contracts%20it%20provides%20allow%20us%20to%20wr&amp;short_link=http://b2l.me/act4t5&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=4&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+2+-+Writing+Data+Reliably&amp;link=http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/&amp;notes=In%20Part%201%2C%20we%20covered%20the%20basic%20concept%20of%20a%20transaction%2C%20what%20the%20ACID%20properties%20are%20and%20how%20they%20specify%20transactional%20behavior%2C%20and%20why%20you%20might%20use%20transactions.%20%20This%20time%2C%20we%27ll%20talk%20more%20specifically%20about%20what%20it%27s%20like%20to%20use%20a%20transaction%2C%20and%20how%20the%20contracts%20it%20provides%20allow%20us%20to%20wr&amp;short_link=http://b2l.me/act4t5&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+2+-+Writing+Data+Reliably&amp;link=http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/&amp;notes=In%20Part%201%2C%20we%20covered%20the%20basic%20concept%20of%20a%20transaction%2C%20what%20the%20ACID%20properties%20are%20and%20how%20they%20specify%20transactional%20behavior%2C%20and%20why%20you%20might%20use%20transactions.%20%20This%20time%2C%20we%27ll%20talk%20more%20specifically%20about%20what%20it%27s%20like%20to%20use%20a%20transaction%2C%20and%20how%20the%20contracts%20it%20provides%20allow%20us%20to%20wr&amp;short_link=http://b2l.me/act4t5&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+2+-+Writing+Data+Reliably&amp;link=http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/&amp;notes=In%20Part%201%2C%20we%20covered%20the%20basic%20concept%20of%20a%20transaction%2C%20what%20the%20ACID%20properties%20are%20and%20how%20they%20specify%20transactional%20behavior%2C%20and%20why%20you%20might%20use%20transactions.%20%20This%20time%2C%20we%27ll%20talk%20more%20specifically%20about%20what%20it%27s%20like%20to%20use%20a%20transaction%2C%20and%20how%20the%20contracts%20it%20provides%20allow%20us%20to%20wr&amp;short_link=http://b2l.me/act4t5&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+2+-+Writing+Data+Reliably&amp;link=http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/&amp;notes=In%20Part%201%2C%20we%20covered%20the%20basic%20concept%20of%20a%20transaction%2C%20what%20the%20ACID%20properties%20are%20and%20how%20they%20specify%20transactional%20behavior%2C%20and%20why%20you%20might%20use%20transactions.%20%20This%20time%2C%20we%27ll%20talk%20more%20specifically%20about%20what%20it%27s%20like%20to%20use%20a%20transaction%2C%20and%20how%20the%20contracts%20it%20provides%20allow%20us%20to%20wr&amp;short_link=http://b2l.me/act4t5&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul><div style="clear: both;"></div></div>

<h3  class="related_post_title">More Related Content</h3><ul class="related_post"><li>No Related Post</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Transactions, Part 1</title>
		<link>http://cwash.org/2010/07/08/transactions-part-1/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=transactions-part-1</link>
		<comments>http://cwash.org/2010/07/08/transactions-part-1/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 14:46:54 +0000</pubDate>
		<dc:creator>Chris Wash</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[ACID properties]]></category>
		<category><![CDATA[CapTech]]></category>
		<category><![CDATA[Enterprise Integration]]></category>
		<category><![CDATA[transactions]]></category>
		<category><![CDATA[Transactions Series]]></category>

		<guid isPermaLink="false">http://cwash.org/?p=460</guid>
		<description><![CDATA[Not only are transactions fundamental building blocks of business software, but also modern distributed systems. Transactions are prevalent in the frameworks and tools we use to build, tune, and scale the software we write.  Transactions in software are important when updating shared resources, like a cache, database, or message queue, but also come into play when reading - especially when access must be coordinated across many separate but concurrent workers reading shared data.  Over the course of this series of articles, we'll talk about what concepts you need to know in order to effectively and correctly make use of transactions in your applications.  This series of articles is written as a set of core concepts to understanding the basics of transactions, and is intended for junior to intermediate level developers, but my hope is that others will benefit from this series as well. ]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://cwash.org/2010/07/08/transactions-part-1/";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><h3>Preface</h3>
<blockquote><p>This work came out of an lack of consistent, cohesive documentation for beginners on transactions.  Similar material does exist, but much of it suffers from either being hard to find, scattered and spotty, or not written at a basic enough level where prerequisite knowledge is not required in order to understand the material covered.  Over the course of this series of articles, we&#8217;ll talk about what concepts you need to know in order to effectively and correctly make use of transactions in your applications.  This series of articles is written as a set of core concepts to understanding the basics of transactions, and is intended for junior to intermediate level developers, but my hope is that others will benefit from this series as well.  As always, feedback is greatly appreciated.</p></blockquote>
<h3>What is a transaction?</h3>
<h4>Etymology</h4>
<p>The word transaction comes from the Latin word transactionem meaning &#8220;an agreement, accomplishment,&#8221; which itself comes from the past participle of the verb transigere, transactus, meaning &#8220;drove or carried through.&#8221;</p>
<h4>Transactions defined</h4>
<p>The word&#8217;s definition is typically related to business or economics, meaning a single business deal, or an exchange of goods between two parties. Most introductions to transactions use an example of debiting one bank account and crediting another.  When we remove funds from one account, we need to make sure that they are deposited into the other.  If something fails (sufficient funds do not exist in the account being debited, or the account being credited has been closed) then both accounts need to be returned to their original state before the exchange began. This is actually a very good example, but how does that apply to software?</p>
<p><span id="more-460"></span></p>
<p>Let&#8217;s back up for a second.  If we focus on what&#8217;s going on, we begin to see there are two distinct activities in play:</p>
<ol>
<li>Grouping a set of multiple changes together that all have been applied successfully, or, in the event of a <em>single</em> failure, that all need to be undone.</li>
<li>Managing (or <em>coordinating</em>) the execution of these changes, when their interactions might interfere with one another.</li>
</ol>
<h4>Examples of Transactional Systems</h4>
<p>Like many other terms in the software lexicon such as &#8220;class&#8221;, &#8220;object&#8221;, &#8220;system&#8221; and &#8220;code&#8221;, the definition of the word &#8220;transaction&#8221; suffers a bit from the word&#8217;s inherent over-abstraction; <!--break-->it is widely applicable to a great many circumstances and situations. But once understood, is easy to spot scenarios where the pattern is used and applied.  Transactions are important in software when updating shared resources, like a cache, database, or message queue, but also come into play when reading &#8211; especially when access must be coordinated across many separate but concurrent workers reading shared data.  We&#8217;ll talk more about this in depth later. Some examples of systems that heavily utilize software transactions are:</p>
<ul>
<li>eCommerce (Shopping-Cart, Billing, Banking) Applications</li>
<li>Relational Databases</li>
<li>Caching Systems</li>
<li>Messaging Systems (Message-Oriented Middleware)</li>
<li>Source Code Management Systems</li>
</ul>
<p>As you can see, not only are these fundamental building blocks of business software, but also modern distributed systems.  Transactions are prevalent in the frameworks and tools we use to build, tune, and scale the software we write.</p>
<h3>Specifying Transactions</h3>
<h4>ACID properties</h4>
<p>A technological definition of the word transaction involves specification of qualities (or &#8220;properties&#8221;) that must be enforced in order for the transaction to be processed successfully.  These properties, the <em>ACID properties</em> as they are known, grow out of our business concept of a transaction (an exchange between two parties).  They exist as conventions that specify or constrain the software that&#8217;s responsible for processing transactions.  Enforcing all four of these properties <em>guarantees</em> the transaction is reliable and therefore viable candidate for successful completion/processing.</p>
<ul>
<li><strong>Atomic</strong> &#8211; When we say transactions need to be atomic, we don&#8217;t mean that they need to be small, though often they are.  What we mean is that <em>you can&#8217;t subdivide a transaction any further</em> &#8211; the group of changes must be viewed as a single set.  When applying that set of changes, it&#8217;s &#8220;all or nothing&#8221; &#8211; that is, each and every individual change processes successfully, or else the entire group is undone completely.  There are no &#8220;partial&#8221; transactions; we can&#8217;t subdivide them further.</li>
<li><strong>Consistent</strong> &#8211; If a system successfully processes a transaction, then all resources involved in the transaction must be consistent afterwords &#8211; whether the transaction processed successfully or not.  For example, if one account is debited and another is credited, the balance of the debited account must be equal to the difference of the original balance and the price of the transaction (and vice-versa for the credited account).  Transactions that do not complete successfully should not have any side-effects to either party involved in the transaction.</li>
<li><strong>Independent</strong> &#8211; Transactions should not interfere with one another, even though they may involve shared resources.  For example, one transaction should not be able to see the work another transaction is currently performing until that transaction has completed.  They should be <em>independent</em> or <em>isolated</em>.  Typically, transaction isolation becomes an issue as transactions being processed become larger: that is, the longer they live or the more resources that they enlist.  Isolation also has performance implications as the amount of load or concurrency increases.  Isolation mechanisms expose either locking or multi-version concurrency control (MVCC, &#8220;diff-ing&#8221; or delta-management).  We will discuss these later when we talk about  <em>concurrency control and isolation</em>.</li>
<li><strong>Durable</strong> &#8211; Systems processing transactions must record successful transactions so that a history of the transactions processed is not lost.  Relevant actions taken are logged for auditing purposes.  This audit trail allows the state of the transactional system to be recreated at any point in time if there is a system failure.  If the information cannot be recorded in this way, we can not say we have a durable transaction, because we won&#8217;t be able to recreate the state of the system if a failure occurs.</li>
</ul>
<p>In a real world business transaction, these conventions are likely enforced by human beings involved in the transaction. If one of these properties were violated during your checkout at a store in the mall, then either you or the clerk would be pretty upset and likely to call in relevant authorities to sort out the matter.  For example, if you bought a shirt but were refused a receipt for the transaction, you would most likely be upset and demand a refund, as the receipt is confirmation of the transaction&#8217;s durability.</p>
<h3>Why Should I Bother Using Transactions?</h3>
<p>As you can see, transactions also provide us with some guarantees about what will happen in the event that something goes wrong during the interactions between shared resources.  These guarantees are very useful to us as software designers and developers, but their benefits may not be intuitively obvious until we consider what our jobs would be like without them.  Transactions give us with a safety net of assumptions; when something goes wrong, we can lean on the transaction&#8217;s guarantee to rollback, or automatically revert all changes from the start of the unit of work.  Imagine if we didn&#8217;t have this guarantee: we would be forced to write code to cleanup the mess we&#8217;ve created, and not only is this code hard to maintain, it&#8217;s hard to write and get correct the first time.  With transactions, we don&#8217;t have to worry about writing extra code to &#8220;cleanup&#8221; the mess we&#8217;ve created, and can instead focus on what the code we&#8217;re writing is actually supposed to do: the business logic.  When something goes wrong, we can safely assume certain scenarios are out of the question by using transactions.  In many circumstances, transactions are key to writing code that is functionally correct, and most of the time make the code cleaner, more robust and maintainable (especially when considering the alternative of coding cleanup logic by hand).</p>
<p>Transactions are fundamentally all about <strong>coordinating access to shared resources</strong>.  A term that can be used interchangeably with the word &#8220;transaction&#8221; is <strong>unit of work.</strong> Transactions are important to software systems because they&#8217;re how we manage interactions with the different systems involved that have access or expose shared resources to clients.</p>
<p>Typically when we work with transactional systems, as application developers we:</p>
<ul>
<li>Write code that explicitly utilizes a transactional API, like JDBC, that will contain the semantics for how to handle the processing of the transaction, <em>or&#8230;</em></li>
<li>Configure a transaction management mechanism, like an EJB container, that can do the processing and coordination work on our behalf based on conventions.</li>
</ul>
<p>The next article in the series will provide examples of both approaches.  Either way, the end result provides a way to keep each unit of work&#8217;s interactions separate from each other, and guarantee that all changes in the set are applied together or undone completely.</p>
<h3>Up Next: <a href="http://cwash.org/2010/07/26/transactions-part-2-writing-data-reliably/">Writing Data Reliably</a></h3>
<ul>
<li>Programming With Transactional Systems</li>
<li>Declarative Transaction Management</li>
</ul>
<h3>Further Reading</h3>
<ul>
<li>http://www.etymonline.com/index.php?term=transaction</li>
<li>http://en.wikipedia.org/wiki/ACID</li>
</ul>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://cwash.org/2010/07/08/transactions-part-1/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+1&amp;link=http://cwash.org/2010/07/08/transactions-part-1/&amp;notes=Not%20only%20are%20transactions%20fundamental%20building%20blocks%20of%20business%20software%2C%20but%20also%20modern%20distributed%20systems.%20Transactions%20are%20prevalent%20in%20the%20frameworks%20and%20tools%20we%20use%20to%20build%2C%20tune%2C%20and%20scale%20the%20software%20we%20write.%20%20Transactions%20in%20software%20are%20important%20when%20updating%20shared%20resources%2C%20like%20a%20cache%2C%20database%2C%20or%20message%20queue%2C%20but%20also%20come%20into%20play%20when%20reading%20-%20especially%20when%20access%20must%20be%20coordinated%20across%20many%20separate%20but%20concurrent%20workers%20reading%20shared%20data.%20%20Over%20the%20course%20of%20this%20series%20of%20articles%2C%20we%27ll%20talk%20about%20what%20concepts%20you%20need%20to%20know%20in%20order%20to%20effectively%20and%20correctly%20make%20use%20of%20transactions%20in%20your%20applications.%20%20This%20series%20of%20articles%20is%20written%20as%20a%20set%20of%20core%20concepts%20to%20understanding%20the%20basics%20of%20transactions%2C%20and%20is%20intended%20for%20junior%20to%20intermediate%20level%20developers%2C%20but%20my%20hope%20is%20that%20others%20will%20benefit%20from%20this%20series%20as%20well.%20&amp;short_link=http://b2l.me/9qk38&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+1&amp;link=http://cwash.org/2010/07/08/transactions-part-1/&amp;notes=Not%20only%20are%20transactions%20fundamental%20building%20blocks%20of%20business%20software%2C%20but%20also%20modern%20distributed%20systems.%20Transactions%20are%20prevalent%20in%20the%20frameworks%20and%20tools%20we%20use%20to%20build%2C%20tune%2C%20and%20scale%20the%20software%20we%20write.%20%20Transactions%20in%20software%20are%20important%20when%20updating%20shared%20resources%2C%20like%20a%20cache%2C%20database%2C%20or%20message%20queue%2C%20but%20also%20come%20into%20play%20when%20reading%20-%20especially%20when%20access%20must%20be%20coordinated%20across%20many%20separate%20but%20concurrent%20workers%20reading%20shared%20data.%20%20Over%20the%20course%20of%20this%20series%20of%20articles%2C%20we%27ll%20talk%20about%20what%20concepts%20you%20need%20to%20know%20in%20order%20to%20effectively%20and%20correctly%20make%20use%20of%20transactions%20in%20your%20applications.%20%20This%20series%20of%20articles%20is%20written%20as%20a%20set%20of%20core%20concepts%20to%20understanding%20the%20basics%20of%20transactions%2C%20and%20is%20intended%20for%20junior%20to%20intermediate%20level%20developers%2C%20but%20my%20hope%20is%20that%20others%20will%20benefit%20from%20this%20series%20as%20well.%20&amp;short_link=http://b2l.me/9qk38&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+1&amp;link=http://cwash.org/2010/07/08/transactions-part-1/&amp;notes=Not%20only%20are%20transactions%20fundamental%20building%20blocks%20of%20business%20software%2C%20but%20also%20modern%20distributed%20systems.%20Transactions%20are%20prevalent%20in%20the%20frameworks%20and%20tools%20we%20use%20to%20build%2C%20tune%2C%20and%20scale%20the%20software%20we%20write.%20%20Transactions%20in%20software%20are%20important%20when%20updating%20shared%20resources%2C%20like%20a%20cache%2C%20database%2C%20or%20message%20queue%2C%20but%20also%20come%20into%20play%20when%20reading%20-%20especially%20when%20access%20must%20be%20coordinated%20across%20many%20separate%20but%20concurrent%20workers%20reading%20shared%20data.%20%20Over%20the%20course%20of%20this%20series%20of%20articles%2C%20we%27ll%20talk%20about%20what%20concepts%20you%20need%20to%20know%20in%20order%20to%20effectively%20and%20correctly%20make%20use%20of%20transactions%20in%20your%20applications.%20%20This%20series%20of%20articles%20is%20written%20as%20a%20set%20of%20core%20concepts%20to%20understanding%20the%20basics%20of%20transactions%2C%20and%20is%20intended%20for%20junior%20to%20intermediate%20level%20developers%2C%20but%20my%20hope%20is%20that%20others%20will%20benefit%20from%20this%20series%20as%20well.%20&amp;short_link=http://b2l.me/9qk38&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=24&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+1&amp;link=http://cwash.org/2010/07/08/transactions-part-1/&amp;notes=Not%20only%20are%20transactions%20fundamental%20building%20blocks%20of%20business%20software%2C%20but%20also%20modern%20distributed%20systems.%20Transactions%20are%20prevalent%20in%20the%20frameworks%20and%20tools%20we%20use%20to%20build%2C%20tune%2C%20and%20scale%20the%20software%20we%20write.%20%20Transactions%20in%20software%20are%20important%20when%20updating%20shared%20resources%2C%20like%20a%20cache%2C%20database%2C%20or%20message%20queue%2C%20but%20also%20come%20into%20play%20when%20reading%20-%20especially%20when%20access%20must%20be%20coordinated%20across%20many%20separate%20but%20concurrent%20workers%20reading%20shared%20data.%20%20Over%20the%20course%20of%20this%20series%20of%20articles%2C%20we%27ll%20talk%20about%20what%20concepts%20you%20need%20to%20know%20in%20order%20to%20effectively%20and%20correctly%20make%20use%20of%20transactions%20in%20your%20applications.%20%20This%20series%20of%20articles%20is%20written%20as%20a%20set%20of%20core%20concepts%20to%20understanding%20the%20basics%20of%20transactions%2C%20and%20is%20intended%20for%20junior%20to%20intermediate%20level%20developers%2C%20but%20my%20hope%20is%20that%20others%20will%20benefit%20from%20this%20series%20as%20well.%20&amp;short_link=http://b2l.me/9qk38&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-gmail">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+1&amp;link=http://cwash.org/2010/07/08/transactions-part-1/&amp;notes=Not%20only%20are%20transactions%20fundamental%20building%20blocks%20of%20business%20software%2C%20but%20also%20modern%20distributed%20systems.%20Transactions%20are%20prevalent%20in%20the%20frameworks%20and%20tools%20we%20use%20to%20build%2C%20tune%2C%20and%20scale%20the%20software%20we%20write.%20%20Transactions%20in%20software%20are%20important%20when%20updating%20shared%20resources%2C%20like%20a%20cache%2C%20database%2C%20or%20message%20queue%2C%20but%20also%20come%20into%20play%20when%20reading%20-%20especially%20when%20access%20must%20be%20coordinated%20across%20many%20separate%20but%20concurrent%20workers%20reading%20shared%20data.%20%20Over%20the%20course%20of%20this%20series%20of%20articles%2C%20we%27ll%20talk%20about%20what%20concepts%20you%20need%20to%20know%20in%20order%20to%20effectively%20and%20correctly%20make%20use%20of%20transactions%20in%20your%20applications.%20%20This%20series%20of%20articles%20is%20written%20as%20a%20set%20of%20core%20concepts%20to%20understanding%20the%20basics%20of%20transactions%2C%20and%20is%20intended%20for%20junior%20to%20intermediate%20level%20developers%2C%20but%20my%20hope%20is%20that%20others%20will%20benefit%20from%20this%20series%20as%20well.%20&amp;short_link=http://b2l.me/9qk38&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=52&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Email this via Gmail">Email this via Gmail</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+1&amp;link=http://cwash.org/2010/07/08/transactions-part-1/&amp;notes=Not%20only%20are%20transactions%20fundamental%20building%20blocks%20of%20business%20software%2C%20but%20also%20modern%20distributed%20systems.%20Transactions%20are%20prevalent%20in%20the%20frameworks%20and%20tools%20we%20use%20to%20build%2C%20tune%2C%20and%20scale%20the%20software%20we%20write.%20%20Transactions%20in%20software%20are%20important%20when%20updating%20shared%20resources%2C%20like%20a%20cache%2C%20database%2C%20or%20message%20queue%2C%20but%20also%20come%20into%20play%20when%20reading%20-%20especially%20when%20access%20must%20be%20coordinated%20across%20many%20separate%20but%20concurrent%20workers%20reading%20shared%20data.%20%20Over%20the%20course%20of%20this%20series%20of%20articles%2C%20we%27ll%20talk%20about%20what%20concepts%20you%20need%20to%20know%20in%20order%20to%20effectively%20and%20correctly%20make%20use%20of%20transactions%20in%20your%20applications.%20%20This%20series%20of%20articles%20is%20written%20as%20a%20set%20of%20core%20concepts%20to%20understanding%20the%20basics%20of%20transactions%2C%20and%20is%20intended%20for%20junior%20to%20intermediate%20level%20developers%2C%20but%20my%20hope%20is%20that%20others%20will%20benefit%20from%20this%20series%20as%20well.%20&amp;short_link=http://b2l.me/9qk38&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=74&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+1&amp;link=http://cwash.org/2010/07/08/transactions-part-1/&amp;notes=Not%20only%20are%20transactions%20fundamental%20building%20blocks%20of%20business%20software%2C%20but%20also%20modern%20distributed%20systems.%20Transactions%20are%20prevalent%20in%20the%20frameworks%20and%20tools%20we%20use%20to%20build%2C%20tune%2C%20and%20scale%20the%20software%20we%20write.%20%20Transactions%20in%20software%20are%20important%20when%20updating%20shared%20resources%2C%20like%20a%20cache%2C%20database%2C%20or%20message%20queue%2C%20but%20also%20come%20into%20play%20when%20reading%20-%20especially%20when%20access%20must%20be%20coordinated%20across%20many%20separate%20but%20concurrent%20workers%20reading%20shared%20data.%20%20Over%20the%20course%20of%20this%20series%20of%20articles%2C%20we%27ll%20talk%20about%20what%20concepts%20you%20need%20to%20know%20in%20order%20to%20effectively%20and%20correctly%20make%20use%20of%20transactions%20in%20your%20applications.%20%20This%20series%20of%20articles%20is%20written%20as%20a%20set%20of%20core%20concepts%20to%20understanding%20the%20basics%20of%20transactions%2C%20and%20is%20intended%20for%20junior%20to%20intermediate%20level%20developers%2C%20but%20my%20hope%20is%20that%20others%20will%20benefit%20from%20this%20series%20as%20well.%20&amp;short_link=http://b2l.me/9qk38&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+1&amp;link=http://cwash.org/2010/07/08/transactions-part-1/&amp;notes=Not%20only%20are%20transactions%20fundamental%20building%20blocks%20of%20business%20software%2C%20but%20also%20modern%20distributed%20systems.%20Transactions%20are%20prevalent%20in%20the%20frameworks%20and%20tools%20we%20use%20to%20build%2C%20tune%2C%20and%20scale%20the%20software%20we%20write.%20%20Transactions%20in%20software%20are%20important%20when%20updating%20shared%20resources%2C%20like%20a%20cache%2C%20database%2C%20or%20message%20queue%2C%20but%20also%20come%20into%20play%20when%20reading%20-%20especially%20when%20access%20must%20be%20coordinated%20across%20many%20separate%20but%20concurrent%20workers%20reading%20shared%20data.%20%20Over%20the%20course%20of%20this%20series%20of%20articles%2C%20we%27ll%20talk%20about%20what%20concepts%20you%20need%20to%20know%20in%20order%20to%20effectively%20and%20correctly%20make%20use%20of%20transactions%20in%20your%20applications.%20%20This%20series%20of%20articles%20is%20written%20as%20a%20set%20of%20core%20concepts%20to%20understanding%20the%20basics%20of%20transactions%2C%20and%20is%20intended%20for%20junior%20to%20intermediate%20level%20developers%2C%20but%20my%20hope%20is%20that%20others%20will%20benefit%20from%20this%20series%20as%20well.%20&amp;short_link=http://b2l.me/9qk38&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=207&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+1&amp;link=http://cwash.org/2010/07/08/transactions-part-1/&amp;notes=Not%20only%20are%20transactions%20fundamental%20building%20blocks%20of%20business%20software%2C%20but%20also%20modern%20distributed%20systems.%20Transactions%20are%20prevalent%20in%20the%20frameworks%20and%20tools%20we%20use%20to%20build%2C%20tune%2C%20and%20scale%20the%20software%20we%20write.%20%20Transactions%20in%20software%20are%20important%20when%20updating%20shared%20resources%2C%20like%20a%20cache%2C%20database%2C%20or%20message%20queue%2C%20but%20also%20come%20into%20play%20when%20reading%20-%20especially%20when%20access%20must%20be%20coordinated%20across%20many%20separate%20but%20concurrent%20workers%20reading%20shared%20data.%20%20Over%20the%20course%20of%20this%20series%20of%20articles%2C%20we%27ll%20talk%20about%20what%20concepts%20you%20need%20to%20know%20in%20order%20to%20effectively%20and%20correctly%20make%20use%20of%20transactions%20in%20your%20applications.%20%20This%20series%20of%20articles%20is%20written%20as%20a%20set%20of%20core%20concepts%20to%20understanding%20the%20basics%20of%20transactions%2C%20and%20is%20intended%20for%20junior%20to%20intermediate%20level%20developers%2C%20but%20my%20hope%20is%20that%20others%20will%20benefit%20from%20this%20series%20as%20well.%20&amp;short_link=http://b2l.me/9qk38&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+1&amp;link=http://cwash.org/2010/07/08/transactions-part-1/&amp;notes=Not%20only%20are%20transactions%20fundamental%20building%20blocks%20of%20business%20software%2C%20but%20also%20modern%20distributed%20systems.%20Transactions%20are%20prevalent%20in%20the%20frameworks%20and%20tools%20we%20use%20to%20build%2C%20tune%2C%20and%20scale%20the%20software%20we%20write.%20%20Transactions%20in%20software%20are%20important%20when%20updating%20shared%20resources%2C%20like%20a%20cache%2C%20database%2C%20or%20message%20queue%2C%20but%20also%20come%20into%20play%20when%20reading%20-%20especially%20when%20access%20must%20be%20coordinated%20across%20many%20separate%20but%20concurrent%20workers%20reading%20shared%20data.%20%20Over%20the%20course%20of%20this%20series%20of%20articles%2C%20we%27ll%20talk%20about%20what%20concepts%20you%20need%20to%20know%20in%20order%20to%20effectively%20and%20correctly%20make%20use%20of%20transactions%20in%20your%20applications.%20%20This%20series%20of%20articles%20is%20written%20as%20a%20set%20of%20core%20concepts%20to%20understanding%20the%20basics%20of%20transactions%2C%20and%20is%20intended%20for%20junior%20to%20intermediate%20level%20developers%2C%20but%20my%20hope%20is%20that%20others%20will%20benefit%20from%20this%20series%20as%20well.%20&amp;short_link=http://b2l.me/9qk38&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=6&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+1&amp;link=http://cwash.org/2010/07/08/transactions-part-1/&amp;notes=Not%20only%20are%20transactions%20fundamental%20building%20blocks%20of%20business%20software%2C%20but%20also%20modern%20distributed%20systems.%20Transactions%20are%20prevalent%20in%20the%20frameworks%20and%20tools%20we%20use%20to%20build%2C%20tune%2C%20and%20scale%20the%20software%20we%20write.%20%20Transactions%20in%20software%20are%20important%20when%20updating%20shared%20resources%2C%20like%20a%20cache%2C%20database%2C%20or%20message%20queue%2C%20but%20also%20come%20into%20play%20when%20reading%20-%20especially%20when%20access%20must%20be%20coordinated%20across%20many%20separate%20but%20concurrent%20workers%20reading%20shared%20data.%20%20Over%20the%20course%20of%20this%20series%20of%20articles%2C%20we%27ll%20talk%20about%20what%20concepts%20you%20need%20to%20know%20in%20order%20to%20effectively%20and%20correctly%20make%20use%20of%20transactions%20in%20your%20applications.%20%20This%20series%20of%20articles%20is%20written%20as%20a%20set%20of%20core%20concepts%20to%20understanding%20the%20basics%20of%20transactions%2C%20and%20is%20intended%20for%20junior%20to%20intermediate%20level%20developers%2C%20but%20my%20hope%20is%20that%20others%20will%20benefit%20from%20this%20series%20as%20well.%20&amp;short_link=http://b2l.me/9qk38&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=4&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+1&amp;link=http://cwash.org/2010/07/08/transactions-part-1/&amp;notes=Not%20only%20are%20transactions%20fundamental%20building%20blocks%20of%20business%20software%2C%20but%20also%20modern%20distributed%20systems.%20Transactions%20are%20prevalent%20in%20the%20frameworks%20and%20tools%20we%20use%20to%20build%2C%20tune%2C%20and%20scale%20the%20software%20we%20write.%20%20Transactions%20in%20software%20are%20important%20when%20updating%20shared%20resources%2C%20like%20a%20cache%2C%20database%2C%20or%20message%20queue%2C%20but%20also%20come%20into%20play%20when%20reading%20-%20especially%20when%20access%20must%20be%20coordinated%20across%20many%20separate%20but%20concurrent%20workers%20reading%20shared%20data.%20%20Over%20the%20course%20of%20this%20series%20of%20articles%2C%20we%27ll%20talk%20about%20what%20concepts%20you%20need%20to%20know%20in%20order%20to%20effectively%20and%20correctly%20make%20use%20of%20transactions%20in%20your%20applications.%20%20This%20series%20of%20articles%20is%20written%20as%20a%20set%20of%20core%20concepts%20to%20understanding%20the%20basics%20of%20transactions%2C%20and%20is%20intended%20for%20junior%20to%20intermediate%20level%20developers%2C%20but%20my%20hope%20is%20that%20others%20will%20benefit%20from%20this%20series%20as%20well.%20&amp;short_link=http://b2l.me/9qk38&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+1&amp;link=http://cwash.org/2010/07/08/transactions-part-1/&amp;notes=Not%20only%20are%20transactions%20fundamental%20building%20blocks%20of%20business%20software%2C%20but%20also%20modern%20distributed%20systems.%20Transactions%20are%20prevalent%20in%20the%20frameworks%20and%20tools%20we%20use%20to%20build%2C%20tune%2C%20and%20scale%20the%20software%20we%20write.%20%20Transactions%20in%20software%20are%20important%20when%20updating%20shared%20resources%2C%20like%20a%20cache%2C%20database%2C%20or%20message%20queue%2C%20but%20also%20come%20into%20play%20when%20reading%20-%20especially%20when%20access%20must%20be%20coordinated%20across%20many%20separate%20but%20concurrent%20workers%20reading%20shared%20data.%20%20Over%20the%20course%20of%20this%20series%20of%20articles%2C%20we%27ll%20talk%20about%20what%20concepts%20you%20need%20to%20know%20in%20order%20to%20effectively%20and%20correctly%20make%20use%20of%20transactions%20in%20your%20applications.%20%20This%20series%20of%20articles%20is%20written%20as%20a%20set%20of%20core%20concepts%20to%20understanding%20the%20basics%20of%20transactions%2C%20and%20is%20intended%20for%20junior%20to%20intermediate%20level%20developers%2C%20but%20my%20hope%20is%20that%20others%20will%20benefit%20from%20this%20series%20as%20well.%20&amp;short_link=http://b2l.me/9qk38&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+1&amp;link=http://cwash.org/2010/07/08/transactions-part-1/&amp;notes=Not%20only%20are%20transactions%20fundamental%20building%20blocks%20of%20business%20software%2C%20but%20also%20modern%20distributed%20systems.%20Transactions%20are%20prevalent%20in%20the%20frameworks%20and%20tools%20we%20use%20to%20build%2C%20tune%2C%20and%20scale%20the%20software%20we%20write.%20%20Transactions%20in%20software%20are%20important%20when%20updating%20shared%20resources%2C%20like%20a%20cache%2C%20database%2C%20or%20message%20queue%2C%20but%20also%20come%20into%20play%20when%20reading%20-%20especially%20when%20access%20must%20be%20coordinated%20across%20many%20separate%20but%20concurrent%20workers%20reading%20shared%20data.%20%20Over%20the%20course%20of%20this%20series%20of%20articles%2C%20we%27ll%20talk%20about%20what%20concepts%20you%20need%20to%20know%20in%20order%20to%20effectively%20and%20correctly%20make%20use%20of%20transactions%20in%20your%20applications.%20%20This%20series%20of%20articles%20is%20written%20as%20a%20set%20of%20core%20concepts%20to%20understanding%20the%20basics%20of%20transactions%2C%20and%20is%20intended%20for%20junior%20to%20intermediate%20level%20developers%2C%20but%20my%20hope%20is%20that%20others%20will%20benefit%20from%20this%20series%20as%20well.%20&amp;short_link=http://b2l.me/9qk38&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Transactions%2C+Part+1&amp;link=http://cwash.org/2010/07/08/transactions-part-1/&amp;notes=Not%20only%20are%20transactions%20fundamental%20building%20blocks%20of%20business%20software%2C%20but%20also%20modern%20distributed%20systems.%20Transactions%20are%20prevalent%20in%20the%20frameworks%20and%20tools%20we%20use%20to%20build%2C%20tune%2C%20and%20scale%20the%20software%20we%20write.%20%20Transactions%20in%20software%20are%20important%20when%20updating%20shared%20resources%2C%20like%20a%20cache%2C%20database%2C%20or%20message%20queue%2C%20but%20also%20come%20into%20play%20when%20reading%20-%20especially%20when%20access%20must%20be%20coordinated%20across%20many%20separate%20but%20concurrent%20workers%20reading%20shared%20data.%20%20Over%20the%20course%20of%20this%20series%20of%20articles%2C%20we%27ll%20talk%20about%20what%20concepts%20you%20need%20to%20know%20in%20order%20to%20effectively%20and%20correctly%20make%20use%20of%20transactions%20in%20your%20applications.%20%20This%20series%20of%20articles%20is%20written%20as%20a%20set%20of%20core%20concepts%20to%20understanding%20the%20basics%20of%20transactions%2C%20and%20is%20intended%20for%20junior%20to%20intermediate%20level%20developers%2C%20but%20my%20hope%20is%20that%20others%20will%20benefit%20from%20this%20series%20as%20well.%20&amp;short_link=http://b2l.me/9qk38&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul><div style="clear: both;"></div></div>

<h3  class="related_post_title">More Related Content</h3><ul class="related_post"><li>January 7, 2010 -- <a href="http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/" title="Eliminate Branching (IF Statements) to Produce Better Code">Eliminate Branching (IF Statements) to Produce Better Code</a> (0)</li><li>July 24, 2009 -- <a href="http://cwash.org/2009/07/24/the-elements-of-reusable-code/" title="The Elements of Reusable Code">The Elements of Reusable Code</a> (0)</li><li>July 29, 2009 -- <a href="http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/" title="What I&#8217;d Tell Myself About Design If I Were Just Beginning">What I&#8217;d Tell Myself About Design If I Were Just Beginning</a> (5)</li><li>June 9, 2009 -- <a href="http://cwash.org/2009/06/09/mocking-with-jmockit/" title="Mocking with JMockit">Mocking with JMockit</a> (5)</li><li>June 3, 2009 -- <a href="http://cwash.org/2009/06/03/what-is-hamcrest/" title="What is Hamcrest?">What is Hamcrest?</a> (0)</li><li>April 15, 2009 -- <a href="http://cwash.org/2009/04/15/osgi-ggity-giggity/" title="OSGi-ggity-Giggity">OSGi-ggity-Giggity</a> (4)</li><li>January 31, 2009 -- <a href="http://cwash.org/2009/01/31/in-response-to-stackoverflow-38quality-doesnt-matter-that-much-jeff-and-joel/" title="In response to Stackoverflow #38/&#8221;Quality Doesn&#8217;t Matter That Much&#8221; &#8212; Jeff and Joel ">In response to Stackoverflow #38/&#8221;Quality Doesn&#8217;t Matter That Much&#8221; &#8212; Jeff and Joel </a> (3)</li><li>January 13, 2009 -- <a href="http://cwash.org/2009/01/13/on-software-quality/" title="On Software Quality">On Software Quality</a> (8)</li><li>November 28, 2008 -- <a href="http://cwash.org/2008/11/28/must-havesreferences-for-modern-java-ee-developers/" title="Must Haves/References For Modern Java EE Developers">Must Haves/References For Modern Java EE Developers</a> (1)</li><li>November 19, 2008 -- <a href="http://cwash.org/2008/11/19/java-6-and-maven-209-on-leopard/" title="Java 6 and Maven 2.0.9 on Leopard">Java 6 and Maven 2.0.9 on Leopard</a> (7)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cwash.org/2010/07/08/transactions-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pairing Lessons #1: Be cynical with code</title>
		<link>http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=pairing-lessons-1-be-cynical-with-code</link>
		<comments>http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 23:55:22 +0000</pubDate>
		<dc:creator>Chris Wash</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cwash.org/?p=448</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/";</script>The following scenario plays out a lot when I pair up with people, so I thought I&#8217;d distill it down and write it up for future reference. I think teaching lessons through scenarios could make for an interesting series of articles, and I&#8217;m sure others have equally interesting experiences to share. I&#8217;ve tried to condense [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><p>The following scenario plays out a lot when I pair up with people, so I thought I&#8217;d distill it down and write it up for future reference.  I think teaching lessons through scenarios could make for an interesting series of articles, and I&#8217;m sure others have equally interesting experiences to share.</p>
<p>I&#8217;ve tried to <em>condense</em> this down to just the steps vital to understanding the underlying problem (though this is certainly not the only way to get there) and <em>introduce</em> some terminology (providing links, even if deferring to google) along the way.  I hope these help out aspiring or new developers.</p>
<p><span id="more-448"></span></p>
<p>Here&#8217;s a scenario:  you find yourself doing X all over the place, (where X is nearly anything) and you&#8217;re just plain tired of writing the code to do it.  You first reluctantly copy and paste it into new places, and twiddle what you need to twiddle to get it to work.  You soon realize this is a pain because it&#8217;s error prone &#8211; you can mess things up using copy and paste.  You fancy yourself the pragmatic type and decide to write a IDE template that will fill in the code for you instead of having to write it out every time. </p>
<p>(Aside: This is but one of many kinds of code generation).  </p>
<blockquote><p>If you are using code generation, you stopped asking &#8220;why&#8221; too soon. -<a href="http://bit.ly/cfdg0P">mahvne</a></p></blockquote>
<p>This helps with the problem of introducing errors, but you soon you realize this code is everywhere, cluttering up other code.  Not only that, when the structure of this code changes a little bit, you need to go and change it everywhere it was copied and pasted, or generated using your template.</p>
<p>(Aside: It&#8217;s a violation of the <a href="http://www.google.com/search?q=dry+principle">DRY principle</a>.)  </p>
<p>Now you decide the best course of action is to try get this code to a common place that&#8217;s visible across every part of your project.  You also have to go find every place you copied and pasted or used your template and replace them with calls to your method that use generic parameters.  </p>
<p>(Aside: You&#8217;ve just created a <a href="http://www.google.com/search?q=three+strike+refactoring">static utility method</a>.)  </p>
<p>You make up your mind that in the future, it will be much easier to do this before the code gets copied and pasted that many times, and vow to follow the <a href="http://www.google.com/search?q=three+strike+refactoring">three-strike rule of refactoring</a>.  </p>
<p>(Aside: And use automated <a href="http://www.google.com/search?q=extract+method+refactoring">extract method refactoring</a> and <a href="http://www.google.com/search?q=move+method+refactoring">move method refactoring</a> to do this).</p>
<p>The lesson to take away is this: any line of code you introduce creates change points and increases complexity.  We need to make sure the lines of code allow into our code base are well-factored/designed/tested and worthy of our ongoing maintenance efforts.  Really &#8211; try to break your code before it leaves your hands.  Fix it, or get rid of it.  While this might seem cynical at first, you&#8217;ll understand it&#8217;s just preventative maintenance, or programming deliberately (not <a href="http://www.pragprog.com/the-pragmatic-programmer/extracts/coincidence">programming by coincidence</a>).</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://www.shareaholic.com/api/share/?title=Pairing+Lessons+%231%3A+Be+cynical+with+code+&amp;link=http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/&amp;notes=The%20following%20scenario%20plays%20out%20a%20lot%20when%20I%20pair%20up%20with%20people%2C%20so%20I%20thought%20I%27d%20distill%20it%20down%20and%20write%20it%20up%20for%20future%20reference.%20%20I%20think%20teaching%20lessons%20through%20scenarios%20could%20make%20for%20an%20interesting%20series%20of%20articles%2C%20and%20I%27m%20sure%20others%20have%20equally%20interesting%20experiences%20to%20share.%0D%0A&amp;short_link=http://bit.ly/bpn1ZV&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=Pairing+Lessons+%231%3A+Be+cynical+with+code+&amp;link=http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/&amp;notes=The%20following%20scenario%20plays%20out%20a%20lot%20when%20I%20pair%20up%20with%20people%2C%20so%20I%20thought%20I%27d%20distill%20it%20down%20and%20write%20it%20up%20for%20future%20reference.%20%20I%20think%20teaching%20lessons%20through%20scenarios%20could%20make%20for%20an%20interesting%20series%20of%20articles%2C%20and%20I%27m%20sure%20others%20have%20equally%20interesting%20experiences%20to%20share.%0D%0A&amp;short_link=http://bit.ly/bpn1ZV&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.shareaholic.com/api/share/?title=Pairing+Lessons+%231%3A+Be+cynical+with+code+&amp;link=http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/&amp;notes=The%20following%20scenario%20plays%20out%20a%20lot%20when%20I%20pair%20up%20with%20people%2C%20so%20I%20thought%20I%27d%20distill%20it%20down%20and%20write%20it%20up%20for%20future%20reference.%20%20I%20think%20teaching%20lessons%20through%20scenarios%20could%20make%20for%20an%20interesting%20series%20of%20articles%2C%20and%20I%27m%20sure%20others%20have%20equally%20interesting%20experiences%20to%20share.%0D%0A&amp;short_link=http://bit.ly/bpn1ZV&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=24&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=Pairing+Lessons+%231%3A+Be+cynical+with+code+&amp;link=http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/&amp;notes=The%20following%20scenario%20plays%20out%20a%20lot%20when%20I%20pair%20up%20with%20people%2C%20so%20I%20thought%20I%27d%20distill%20it%20down%20and%20write%20it%20up%20for%20future%20reference.%20%20I%20think%20teaching%20lessons%20through%20scenarios%20could%20make%20for%20an%20interesting%20series%20of%20articles%2C%20and%20I%27m%20sure%20others%20have%20equally%20interesting%20experiences%20to%20share.%0D%0A&amp;short_link=http://bit.ly/bpn1ZV&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-gmail">
			<a href="http://www.shareaholic.com/api/share/?title=Pairing+Lessons+%231%3A+Be+cynical+with+code+&amp;link=http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/&amp;notes=The%20following%20scenario%20plays%20out%20a%20lot%20when%20I%20pair%20up%20with%20people%2C%20so%20I%20thought%20I%27d%20distill%20it%20down%20and%20write%20it%20up%20for%20future%20reference.%20%20I%20think%20teaching%20lessons%20through%20scenarios%20could%20make%20for%20an%20interesting%20series%20of%20articles%2C%20and%20I%27m%20sure%20others%20have%20equally%20interesting%20experiences%20to%20share.%0D%0A&amp;short_link=http://bit.ly/bpn1ZV&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=52&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Email this via Gmail">Email this via Gmail</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.shareaholic.com/api/share/?title=Pairing+Lessons+%231%3A+Be+cynical+with+code+&amp;link=http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/&amp;notes=The%20following%20scenario%20plays%20out%20a%20lot%20when%20I%20pair%20up%20with%20people%2C%20so%20I%20thought%20I%27d%20distill%20it%20down%20and%20write%20it%20up%20for%20future%20reference.%20%20I%20think%20teaching%20lessons%20through%20scenarios%20could%20make%20for%20an%20interesting%20series%20of%20articles%2C%20and%20I%27m%20sure%20others%20have%20equally%20interesting%20experiences%20to%20share.%0D%0A&amp;short_link=http://bit.ly/bpn1ZV&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=74&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.shareaholic.com/api/share/?title=Pairing+Lessons+%231%3A+Be+cynical+with+code+&amp;link=http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/&amp;notes=The%20following%20scenario%20plays%20out%20a%20lot%20when%20I%20pair%20up%20with%20people%2C%20so%20I%20thought%20I%27d%20distill%20it%20down%20and%20write%20it%20up%20for%20future%20reference.%20%20I%20think%20teaching%20lessons%20through%20scenarios%20could%20make%20for%20an%20interesting%20series%20of%20articles%2C%20and%20I%27m%20sure%20others%20have%20equally%20interesting%20experiences%20to%20share.%0D%0A&amp;short_link=http://bit.ly/bpn1ZV&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.shareaholic.com/api/share/?title=Pairing+Lessons+%231%3A+Be+cynical+with+code+&amp;link=http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/&amp;notes=The%20following%20scenario%20plays%20out%20a%20lot%20when%20I%20pair%20up%20with%20people%2C%20so%20I%20thought%20I%27d%20distill%20it%20down%20and%20write%20it%20up%20for%20future%20reference.%20%20I%20think%20teaching%20lessons%20through%20scenarios%20could%20make%20for%20an%20interesting%20series%20of%20articles%2C%20and%20I%27m%20sure%20others%20have%20equally%20interesting%20experiences%20to%20share.%0D%0A&amp;short_link=http://bit.ly/bpn1ZV&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=207&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=Pairing+Lessons+%231%3A+Be+cynical+with+code+&amp;link=http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/&amp;notes=The%20following%20scenario%20plays%20out%20a%20lot%20when%20I%20pair%20up%20with%20people%2C%20so%20I%20thought%20I%27d%20distill%20it%20down%20and%20write%20it%20up%20for%20future%20reference.%20%20I%20think%20teaching%20lessons%20through%20scenarios%20could%20make%20for%20an%20interesting%20series%20of%20articles%2C%20and%20I%27m%20sure%20others%20have%20equally%20interesting%20experiences%20to%20share.%0D%0A&amp;short_link=http://bit.ly/bpn1ZV&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.shareaholic.com/api/share/?title=Pairing+Lessons+%231%3A+Be+cynical+with+code+&amp;link=http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/&amp;notes=The%20following%20scenario%20plays%20out%20a%20lot%20when%20I%20pair%20up%20with%20people%2C%20so%20I%20thought%20I%27d%20distill%20it%20down%20and%20write%20it%20up%20for%20future%20reference.%20%20I%20think%20teaching%20lessons%20through%20scenarios%20could%20make%20for%20an%20interesting%20series%20of%20articles%2C%20and%20I%27m%20sure%20others%20have%20equally%20interesting%20experiences%20to%20share.%0D%0A&amp;short_link=http://bit.ly/bpn1ZV&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=6&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.shareaholic.com/api/share/?title=Pairing+Lessons+%231%3A+Be+cynical+with+code+&amp;link=http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/&amp;notes=The%20following%20scenario%20plays%20out%20a%20lot%20when%20I%20pair%20up%20with%20people%2C%20so%20I%20thought%20I%27d%20distill%20it%20down%20and%20write%20it%20up%20for%20future%20reference.%20%20I%20think%20teaching%20lessons%20through%20scenarios%20could%20make%20for%20an%20interesting%20series%20of%20articles%2C%20and%20I%27m%20sure%20others%20have%20equally%20interesting%20experiences%20to%20share.%0D%0A&amp;short_link=http://bit.ly/bpn1ZV&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=4&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=Pairing+Lessons+%231%3A+Be+cynical+with+code+&amp;link=http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/&amp;notes=The%20following%20scenario%20plays%20out%20a%20lot%20when%20I%20pair%20up%20with%20people%2C%20so%20I%20thought%20I%27d%20distill%20it%20down%20and%20write%20it%20up%20for%20future%20reference.%20%20I%20think%20teaching%20lessons%20through%20scenarios%20could%20make%20for%20an%20interesting%20series%20of%20articles%2C%20and%20I%27m%20sure%20others%20have%20equally%20interesting%20experiences%20to%20share.%0D%0A&amp;short_link=http://bit.ly/bpn1ZV&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Pairing+Lessons+%231%3A+Be+cynical+with+code+&amp;link=http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/&amp;notes=The%20following%20scenario%20plays%20out%20a%20lot%20when%20I%20pair%20up%20with%20people%2C%20so%20I%20thought%20I%27d%20distill%20it%20down%20and%20write%20it%20up%20for%20future%20reference.%20%20I%20think%20teaching%20lessons%20through%20scenarios%20could%20make%20for%20an%20interesting%20series%20of%20articles%2C%20and%20I%27m%20sure%20others%20have%20equally%20interesting%20experiences%20to%20share.%0D%0A&amp;short_link=http://bit.ly/bpn1ZV&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=Pairing+Lessons+%231%3A+Be+cynical+with+code+&amp;link=http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/&amp;notes=The%20following%20scenario%20plays%20out%20a%20lot%20when%20I%20pair%20up%20with%20people%2C%20so%20I%20thought%20I%27d%20distill%20it%20down%20and%20write%20it%20up%20for%20future%20reference.%20%20I%20think%20teaching%20lessons%20through%20scenarios%20could%20make%20for%20an%20interesting%20series%20of%20articles%2C%20and%20I%27m%20sure%20others%20have%20equally%20interesting%20experiences%20to%20share.%0D%0A&amp;short_link=http://bit.ly/bpn1ZV&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Pairing+Lessons+%231%3A+Be+cynical+with+code+&amp;link=http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/&amp;notes=The%20following%20scenario%20plays%20out%20a%20lot%20when%20I%20pair%20up%20with%20people%2C%20so%20I%20thought%20I%27d%20distill%20it%20down%20and%20write%20it%20up%20for%20future%20reference.%20%20I%20think%20teaching%20lessons%20through%20scenarios%20could%20make%20for%20an%20interesting%20series%20of%20articles%2C%20and%20I%27m%20sure%20others%20have%20equally%20interesting%20experiences%20to%20share.%0D%0A&amp;short_link=http://bit.ly/bpn1ZV&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul><div style="clear: both;"></div></div>

<h3  class="related_post_title">More Related Content</h3><ul class="related_post"><li>No Related Post</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cwash.org/2010/02/10/pairing-lessons-1-be-cynical-with-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Eliminate Branching (IF Statements) to Produce Better Code</title>
		<link>http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=eliminate-branching-if-statements-to-produce-better-code</link>
		<comments>http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 21:05:46 +0000</pubDate>
		<dc:creator>Chris Wash</dc:creator>
				<category><![CDATA[Developer Testing]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[CapTech]]></category>
		<category><![CDATA[exercises]]></category>
		<category><![CDATA[polymorphism]]></category>
		<category><![CDATA[SOLID]]></category>
		<category><![CDATA[techniques]]></category>

		<guid isPermaLink="false">http://cwash.org/?p=426</guid>
		<description><![CDATA[In a recent tech talk, I watched Miško Hevery propose an interesting challenge to his audience: start a toy project and try to write the code with no if-else or switch blocks at all. None at all?  Before thinking about how to do this, why would you want to do it in the first place?  On the surface, it may seem to the unassuming a bit counter-intuitive.  Comparison-based branching is at the heart of programming, and the concept of an if-else or switch block is almost universal, existing in most every programming language ever devised.  What's Hevery's agenda with this exercise – and what can possibly be gained from trying to write a program without branching entirely?]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><p>In a <a title="The Clean Code Talks -- Inheritance, Polymorphism, &amp; Testing" href="http://www.youtube.com/watch?v=4F72VULWFvc&amp;feature=channel">recent tech talk</a>, I watched Miško Hevery propose an interesting challenge to his audience: start a toy project and try to write the code with no if-else or switch blocks at all. None <em>at all</em>?  Before thinking about how to do this, <em>why</em> would you want to do it in the first place?  On the surface, it may seem to the unassuming a bit counter-intuitive.  Comparison-based branching is at the heart of programming, and the concept of an if-else or switch block is almost universal, existing in most every programming language ever devised.  What&#8217;s Hevery&#8217;s agenda with this exercise – and what can possibly be gained from trying to write a program without branching <em>entirely?</em></p>
<p><em><span id="more-426"></span><br />
</em></p>
<h3>Branching Considered Harmful?</h3>
<p>In 1968, <a title="Dijkstra on Wikipedia" href="http://en.wikipedia.org/wiki/Edsger_Dijkstra">E.W. Diskjstra</a>, famed programmer and author of the shortest-path algorithm, wrote an article to be published in the <em>Communications of the ACM</em> entitled &#8220;A Case Against the GOTO Statement&#8221; that was later renamed &#8220;Goto Statement Considered Harmful&#8221; by the editor.  The phrase has grown into a meme of its own in the CS community, denoting a practice that has fallen from grace and become widely discouraged.</p>
<p>I bring this up not to point out its cultural signficance, but the core of his argument:</p>
<blockquote><p><em>The go to statement as it stands is just too primitive[;] it is too much an invitation to make a mess of one&#8217;s program.</em></p></blockquote>
<p>Essentially, Hevery is making this very point for the if-else statement, 40 years later, but for very similar reasons.  If-else and switch statements easily account for some of the ugliest code that I have seen (and written).  Overuse of if-else blocks can lead to the <a title="The Arrow Antipattern on c2wiki" href="http://c2.com/cgi/wiki?ArrowAntiPattern" target="_blank">arrow antipattern</a>, a scenario where conditional branches are so deeply nested they become unmaintainable.  Too often developers fall into this trap, described aptly by another Dijsktra quote:</p>
<blockquote><p><em>The prisoner falls in love with his chains&#8230;</em></p></blockquote>
<p><em><span style="font-style: normal;">Hevery&#8217;s point is that code without if (or switch) statements is:</span></em></p>
<ul>
<li>Easier to read and understand</li>
<li>Easier to test</li>
<li>Easier to maintain, extend, etc.</li>
</ul>
<p>But how do we write code without branching constructs?   If we are talking about fixing the arrow antipattern, you may know a time when you avoided this antipattern by using separation of concerns, for example, the MVC pattern instead of a monolithic servlet.</p>
<h3>Hevery&#8217;s Suggestions</h3>
<p>Hevery goes on to explain how good OO design can eliminate the need to explicitly code the most egregious uses of branching, by instead favoring dependency injection and <a title="Polymorphism on Wikipedia" href="http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming">polymorphism</a>.  But this technique is only really useful where we can create methods and objects that are <a title="Cohesion on Wikipedia" href="http://en.wikipedia.org/wiki/Cohesion_(computer_science)#High_cohesion">cohesive</a> and <a title="loose coupling on wikipedia" href="http://en.wikipedia.org/wiki/Loose_coupling" target="_blank">loosely coupled</a>.</p>
<blockquote><p><em>With polymorphism, you dispatch into a method whose binding is not known at compile-time but determined at runtime.</em></p></blockquote>
<p>This is the weapon of choice when we are working on code that is highly procedural and there is a bad OO-design smell.  But it is far from a one-size-fits-all solution.  If we were to transform every branch in our program by instead using polymorphism, it would be a tangle of incohesive objects that would likely be harder to maintain than the original procedural code.  Luckily, there are other techniques that are more appropriate in certain circumstances.</p>
<p>Hevery continues: certainly, primitive comparisons are going to be the most difficult to eliminate, but shouldn&#8217;t happen very often.  So we reluctantly allow these to stick around.  Another class of if statements exist to check against nulls.  Hevery explains how null pointer dereference checks quickly become pandemic, occurring every time a member of an object is referenced.  This should largely be a non-issue, and is only a problem because code is allowing nulls to be returned.  The semantics/method contract of most methods can easily be changed to prohibit the return of nulls.  (This is only really difficult when recursion is used.)  Developers, as it turns out, don&#8217;t have to return null values.  This is particularly true with collections, and warranted a section in Joshua Bloch&#8217;s <a title="Effective Java on Safari" href="http://my.safaribooksonline.com/9780137150021">Effective Java</a>.  Instead return an empty collection can be returned, or you can implement the <a title="Null Object Pattern on Wikipedia" href="http://en.wikipedia.org/wiki/Null_Object_pattern">null object pattern</a> (e.g., a no-op logger).  Hevery finally rallies for ditching using return codes in favor of throwing relevant exceptions.</p>
<p><em>Note: I encourage you to watch the talk, as he continues talking about how specifically you can use inheritance and polymorphism to remove branching. The rest of this article will talk about <strong>other strategies to remove unnecessary branches</strong>.</em></p>
<h3><strong>Remove Copy-Pasted Loops And Conditionals: Introduce Enumerable Operations</strong></h3>
<p><strong> </strong>Many languages contain an enumerable construct that includes &#8220;higher order&#8221; looping operations that can eliminate the need for many specialized cases that are usually solved by a foreach loop with an embedded conditional check of some sort.  This type of construct can be traced back to Smalltalk, and exists in most modern dynamic languages such as Ruby, Groovy, Python, and provided by a number of Javascript libraries.  Getting this to work in Java is a bit of a challenge because there is no mechanism for closures (yet) but some partial solutions do exist (they lack the full power of true closures, but overusing true closures can break down OO-style constructs).  For further information, take a look at <a title="LambdaJ on Google Code" href="http://code.google.com/p/lambdaj/">lambdaj</a>, Adrian Kuhn&#8217;s <a title="Adrian Kuhn: Pimp My Foreach" href="http://www.iam.unibe.ch/~akuhn/blog/2008/pimp-my-foreach-loop/">Pimp My Foreach</a> and <a title="Google Collections Iterators Javadoc" href="http://google-collections.googlecode.com/svn/trunk/javadoc/index.html?com/google/common/collect/Iterators.html">Google Collections&#8217; Iterators</a>.</p>
<p>Here is an example of the types of things this style of programming can fix.</p>
<pre>List&lt;Integer&gt; numbers = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
List&lt;Integer&gt; oddNumbers = new ArrayList&lt;Integer&gt;();

for (Integer check : numbers) {
    if (check % 2 != 0) {
      oddNumbers.add(check);
    }
}</pre>
<p>In the case of the Google Collection, they call their enumberable construct a &#8220;Iterable&#8221; and the closure construct a &#8220;Predicates,&#8221; we&#8217;ll be able to replace the above code with this:</p>
<pre>Predicate&lt;Integer&gt; oddIntegerPredicate = new Predicate&lt;Integer&gt;() {
    public boolean apply(Integer input) {
        return input % 2 != 0;
    }
};

Iterable&lt;Integer&gt; oddNumbers = Iterables.filter(numbers, oddIntegerPredicate);</pre>
<p>LambdaJ, and Kuhn&#8217;s solution work in a similar fashion.  While syntactically not as pretty as the dynamic languages, these constructs are functionally equivalent and arguably no more painful than the original code.  In many cases of deeply nested for-if-for-ifs, it significantly reduces the cyclomatic complexity).  This style of programming, once everyone on the team has become familiar with it, can reduce often copy-and-pasted looping code to one liners, removing some really ugly duplication and the potential for those hard-to-spot copy and paste errors.</p>
<h3>Lookout for Train Wrecks: Obey the Law (of Demeter &#8211; and other SOLID Principles).</h3>
<p>Steve Freeman and Nat Pryce describe &#8220;train wreck&#8221; code in their new book <a title="Growing OO Software Guided by Tests on Safari" href="http://search.safaribooksonline.com/9780321574442">Growing Object Oriented Software Guided By Tests</a> as chained &#8220;getter&#8221; method calls, linked together like train cars.  The authors call them &#8220;wrecks&#8221; because the code is essentially an egregious violation of the Law of Demeter.  One of Nat&#8217;s examples:</p>
<pre>employee.getJobDescription().getResponsibilities().add(new ManagementResponsibility(employee.getDepartment()));</pre>
<p>The problem is that this style of programming destroys all encapsulation and information hiding concepts in code.  Train wrecks can become pandemic as well because instead of using an object-oriented style of programming that passes messages between objects to perform work, we essentially allow objects to know too much about their collaborators and details.  Another way this has been said is &#8220;talk only to your friends&#8221; or &#8220;ask, don&#8217;t tell.&#8221;  A client object <em>asks</em> other objects what their state is, and uses an if statement to perform an action, instead of <em>telling</em> the collaborating object what it wants done, and letting that object figure it out.  Instead of the train wreck, we should extract the behavior to a method that allows us to tell our collaborator what we want done:</p>
<pre>employee.promoteToManager();</pre>
<p>Complex conditional &#8220;train wrecks&#8221; can often be extracted in the same way.  Quite often these types of objects are really just procedural code masquerading as a class, and will be in violation of other SOLID-principles such as the Single Responsibility principle.  Making this code more &#8220;SOLID&#8221; will make it easier to test and maintain.</p>
<h3><strong>The Bigger Picture: Well Designed, Testable Code</strong></h3>
<p>Remember, this exercise in design (by removing ugly branching) is not just for aesthetics.  There is a very legit reason to try these techniques: writing testable code.  Hevery&#8217;s talk (and his other talks) are essentially about how to write code that is testable.  Freeman and Pryce&#8217;s book is about the very close relationship between testability and OO-design.</p>
<p>If you like these ideas, you might be interested to try the <a title="TDD As If You Meant It" href="http://gojko.net/2009/08/02/tdd-as-if-you-meant-it-revisited/">TDD As If You Meant It</a> exercise as well.  This is an exercise that will show you how to listen to test code to drive adding features that are driven out by tests into your production codebase by implementing them first in the test class.  This is the ultimate way to ensure that your production code contains only what it needs and nothing more, and that code is adequately covered by tests!  Remember, the more code you write into production code, the more you&#8217;re going to need to maintain it, and the more skeptical you should be about how readable and well designed it is.  These techniques and exercises may seem silly on the surface, but if you explore them I am confident they will help you write better code!</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://www.shareaholic.com/api/share/?title=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.shareaholic.com/api/share/?title=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=24&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-gmail">
			<a href="http://www.shareaholic.com/api/share/?title=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=52&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Email this via Gmail">Email this via Gmail</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.shareaholic.com/api/share/?title=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=74&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.shareaholic.com/api/share/?title=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.shareaholic.com/api/share/?title=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=207&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.shareaholic.com/api/share/?title=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=6&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.shareaholic.com/api/share/?title=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=4&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul><div style="clear: both;"></div></div>

<h3  class="related_post_title">More Related Content</h3><ul class="related_post"><li>July 8, 2010 -- <a href="http://cwash.org/2010/07/08/transactions-part-1/" title="Transactions, Part 1">Transactions, Part 1</a> (0)</li><li>July 24, 2009 -- <a href="http://cwash.org/2009/07/24/the-elements-of-reusable-code/" title="The Elements of Reusable Code">The Elements of Reusable Code</a> (0)</li><li>July 29, 2009 -- <a href="http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/" title="What I&#8217;d Tell Myself About Design If I Were Just Beginning">What I&#8217;d Tell Myself About Design If I Were Just Beginning</a> (5)</li><li>June 9, 2009 -- <a href="http://cwash.org/2009/06/09/mocking-with-jmockit/" title="Mocking with JMockit">Mocking with JMockit</a> (5)</li><li>June 3, 2009 -- <a href="http://cwash.org/2009/06/03/what-is-hamcrest/" title="What is Hamcrest?">What is Hamcrest?</a> (0)</li><li>April 15, 2009 -- <a href="http://cwash.org/2009/04/15/osgi-ggity-giggity/" title="OSGi-ggity-Giggity">OSGi-ggity-Giggity</a> (4)</li><li>January 31, 2009 -- <a href="http://cwash.org/2009/01/31/in-response-to-stackoverflow-38quality-doesnt-matter-that-much-jeff-and-joel/" title="In response to Stackoverflow #38/&#8221;Quality Doesn&#8217;t Matter That Much&#8221; &#8212; Jeff and Joel ">In response to Stackoverflow #38/&#8221;Quality Doesn&#8217;t Matter That Much&#8221; &#8212; Jeff and Joel </a> (3)</li><li>January 13, 2009 -- <a href="http://cwash.org/2009/01/13/on-software-quality/" title="On Software Quality">On Software Quality</a> (8)</li><li>November 28, 2008 -- <a href="http://cwash.org/2008/11/28/must-havesreferences-for-modern-java-ee-developers/" title="Must Haves/References For Modern Java EE Developers">Must Haves/References For Modern Java EE Developers</a> (1)</li><li>November 19, 2008 -- <a href="http://cwash.org/2008/11/19/java-6-and-maven-209-on-leopard/" title="Java 6 and Maven 2.0.9 on Leopard">Java 6 and Maven 2.0.9 on Leopard</a> (7)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A response to &#8220;Can Java Be Saved?&#8221;</title>
		<link>http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=a-response-to-can-java-be-saved</link>
		<comments>http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 06:51:25 +0000</pubDate>
		<dc:creator>Chris Wash</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[guice]]></category>
		<category><![CDATA[response]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Weld]]></category>

		<guid isPermaLink="false">http://cwash.org/?p=414</guid>
		<description><![CDATA[I recently read Scott Leberknight's "Can Java Be Saved?" with interest and started typing up a response.  As Scott's article was a bit lengthy, so became the comments I had in response.  In excess of 1000 characters, which JRoller told me was spam when I finally tried to post it.  I'm still never surprised to see that happen to my writing.  Anyway, I'm probably just an oaf, but I didn't see Scott's e-mail listed anywhere on his site (probably another spam-deterrent choice) so I thought since I spent more than a few minutes on it, it'd be appropriate enough to post here and link it.  Sorry but I don't have the energy to evolve this into a more formal post, perhaps I will at a later date.]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><p>I recently read <a href="http://www.sleberknight.com/blog" target="_blank">Scott Leberknight</a>&#8216;s &#8220;<a href="http://www.sleberknight.com/blog/sleberkn/entry/can_java_be_saved" target="_blank">Can Java Be Saved?</a>&#8221; with interest and started typing up a response.  As Scott&#8217;s article was a bit lengthy, so became the comments I had in response.  In excess of 1000 characters, which JRoller told me was spam when I finally tried to post it.  I&#8217;m still never surprised to see that happen to my writing.  Anyway, I&#8217;m probably just an oaf, but I didn&#8217;t see Scott&#8217;s e-mail listed anywhere on his site (probably another spam-deterrent choice) so I thought since I spent more than a few minutes on it, it&#8217;d be appropriate enough to post here and link it.  Sorry but I don&#8217;t have the energy to evolve this into a more formal post, perhaps I will at a later date.</p>
<p><span id="more-414"></span></p>
<blockquote><p><!-- 		@page { size: 8.5in 11in; margin: 0.79in } 		P { margin-bottom: 0.08in } --></p>
<p style="margin-bottom: 0in;">A Newardian post deserves a Newardian reply!   I really do like your suggestions for language additions.  But those kind of additions are going to be happening in the context of the JCP, and that&#8217;s its own can of worms. I think you can directly attribute people&#8217;s despise for Java, or maybe more aptly, their enthusiasm for new languages, to the difficulty involved in making language changes to Java.  Regardless, there is still freshness to the innovation in the Java world.  People are just looking for it in the wrong place.  Certainly, the virtual machine ecosystem is alive and well, as you point out.  But in terms of innovation within Java, it&#8217;s happening in the programming model, not the language.  Some of Spring, Guice, and CDI&#8217;s enhancements to the programming model are novel ways of evolving Java given the restrictive &#8220;all things to everyone&#8221;, bureaucratic nature of language changes at this stage of the game.</p>
<p style="margin-bottom: 0in;">Here are a few examples of programming model innovation.  The native property syntax reminds me of another recent <a href="http://in.relation.to/Bloggers/WritingAPropertiesFileInJava" target="_blank">blog entry</a> by Gavin King.   Sure, it&#8217;s a bit more verbose, and lacks the punch of a native language feature.  But is it that much different?  Also, do I really need type inference?  I see it as easier to read and more powerful to leave the variable declaration and remove the duplication by nixing the call to the constructor, and annotating a producer method.  It also gets rid of the new keyword, which was your next item on the list.  Again, this is a programming model enhancement that&#8217;s independently evolved and proven itself.</p>
<p style="margin-bottom: 0in;">I think there is still a lot of room for evolution, and a lot of work being done.  As you point out, the 800 lb gorilla seems to be continued by-in and enthusiasm from vendors &#8211; but most if not all of the real innovation that is occurring in the community is through open source software.  It&#8217;s interesting how you have a rift in the open source space, in how, e.g. the Spring and the Seam teams have approached developing open source, and the willingness to reintroduce innovations to the JCP.  Is the JCP to be used as a springboard for adoption, or a roundtable to resolve pain points everyone is feeling?  Its days as a think-tank for ivory tower specs are gone.  The fate of Java is not yet sealed, but I think in terms of the language, we&#8217;re only really going to see problems that everyone feels are painful get pushed through diplomatically.  But that&#8217;s not to say that there are programming model enhancements that can be pushed for adoption.  Vendors seem to be more open to evolving the programming model than the language at this point, if someone is willing to baby a spec through the process.</p>
<p style="margin-bottom: 0in;">I certainly welcome any of your thoughts on this.  Thanks for the interesting article.</p>
</blockquote>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://www.shareaholic.com/api/share/?title=A+response+to+%22Can+Java+Be+Saved%3F%22&amp;link=http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/&amp;notes=I%20recently%20read%20Scott%20Leberknight%27s%20%22Can%20Java%20Be%20Saved%3F%22%20with%20interest%20and%20started%20typing%20up%20a%20response.%20%20As%20Scott%27s%20article%20was%20a%20bit%20lengthy%2C%20so%20became%20the%20comments%20I%20had%20in%20response.%20%20In%20excess%20of%201000%20characters%2C%20which%20JRoller%20told%20me%20was%20spam%20when%20I%20finally%20tried%20to%20post%20it.%20%20I%27m%20still%20never%20surprised%20to%20see%20that%20happen%20to%20my%20writing.%20%20Anyway%2C%20I%27m%20probably%20just%20an%20oaf%2C%20but%20I%20didn%27t%20see%20Scott%27s%20e-mail%20listed%20anywhere%20on%20his%20site%20%28probably%20another%20spam-deterrent%20choice%29%20so%20I%20thought%20since%20I%20spent%20more%20than%20a%20few%20minutes%20on%20it%2C%20it%27d%20be%20appropriate%20enough%20to%20post%20here%20and%20link%20it.%20%20Sorry%20but%20I%20don%27t%20have%20the%20energy%20to%20evolve%20this%20into%20a%20more%20formal%20post%2C%20perhaps%20I%20will%20at%20a%20later%20date.&amp;short_link=http://bit.ly/brIbBX&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=A+response+to+%22Can+Java+Be+Saved%3F%22&amp;link=http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/&amp;notes=I%20recently%20read%20Scott%20Leberknight%27s%20%22Can%20Java%20Be%20Saved%3F%22%20with%20interest%20and%20started%20typing%20up%20a%20response.%20%20As%20Scott%27s%20article%20was%20a%20bit%20lengthy%2C%20so%20became%20the%20comments%20I%20had%20in%20response.%20%20In%20excess%20of%201000%20characters%2C%20which%20JRoller%20told%20me%20was%20spam%20when%20I%20finally%20tried%20to%20post%20it.%20%20I%27m%20still%20never%20surprised%20to%20see%20that%20happen%20to%20my%20writing.%20%20Anyway%2C%20I%27m%20probably%20just%20an%20oaf%2C%20but%20I%20didn%27t%20see%20Scott%27s%20e-mail%20listed%20anywhere%20on%20his%20site%20%28probably%20another%20spam-deterrent%20choice%29%20so%20I%20thought%20since%20I%20spent%20more%20than%20a%20few%20minutes%20on%20it%2C%20it%27d%20be%20appropriate%20enough%20to%20post%20here%20and%20link%20it.%20%20Sorry%20but%20I%20don%27t%20have%20the%20energy%20to%20evolve%20this%20into%20a%20more%20formal%20post%2C%20perhaps%20I%20will%20at%20a%20later%20date.&amp;short_link=http://bit.ly/brIbBX&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.shareaholic.com/api/share/?title=A+response+to+%22Can+Java+Be+Saved%3F%22&amp;link=http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/&amp;notes=I%20recently%20read%20Scott%20Leberknight%27s%20%22Can%20Java%20Be%20Saved%3F%22%20with%20interest%20and%20started%20typing%20up%20a%20response.%20%20As%20Scott%27s%20article%20was%20a%20bit%20lengthy%2C%20so%20became%20the%20comments%20I%20had%20in%20response.%20%20In%20excess%20of%201000%20characters%2C%20which%20JRoller%20told%20me%20was%20spam%20when%20I%20finally%20tried%20to%20post%20it.%20%20I%27m%20still%20never%20surprised%20to%20see%20that%20happen%20to%20my%20writing.%20%20Anyway%2C%20I%27m%20probably%20just%20an%20oaf%2C%20but%20I%20didn%27t%20see%20Scott%27s%20e-mail%20listed%20anywhere%20on%20his%20site%20%28probably%20another%20spam-deterrent%20choice%29%20so%20I%20thought%20since%20I%20spent%20more%20than%20a%20few%20minutes%20on%20it%2C%20it%27d%20be%20appropriate%20enough%20to%20post%20here%20and%20link%20it.%20%20Sorry%20but%20I%20don%27t%20have%20the%20energy%20to%20evolve%20this%20into%20a%20more%20formal%20post%2C%20perhaps%20I%20will%20at%20a%20later%20date.&amp;short_link=http://bit.ly/brIbBX&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=24&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=A+response+to+%22Can+Java+Be+Saved%3F%22&amp;link=http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/&amp;notes=I%20recently%20read%20Scott%20Leberknight%27s%20%22Can%20Java%20Be%20Saved%3F%22%20with%20interest%20and%20started%20typing%20up%20a%20response.%20%20As%20Scott%27s%20article%20was%20a%20bit%20lengthy%2C%20so%20became%20the%20comments%20I%20had%20in%20response.%20%20In%20excess%20of%201000%20characters%2C%20which%20JRoller%20told%20me%20was%20spam%20when%20I%20finally%20tried%20to%20post%20it.%20%20I%27m%20still%20never%20surprised%20to%20see%20that%20happen%20to%20my%20writing.%20%20Anyway%2C%20I%27m%20probably%20just%20an%20oaf%2C%20but%20I%20didn%27t%20see%20Scott%27s%20e-mail%20listed%20anywhere%20on%20his%20site%20%28probably%20another%20spam-deterrent%20choice%29%20so%20I%20thought%20since%20I%20spent%20more%20than%20a%20few%20minutes%20on%20it%2C%20it%27d%20be%20appropriate%20enough%20to%20post%20here%20and%20link%20it.%20%20Sorry%20but%20I%20don%27t%20have%20the%20energy%20to%20evolve%20this%20into%20a%20more%20formal%20post%2C%20perhaps%20I%20will%20at%20a%20later%20date.&amp;short_link=http://bit.ly/brIbBX&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-gmail">
			<a href="http://www.shareaholic.com/api/share/?title=A+response+to+%22Can+Java+Be+Saved%3F%22&amp;link=http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/&amp;notes=I%20recently%20read%20Scott%20Leberknight%27s%20%22Can%20Java%20Be%20Saved%3F%22%20with%20interest%20and%20started%20typing%20up%20a%20response.%20%20As%20Scott%27s%20article%20was%20a%20bit%20lengthy%2C%20so%20became%20the%20comments%20I%20had%20in%20response.%20%20In%20excess%20of%201000%20characters%2C%20which%20JRoller%20told%20me%20was%20spam%20when%20I%20finally%20tried%20to%20post%20it.%20%20I%27m%20still%20never%20surprised%20to%20see%20that%20happen%20to%20my%20writing.%20%20Anyway%2C%20I%27m%20probably%20just%20an%20oaf%2C%20but%20I%20didn%27t%20see%20Scott%27s%20e-mail%20listed%20anywhere%20on%20his%20site%20%28probably%20another%20spam-deterrent%20choice%29%20so%20I%20thought%20since%20I%20spent%20more%20than%20a%20few%20minutes%20on%20it%2C%20it%27d%20be%20appropriate%20enough%20to%20post%20here%20and%20link%20it.%20%20Sorry%20but%20I%20don%27t%20have%20the%20energy%20to%20evolve%20this%20into%20a%20more%20formal%20post%2C%20perhaps%20I%20will%20at%20a%20later%20date.&amp;short_link=http://bit.ly/brIbBX&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=52&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Email this via Gmail">Email this via Gmail</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.shareaholic.com/api/share/?title=A+response+to+%22Can+Java+Be+Saved%3F%22&amp;link=http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/&amp;notes=I%20recently%20read%20Scott%20Leberknight%27s%20%22Can%20Java%20Be%20Saved%3F%22%20with%20interest%20and%20started%20typing%20up%20a%20response.%20%20As%20Scott%27s%20article%20was%20a%20bit%20lengthy%2C%20so%20became%20the%20comments%20I%20had%20in%20response.%20%20In%20excess%20of%201000%20characters%2C%20which%20JRoller%20told%20me%20was%20spam%20when%20I%20finally%20tried%20to%20post%20it.%20%20I%27m%20still%20never%20surprised%20to%20see%20that%20happen%20to%20my%20writing.%20%20Anyway%2C%20I%27m%20probably%20just%20an%20oaf%2C%20but%20I%20didn%27t%20see%20Scott%27s%20e-mail%20listed%20anywhere%20on%20his%20site%20%28probably%20another%20spam-deterrent%20choice%29%20so%20I%20thought%20since%20I%20spent%20more%20than%20a%20few%20minutes%20on%20it%2C%20it%27d%20be%20appropriate%20enough%20to%20post%20here%20and%20link%20it.%20%20Sorry%20but%20I%20don%27t%20have%20the%20energy%20to%20evolve%20this%20into%20a%20more%20formal%20post%2C%20perhaps%20I%20will%20at%20a%20later%20date.&amp;short_link=http://bit.ly/brIbBX&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=74&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.shareaholic.com/api/share/?title=A+response+to+%22Can+Java+Be+Saved%3F%22&amp;link=http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/&amp;notes=I%20recently%20read%20Scott%20Leberknight%27s%20%22Can%20Java%20Be%20Saved%3F%22%20with%20interest%20and%20started%20typing%20up%20a%20response.%20%20As%20Scott%27s%20article%20was%20a%20bit%20lengthy%2C%20so%20became%20the%20comments%20I%20had%20in%20response.%20%20In%20excess%20of%201000%20characters%2C%20which%20JRoller%20told%20me%20was%20spam%20when%20I%20finally%20tried%20to%20post%20it.%20%20I%27m%20still%20never%20surprised%20to%20see%20that%20happen%20to%20my%20writing.%20%20Anyway%2C%20I%27m%20probably%20just%20an%20oaf%2C%20but%20I%20didn%27t%20see%20Scott%27s%20e-mail%20listed%20anywhere%20on%20his%20site%20%28probably%20another%20spam-deterrent%20choice%29%20so%20I%20thought%20since%20I%20spent%20more%20than%20a%20few%20minutes%20on%20it%2C%20it%27d%20be%20appropriate%20enough%20to%20post%20here%20and%20link%20it.%20%20Sorry%20but%20I%20don%27t%20have%20the%20energy%20to%20evolve%20this%20into%20a%20more%20formal%20post%2C%20perhaps%20I%20will%20at%20a%20later%20date.&amp;short_link=http://bit.ly/brIbBX&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.shareaholic.com/api/share/?title=A+response+to+%22Can+Java+Be+Saved%3F%22&amp;link=http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/&amp;notes=I%20recently%20read%20Scott%20Leberknight%27s%20%22Can%20Java%20Be%20Saved%3F%22%20with%20interest%20and%20started%20typing%20up%20a%20response.%20%20As%20Scott%27s%20article%20was%20a%20bit%20lengthy%2C%20so%20became%20the%20comments%20I%20had%20in%20response.%20%20In%20excess%20of%201000%20characters%2C%20which%20JRoller%20told%20me%20was%20spam%20when%20I%20finally%20tried%20to%20post%20it.%20%20I%27m%20still%20never%20surprised%20to%20see%20that%20happen%20to%20my%20writing.%20%20Anyway%2C%20I%27m%20probably%20just%20an%20oaf%2C%20but%20I%20didn%27t%20see%20Scott%27s%20e-mail%20listed%20anywhere%20on%20his%20site%20%28probably%20another%20spam-deterrent%20choice%29%20so%20I%20thought%20since%20I%20spent%20more%20than%20a%20few%20minutes%20on%20it%2C%20it%27d%20be%20appropriate%20enough%20to%20post%20here%20and%20link%20it.%20%20Sorry%20but%20I%20don%27t%20have%20the%20energy%20to%20evolve%20this%20into%20a%20more%20formal%20post%2C%20perhaps%20I%20will%20at%20a%20later%20date.&amp;short_link=http://bit.ly/brIbBX&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=207&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=A+response+to+%22Can+Java+Be+Saved%3F%22&amp;link=http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/&amp;notes=I%20recently%20read%20Scott%20Leberknight%27s%20%22Can%20Java%20Be%20Saved%3F%22%20with%20interest%20and%20started%20typing%20up%20a%20response.%20%20As%20Scott%27s%20article%20was%20a%20bit%20lengthy%2C%20so%20became%20the%20comments%20I%20had%20in%20response.%20%20In%20excess%20of%201000%20characters%2C%20which%20JRoller%20told%20me%20was%20spam%20when%20I%20finally%20tried%20to%20post%20it.%20%20I%27m%20still%20never%20surprised%20to%20see%20that%20happen%20to%20my%20writing.%20%20Anyway%2C%20I%27m%20probably%20just%20an%20oaf%2C%20but%20I%20didn%27t%20see%20Scott%27s%20e-mail%20listed%20anywhere%20on%20his%20site%20%28probably%20another%20spam-deterrent%20choice%29%20so%20I%20thought%20since%20I%20spent%20more%20than%20a%20few%20minutes%20on%20it%2C%20it%27d%20be%20appropriate%20enough%20to%20post%20here%20and%20link%20it.%20%20Sorry%20but%20I%20don%27t%20have%20the%20energy%20to%20evolve%20this%20into%20a%20more%20formal%20post%2C%20perhaps%20I%20will%20at%20a%20later%20date.&amp;short_link=http://bit.ly/brIbBX&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.shareaholic.com/api/share/?title=A+response+to+%22Can+Java+Be+Saved%3F%22&amp;link=http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/&amp;notes=I%20recently%20read%20Scott%20Leberknight%27s%20%22Can%20Java%20Be%20Saved%3F%22%20with%20interest%20and%20started%20typing%20up%20a%20response.%20%20As%20Scott%27s%20article%20was%20a%20bit%20lengthy%2C%20so%20became%20the%20comments%20I%20had%20in%20response.%20%20In%20excess%20of%201000%20characters%2C%20which%20JRoller%20told%20me%20was%20spam%20when%20I%20finally%20tried%20to%20post%20it.%20%20I%27m%20still%20never%20surprised%20to%20see%20that%20happen%20to%20my%20writing.%20%20Anyway%2C%20I%27m%20probably%20just%20an%20oaf%2C%20but%20I%20didn%27t%20see%20Scott%27s%20e-mail%20listed%20anywhere%20on%20his%20site%20%28probably%20another%20spam-deterrent%20choice%29%20so%20I%20thought%20since%20I%20spent%20more%20than%20a%20few%20minutes%20on%20it%2C%20it%27d%20be%20appropriate%20enough%20to%20post%20here%20and%20link%20it.%20%20Sorry%20but%20I%20don%27t%20have%20the%20energy%20to%20evolve%20this%20into%20a%20more%20formal%20post%2C%20perhaps%20I%20will%20at%20a%20later%20date.&amp;short_link=http://bit.ly/brIbBX&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=6&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.shareaholic.com/api/share/?title=A+response+to+%22Can+Java+Be+Saved%3F%22&amp;link=http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/&amp;notes=I%20recently%20read%20Scott%20Leberknight%27s%20%22Can%20Java%20Be%20Saved%3F%22%20with%20interest%20and%20started%20typing%20up%20a%20response.%20%20As%20Scott%27s%20article%20was%20a%20bit%20lengthy%2C%20so%20became%20the%20comments%20I%20had%20in%20response.%20%20In%20excess%20of%201000%20characters%2C%20which%20JRoller%20told%20me%20was%20spam%20when%20I%20finally%20tried%20to%20post%20it.%20%20I%27m%20still%20never%20surprised%20to%20see%20that%20happen%20to%20my%20writing.%20%20Anyway%2C%20I%27m%20probably%20just%20an%20oaf%2C%20but%20I%20didn%27t%20see%20Scott%27s%20e-mail%20listed%20anywhere%20on%20his%20site%20%28probably%20another%20spam-deterrent%20choice%29%20so%20I%20thought%20since%20I%20spent%20more%20than%20a%20few%20minutes%20on%20it%2C%20it%27d%20be%20appropriate%20enough%20to%20post%20here%20and%20link%20it.%20%20Sorry%20but%20I%20don%27t%20have%20the%20energy%20to%20evolve%20this%20into%20a%20more%20formal%20post%2C%20perhaps%20I%20will%20at%20a%20later%20date.&amp;short_link=http://bit.ly/brIbBX&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=4&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=A+response+to+%22Can+Java+Be+Saved%3F%22&amp;link=http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/&amp;notes=I%20recently%20read%20Scott%20Leberknight%27s%20%22Can%20Java%20Be%20Saved%3F%22%20with%20interest%20and%20started%20typing%20up%20a%20response.%20%20As%20Scott%27s%20article%20was%20a%20bit%20lengthy%2C%20so%20became%20the%20comments%20I%20had%20in%20response.%20%20In%20excess%20of%201000%20characters%2C%20which%20JRoller%20told%20me%20was%20spam%20when%20I%20finally%20tried%20to%20post%20it.%20%20I%27m%20still%20never%20surprised%20to%20see%20that%20happen%20to%20my%20writing.%20%20Anyway%2C%20I%27m%20probably%20just%20an%20oaf%2C%20but%20I%20didn%27t%20see%20Scott%27s%20e-mail%20listed%20anywhere%20on%20his%20site%20%28probably%20another%20spam-deterrent%20choice%29%20so%20I%20thought%20since%20I%20spent%20more%20than%20a%20few%20minutes%20on%20it%2C%20it%27d%20be%20appropriate%20enough%20to%20post%20here%20and%20link%20it.%20%20Sorry%20but%20I%20don%27t%20have%20the%20energy%20to%20evolve%20this%20into%20a%20more%20formal%20post%2C%20perhaps%20I%20will%20at%20a%20later%20date.&amp;short_link=http://bit.ly/brIbBX&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=A+response+to+%22Can+Java+Be+Saved%3F%22&amp;link=http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/&amp;notes=I%20recently%20read%20Scott%20Leberknight%27s%20%22Can%20Java%20Be%20Saved%3F%22%20with%20interest%20and%20started%20typing%20up%20a%20response.%20%20As%20Scott%27s%20article%20was%20a%20bit%20lengthy%2C%20so%20became%20the%20comments%20I%20had%20in%20response.%20%20In%20excess%20of%201000%20characters%2C%20which%20JRoller%20told%20me%20was%20spam%20when%20I%20finally%20tried%20to%20post%20it.%20%20I%27m%20still%20never%20surprised%20to%20see%20that%20happen%20to%20my%20writing.%20%20Anyway%2C%20I%27m%20probably%20just%20an%20oaf%2C%20but%20I%20didn%27t%20see%20Scott%27s%20e-mail%20listed%20anywhere%20on%20his%20site%20%28probably%20another%20spam-deterrent%20choice%29%20so%20I%20thought%20since%20I%20spent%20more%20than%20a%20few%20minutes%20on%20it%2C%20it%27d%20be%20appropriate%20enough%20to%20post%20here%20and%20link%20it.%20%20Sorry%20but%20I%20don%27t%20have%20the%20energy%20to%20evolve%20this%20into%20a%20more%20formal%20post%2C%20perhaps%20I%20will%20at%20a%20later%20date.&amp;short_link=http://bit.ly/brIbBX&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=A+response+to+%22Can+Java+Be+Saved%3F%22&amp;link=http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/&amp;notes=I%20recently%20read%20Scott%20Leberknight%27s%20%22Can%20Java%20Be%20Saved%3F%22%20with%20interest%20and%20started%20typing%20up%20a%20response.%20%20As%20Scott%27s%20article%20was%20a%20bit%20lengthy%2C%20so%20became%20the%20comments%20I%20had%20in%20response.%20%20In%20excess%20of%201000%20characters%2C%20which%20JRoller%20told%20me%20was%20spam%20when%20I%20finally%20tried%20to%20post%20it.%20%20I%27m%20still%20never%20surprised%20to%20see%20that%20happen%20to%20my%20writing.%20%20Anyway%2C%20I%27m%20probably%20just%20an%20oaf%2C%20but%20I%20didn%27t%20see%20Scott%27s%20e-mail%20listed%20anywhere%20on%20his%20site%20%28probably%20another%20spam-deterrent%20choice%29%20so%20I%20thought%20since%20I%20spent%20more%20than%20a%20few%20minutes%20on%20it%2C%20it%27d%20be%20appropriate%20enough%20to%20post%20here%20and%20link%20it.%20%20Sorry%20but%20I%20don%27t%20have%20the%20energy%20to%20evolve%20this%20into%20a%20more%20formal%20post%2C%20perhaps%20I%20will%20at%20a%20later%20date.&amp;short_link=http://bit.ly/brIbBX&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=A+response+to+%22Can+Java+Be+Saved%3F%22&amp;link=http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/&amp;notes=I%20recently%20read%20Scott%20Leberknight%27s%20%22Can%20Java%20Be%20Saved%3F%22%20with%20interest%20and%20started%20typing%20up%20a%20response.%20%20As%20Scott%27s%20article%20was%20a%20bit%20lengthy%2C%20so%20became%20the%20comments%20I%20had%20in%20response.%20%20In%20excess%20of%201000%20characters%2C%20which%20JRoller%20told%20me%20was%20spam%20when%20I%20finally%20tried%20to%20post%20it.%20%20I%27m%20still%20never%20surprised%20to%20see%20that%20happen%20to%20my%20writing.%20%20Anyway%2C%20I%27m%20probably%20just%20an%20oaf%2C%20but%20I%20didn%27t%20see%20Scott%27s%20e-mail%20listed%20anywhere%20on%20his%20site%20%28probably%20another%20spam-deterrent%20choice%29%20so%20I%20thought%20since%20I%20spent%20more%20than%20a%20few%20minutes%20on%20it%2C%20it%27d%20be%20appropriate%20enough%20to%20post%20here%20and%20link%20it.%20%20Sorry%20but%20I%20don%27t%20have%20the%20energy%20to%20evolve%20this%20into%20a%20more%20formal%20post%2C%20perhaps%20I%20will%20at%20a%20later%20date.&amp;short_link=http://bit.ly/brIbBX&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul><div style="clear: both;"></div></div>

<h3  class="related_post_title">More Related Content</h3><ul class="related_post"><li>November 28, 2008 -- <a href="http://cwash.org/2008/11/28/must-havesreferences-for-modern-java-ee-developers/" title="Must Haves/References For Modern Java EE Developers">Must Haves/References For Modern Java EE Developers</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cwash.org/2009/11/14/a-response-to-can-java-be-saved/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What I&#8217;d Tell Myself About Design If I Were Just Beginning</title>
		<link>http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=what-id-tell-myself-about-design-if-i-were-just-beginning</link>
		<comments>http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 02:25:26 +0000</pubDate>
		<dc:creator>Chris Wash</dc:creator>
				<category><![CDATA[Meta/Blog]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[CapTech]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[links]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://cwash.org/?p=104</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/";</script>From all the time I&#8217;ve spent learning design, there are a handful of things I&#8217;d hope to remember or re-read if I were to ever get amnesia and have to start all over again.  For new comers and experts alike, I&#8217;d like to share a few ideas about design worth thinking about through musings and [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><address style="text-align: left;">From all the time I&#8217;ve spent learning design, there are a handful of things I&#8217;d hope to remember or re-read if I were to ever get amnesia and have to start all over again.  For new comers and experts alike, I&#8217;d like to share a few ideas about design worth thinking about through musings and links to other material that has helped form my opinions (some more relevant than others).</address>
<address style="text-align: left;"><span id="more-104"></span><br />
</address>
<blockquote>
<p style="text-align: right;">The competent programmer is fully aware of the strictly limited size of his (or her) own skull; therefore he (or she) approaches the programming task in full humility, and among other things he (or she) avoids clever tricks like the plague.<br />
–E.W. Dijkstra</p>
</blockquote>
<p>Designers are something of an enigma to most people; however, the fruits of their labor are largely understood, well used and appreciated. Design is often considered to be a purely aesthetic matter: a matter of taste, of style, of opinion; we know a good design when it when we see it.  But many times we can&#8217;t quite put our thumb on <em>why</em> it&#8217;s good design, or what makes it well-thought out, or pleasing. Maybe some of us can articulate what it is we like about design, but few connect the dots enough to notice recurring qualities in our favorite designs or think about <a href="http://en.wikipedia.org/wiki/Golden_ratio">underlying truths beneath those qualities</a>.  Even fewer still have the experience to know <em>when to apply these qualities, within the proper context</em> to produce a good design.<img class="alignright size-medium wp-image-373" title="parthenon" src="http://cwash.org/wp-content/uploads/2009/07/parthenon-300x199.jpg" alt="parthenon" width="300" height="199" /></p>
<p>Programmers are equally if not more engimatic to general society.  Perhaps on the surface for <a href="http://xkcd.com/378/">many</a> <a href="http://stackoverflow.com/questions/406760/whats-your-most-controversial-programming-opinion">different</a> <a href="http://www.codethinked.com/post/2007/12/06/The-Programmer-Dress-Code.aspx">reasons</a>.  But, coincidentally developers are designing all the time; could this play a part, perhaps?  Is this why we refer to the most experienced of our profession as &#8220;<em>architects</em>?&#8221;  Aren&#8217;t we, by way of programming systems and wiring them together, inherently striving for greatness as <em>designers</em>?</p>
<p>Especially when you&#8217;re inexperienced, you&#8217;re very much focused on learning what it is about a design that you find compelling, agreeable, or pleasing.  We value a good design so much that we spend a lot of time studying, talking about and debating what makes a good design.  As the narrator (Jack?) in <em>Fight Club</em> admits he had become &#8220;a slave to the <em>IKEA nesting instinct</em>,&#8221; so too, I find myself in willing servitude of the <em>design pattern instinct</em> at times.  More often nowadays I stop and catch myself, and I&#8217;d like to think I&#8217;m not the only one with this premonition.  Too quick are we to whip out our <a href="http://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional/dp/0201633612">design patterns books</a> (especially right after we&#8217;ve finished them) and, as the first order of business, <a href="http://blog.jovan-s.com/2009/07/29/do-not-use-design-patterns-upfront/">go to town</a> <a href="http://blogs.captechventures.com/blog/andy-pemberton/knowing-when-apply-design-pattern">solving problems we don&#8217;t have</a>.</p>
<p>Few products today need to ship in shrink wrapped boxes as they once traditionally had to.  The connectedness of the Internet, coupled with all of the modern advances in hardware have fundamentally changed the way we look at developing software.  &#8220;<em><a href="http://en.wikipedia.org/wiki/Project_triangle">Fast, cheap, good &#8211; pick any two</a></em>&#8221; still applies, but fast is faster, cheap is <em>a lot</em> cheaper, and good encompasses much more today than ever before.  Software development still <a href="http://computer.howstuffworks.com/wirths-law.htm">lags</a> <a href="http://en.wikipedia.org/wiki/Software_bloat">behind</a>.</p>
<p>The unwise developer makes all design decisions up front, for fear of getting the solution wrong.  The enlightened developer creates the closest thing to a working solution now, for fear of getting the solution wrong.  The former produces a solution that fits his view of the problem.  The later continually adapts his solution to encompass others&#8217; view of the problem.  Which is more valuable?  The unwise developer wants to be done, and will sacrifice alienating some users.  The enlightened developer wants to be correct, and will compromise stability of the design.  Why do this? Because inactivity leads to <a href="http://en.wikipedia.org/wiki/Software_rot">software rot</a>, and we have techniques for <a href="http://en.wikipedia.org/wiki/Test-driven_development">dealing</a> <a href="http://en.wikipedia.org/wiki/Continuous_integration">with</a> <a href="http://www.pragprog.com/titles/auto/pragmatic-project-automation">changing designs</a>.  What good are our <a href="http://blog.thinkrelevance.com/2007/5/17/design-patterns-are-code-smells">big up-front design skills</a> when the requirements change?</p>
<p>Like Darwin encouraged us to do with our views of biology, we&#8217;ve ditched an up-front design model for an evolutionary one.  There is increasingly a focus on software as an evolutionary process everywhere, and it&#8217;s phrased in many ways: <em><a href="http://en.wikipedia.org/wiki/Continuous_production">continuous production</a></em> (Cal Henderson&#8217;s <a href="http://www.webstock.org.nz/talks/speakers/cal-henderson/building-big-on-the-web/">explanation</a> the best); the <a href="http://en.wikipedia.org/wiki/Perpetual_beta"><em>perpetual beta</em></a>; &#8220;<em>no such thing as done</em>&#8220;, <em><a href="http://agilemanifesto.org/">agility</a> &#8211; openness and responsiveness to change</em>.  The risk of bloated software has lead us down a path to focus on and seek to create small, modular applications that create &#8220;business&#8221; value immediately, <a href="http://en.wikipedia.org/wiki/Unix_philosophy">can be chained together but never grow overly complex</a>.  Our design decisions still exist, but they are on a much smaller scale, and with much <a href="http://www.amazon.com/Refactoring-Patterns-Addison-Wesley-Signature-Kerievsky/dp/0321213351/ref=sr_1_1?ie=UTF8&amp;qid=1248913490&amp;sr=8-1">more concrete drivers</a> and concerns factored in.  And we understand that we&#8217;re fundamentally in error when we make them out-of-context.</p>
<p>Others have thoroughly dealt with this topic in much greater detail than I can or care to.  Neal Ford has written about what <a href="http://www.ibm.com/developerworks/views/java/libraryview.jsp?search_by=evolutionary+architecture+emergent+design:">Evolutionary Architecture and Emergent Design</a> look like.  Martin Fowler has asked, &#8220;<a href="http://martinfowler.com/articles/designDead.html"><em>Is Design Dead?</em></a>&#8221; and answered his own question.  I would encourage you to read their thoughts on the matter, as my own conclusions are largely in line with theirs.</p>
<p>That being said, there are some tools that you should look to add to your collection as you go.  Any craftsman must learn to master his or her tools.  Just having the tool in your toolbox does not necessarily make you a good designer; because they must be used properly, in the proper context.  Like Clint Eastwood&#8217;s character explains of his toolkit in <em>Gran Turino</em>: each tool has a job, its own context and realm of applicability.  Also, resist the urge to apply them every chance you get.  Much of good design is about <a title="External link to http://www.presentationzen.com/presentationzen/2007/03/can_limitations.html" href="http://www.presentationzen.com/presentationzen/2007/03/can_limitations.html" target="_blank">restraint</a>.  Try to see them applied correctly, or incorrectly, and talk with others about it.  Keep a conversation going; don&#8217;t allow your codebase to become stagnant.  Continually question, strike the right balance and good designs will emerge and bad designs will improve. Your methodology may have you delivering in sprints, but good design is a marathon.</p>
<p>Here are the basic tools.  There is a lot of material out there on these &#8211; easy to Google or find on Wikipedia.  I am not going to offer any links or speculation, because I think these things should continually be discussed.  You should be constantly evolving your own opinions and knowledge of them.  They must be used, discussed and understood in order to be mastered.</p>
<p>Overarching Computing Principles &#8211; Reduce Complexity</p>
<ul>
<li>KISS &#8211; Keep it Simple, Stupid.</li>
<li>DRY &#8211; Don&#8217;t Repeat Yourself.</li>
<li>YAGNI &#8211; You Ain&#8217;t Gonna Need It.</li>
<li>SOC &#8211; Separation of Concerns.</li>
</ul>
<p>Basic OO Principles</p>
<ul>
<li>Inheritence</li>
<li>Coupling/Cohesion (dependency)</li>
<li>Encapsulation</li>
<li>Modularity</li>
<li>Abstraction</li>
<li>Polymorphism</li>
</ul>
<p>SOLID (Advanced) OO Principles</p>
<ul>
<li>Single Responsibility Principle</li>
<li>Open/Closed Principle</li>
<li>Liskov Substitution Principle</li>
<li>Interface Segregation Principle</li>
<li>Dependency Inversion Principle</li>
</ul>
<p>Emergent Design Techniques</p>
<ul>
<li>Test Driven Development</li>
<li>Continuous Integration</li>
<li>Refactoring</li>
<li>Project Automation</li>
<li>Continuous Production</li>
</ul>
<p>To borrow a great closing quote from <a href="http://rubyhoedown2008.confreaks.com/05-bryan-liles-lightning-talk-tatft-test-all-the-f-in-time.html">Brian Liles</a>,</p>
<blockquote>
<p style="text-align: right;">Do not seek to follow in the footsteps of the wise.  Seek what they sought.<br />
–Matsuo Basho</p>
</blockquote>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://www.shareaholic.com/api/share/?title=What+I%27d+Tell+Myself+About+Design+If+I+Were+Just+Beginning&amp;link=http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/&amp;notes=From%20all%20the%20time%20I%27ve%20spent%20learning%20design%2C%20there%20are%20a%20handful%20of%20things%20I%27d%20hope%20to%20remember%20or%20re-read%20if%20I%20were%20to%20ever%20get%20amnesia%20and%20have%20to%20start%20all%20over%20again.%C2%A0%20For%20new%20comers%20and%20experts%20alike%2C%20I%27d%20like%20to%20share%20a%20few%20ideas%20about%20design%20worth%20thinking%20about%20through%20musings%20and%20links%20to&amp;short_link=http://bit.ly/aVQIyj&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=What+I%27d+Tell+Myself+About+Design+If+I+Were+Just+Beginning&amp;link=http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/&amp;notes=From%20all%20the%20time%20I%27ve%20spent%20learning%20design%2C%20there%20are%20a%20handful%20of%20things%20I%27d%20hope%20to%20remember%20or%20re-read%20if%20I%20were%20to%20ever%20get%20amnesia%20and%20have%20to%20start%20all%20over%20again.%C2%A0%20For%20new%20comers%20and%20experts%20alike%2C%20I%27d%20like%20to%20share%20a%20few%20ideas%20about%20design%20worth%20thinking%20about%20through%20musings%20and%20links%20to&amp;short_link=http://bit.ly/aVQIyj&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.shareaholic.com/api/share/?title=What+I%27d+Tell+Myself+About+Design+If+I+Were+Just+Beginning&amp;link=http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/&amp;notes=From%20all%20the%20time%20I%27ve%20spent%20learning%20design%2C%20there%20are%20a%20handful%20of%20things%20I%27d%20hope%20to%20remember%20or%20re-read%20if%20I%20were%20to%20ever%20get%20amnesia%20and%20have%20to%20start%20all%20over%20again.%C2%A0%20For%20new%20comers%20and%20experts%20alike%2C%20I%27d%20like%20to%20share%20a%20few%20ideas%20about%20design%20worth%20thinking%20about%20through%20musings%20and%20links%20to&amp;short_link=http://bit.ly/aVQIyj&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=24&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=What+I%27d+Tell+Myself+About+Design+If+I+Were+Just+Beginning&amp;link=http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/&amp;notes=From%20all%20the%20time%20I%27ve%20spent%20learning%20design%2C%20there%20are%20a%20handful%20of%20things%20I%27d%20hope%20to%20remember%20or%20re-read%20if%20I%20were%20to%20ever%20get%20amnesia%20and%20have%20to%20start%20all%20over%20again.%C2%A0%20For%20new%20comers%20and%20experts%20alike%2C%20I%27d%20like%20to%20share%20a%20few%20ideas%20about%20design%20worth%20thinking%20about%20through%20musings%20and%20links%20to&amp;short_link=http://bit.ly/aVQIyj&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-gmail">
			<a href="http://www.shareaholic.com/api/share/?title=What+I%27d+Tell+Myself+About+Design+If+I+Were+Just+Beginning&amp;link=http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/&amp;notes=From%20all%20the%20time%20I%27ve%20spent%20learning%20design%2C%20there%20are%20a%20handful%20of%20things%20I%27d%20hope%20to%20remember%20or%20re-read%20if%20I%20were%20to%20ever%20get%20amnesia%20and%20have%20to%20start%20all%20over%20again.%C2%A0%20For%20new%20comers%20and%20experts%20alike%2C%20I%27d%20like%20to%20share%20a%20few%20ideas%20about%20design%20worth%20thinking%20about%20through%20musings%20and%20links%20to&amp;short_link=http://bit.ly/aVQIyj&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=52&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Email this via Gmail">Email this via Gmail</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.shareaholic.com/api/share/?title=What+I%27d+Tell+Myself+About+Design+If+I+Were+Just+Beginning&amp;link=http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/&amp;notes=From%20all%20the%20time%20I%27ve%20spent%20learning%20design%2C%20there%20are%20a%20handful%20of%20things%20I%27d%20hope%20to%20remember%20or%20re-read%20if%20I%20were%20to%20ever%20get%20amnesia%20and%20have%20to%20start%20all%20over%20again.%C2%A0%20For%20new%20comers%20and%20experts%20alike%2C%20I%27d%20like%20to%20share%20a%20few%20ideas%20about%20design%20worth%20thinking%20about%20through%20musings%20and%20links%20to&amp;short_link=http://bit.ly/aVQIyj&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=74&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.shareaholic.com/api/share/?title=What+I%27d+Tell+Myself+About+Design+If+I+Were+Just+Beginning&amp;link=http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/&amp;notes=From%20all%20the%20time%20I%27ve%20spent%20learning%20design%2C%20there%20are%20a%20handful%20of%20things%20I%27d%20hope%20to%20remember%20or%20re-read%20if%20I%20were%20to%20ever%20get%20amnesia%20and%20have%20to%20start%20all%20over%20again.%C2%A0%20For%20new%20comers%20and%20experts%20alike%2C%20I%27d%20like%20to%20share%20a%20few%20ideas%20about%20design%20worth%20thinking%20about%20through%20musings%20and%20links%20to&amp;short_link=http://bit.ly/aVQIyj&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.shareaholic.com/api/share/?title=What+I%27d+Tell+Myself+About+Design+If+I+Were+Just+Beginning&amp;link=http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/&amp;notes=From%20all%20the%20time%20I%27ve%20spent%20learning%20design%2C%20there%20are%20a%20handful%20of%20things%20I%27d%20hope%20to%20remember%20or%20re-read%20if%20I%20were%20to%20ever%20get%20amnesia%20and%20have%20to%20start%20all%20over%20again.%C2%A0%20For%20new%20comers%20and%20experts%20alike%2C%20I%27d%20like%20to%20share%20a%20few%20ideas%20about%20design%20worth%20thinking%20about%20through%20musings%20and%20links%20to&amp;short_link=http://bit.ly/aVQIyj&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=207&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=What+I%27d+Tell+Myself+About+Design+If+I+Were+Just+Beginning&amp;link=http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/&amp;notes=From%20all%20the%20time%20I%27ve%20spent%20learning%20design%2C%20there%20are%20a%20handful%20of%20things%20I%27d%20hope%20to%20remember%20or%20re-read%20if%20I%20were%20to%20ever%20get%20amnesia%20and%20have%20to%20start%20all%20over%20again.%C2%A0%20For%20new%20comers%20and%20experts%20alike%2C%20I%27d%20like%20to%20share%20a%20few%20ideas%20about%20design%20worth%20thinking%20about%20through%20musings%20and%20links%20to&amp;short_link=http://bit.ly/aVQIyj&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.shareaholic.com/api/share/?title=What+I%27d+Tell+Myself+About+Design+If+I+Were+Just+Beginning&amp;link=http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/&amp;notes=From%20all%20the%20time%20I%27ve%20spent%20learning%20design%2C%20there%20are%20a%20handful%20of%20things%20I%27d%20hope%20to%20remember%20or%20re-read%20if%20I%20were%20to%20ever%20get%20amnesia%20and%20have%20to%20start%20all%20over%20again.%C2%A0%20For%20new%20comers%20and%20experts%20alike%2C%20I%27d%20like%20to%20share%20a%20few%20ideas%20about%20design%20worth%20thinking%20about%20through%20musings%20and%20links%20to&amp;short_link=http://bit.ly/aVQIyj&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=6&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.shareaholic.com/api/share/?title=What+I%27d+Tell+Myself+About+Design+If+I+Were+Just+Beginning&amp;link=http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/&amp;notes=From%20all%20the%20time%20I%27ve%20spent%20learning%20design%2C%20there%20are%20a%20handful%20of%20things%20I%27d%20hope%20to%20remember%20or%20re-read%20if%20I%20were%20to%20ever%20get%20amnesia%20and%20have%20to%20start%20all%20over%20again.%C2%A0%20For%20new%20comers%20and%20experts%20alike%2C%20I%27d%20like%20to%20share%20a%20few%20ideas%20about%20design%20worth%20thinking%20about%20through%20musings%20and%20links%20to&amp;short_link=http://bit.ly/aVQIyj&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=4&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=What+I%27d+Tell+Myself+About+Design+If+I+Were+Just+Beginning&amp;link=http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/&amp;notes=From%20all%20the%20time%20I%27ve%20spent%20learning%20design%2C%20there%20are%20a%20handful%20of%20things%20I%27d%20hope%20to%20remember%20or%20re-read%20if%20I%20were%20to%20ever%20get%20amnesia%20and%20have%20to%20start%20all%20over%20again.%C2%A0%20For%20new%20comers%20and%20experts%20alike%2C%20I%27d%20like%20to%20share%20a%20few%20ideas%20about%20design%20worth%20thinking%20about%20through%20musings%20and%20links%20to&amp;short_link=http://bit.ly/aVQIyj&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=What+I%27d+Tell+Myself+About+Design+If+I+Were+Just+Beginning&amp;link=http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/&amp;notes=From%20all%20the%20time%20I%27ve%20spent%20learning%20design%2C%20there%20are%20a%20handful%20of%20things%20I%27d%20hope%20to%20remember%20or%20re-read%20if%20I%20were%20to%20ever%20get%20amnesia%20and%20have%20to%20start%20all%20over%20again.%C2%A0%20For%20new%20comers%20and%20experts%20alike%2C%20I%27d%20like%20to%20share%20a%20few%20ideas%20about%20design%20worth%20thinking%20about%20through%20musings%20and%20links%20to&amp;short_link=http://bit.ly/aVQIyj&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=What+I%27d+Tell+Myself+About+Design+If+I+Were+Just+Beginning&amp;link=http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/&amp;notes=From%20all%20the%20time%20I%27ve%20spent%20learning%20design%2C%20there%20are%20a%20handful%20of%20things%20I%27d%20hope%20to%20remember%20or%20re-read%20if%20I%20were%20to%20ever%20get%20amnesia%20and%20have%20to%20start%20all%20over%20again.%C2%A0%20For%20new%20comers%20and%20experts%20alike%2C%20I%27d%20like%20to%20share%20a%20few%20ideas%20about%20design%20worth%20thinking%20about%20through%20musings%20and%20links%20to&amp;short_link=http://bit.ly/aVQIyj&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=What+I%27d+Tell+Myself+About+Design+If+I+Were+Just+Beginning&amp;link=http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/&amp;notes=From%20all%20the%20time%20I%27ve%20spent%20learning%20design%2C%20there%20are%20a%20handful%20of%20things%20I%27d%20hope%20to%20remember%20or%20re-read%20if%20I%20were%20to%20ever%20get%20amnesia%20and%20have%20to%20start%20all%20over%20again.%C2%A0%20For%20new%20comers%20and%20experts%20alike%2C%20I%27d%20like%20to%20share%20a%20few%20ideas%20about%20design%20worth%20thinking%20about%20through%20musings%20and%20links%20to&amp;short_link=http://bit.ly/aVQIyj&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul><div style="clear: both;"></div></div>

<h3  class="related_post_title">More Related Content</h3><ul class="related_post"><li>July 24, 2009 -- <a href="http://cwash.org/2009/07/24/the-elements-of-reusable-code/" title="The Elements of Reusable Code">The Elements of Reusable Code</a> (0)</li><li>July 8, 2010 -- <a href="http://cwash.org/2010/07/08/transactions-part-1/" title="Transactions, Part 1">Transactions, Part 1</a> (0)</li><li>January 7, 2010 -- <a href="http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/" title="Eliminate Branching (IF Statements) to Produce Better Code">Eliminate Branching (IF Statements) to Produce Better Code</a> (0)</li><li>June 9, 2009 -- <a href="http://cwash.org/2009/06/09/mocking-with-jmockit/" title="Mocking with JMockit">Mocking with JMockit</a> (5)</li><li>June 3, 2009 -- <a href="http://cwash.org/2009/06/03/what-is-hamcrest/" title="What is Hamcrest?">What is Hamcrest?</a> (0)</li><li>April 15, 2009 -- <a href="http://cwash.org/2009/04/15/osgi-ggity-giggity/" title="OSGi-ggity-Giggity">OSGi-ggity-Giggity</a> (4)</li><li>December 1, 2008 -- <a href="http://cwash.org/2008/12/01/24ways/" title="24ways">24ways</a> (1)</li><li>November 28, 2008 -- <a href="http://cwash.org/2008/11/28/must-havesreferences-for-modern-java-ee-developers/" title="Must Haves/References For Modern Java EE Developers">Must Haves/References For Modern Java EE Developers</a> (1)</li><li>November 19, 2008 -- <a href="http://cwash.org/2008/11/19/java-6-and-maven-209-on-leopard/" title="Java 6 and Maven 2.0.9 on Leopard">Java 6 and Maven 2.0.9 on Leopard</a> (7)</li><li>August 2, 2008 -- <a href="http://cwash.org/2008/08/02/how-i-escape-the-reuse-trap/" title="How I Escape the &#8220;Reuse Trap&#8221;">How I Escape the &#8220;Reuse Trap&#8221;</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Elements of Reusable Code</title>
		<link>http://cwash.org/2009/07/24/the-elements-of-reusable-code/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=the-elements-of-reusable-code</link>
		<comments>http://cwash.org/2009/07/24/the-elements-of-reusable-code/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 19:35:49 +0000</pubDate>
		<dc:creator>Chris Wash</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[CapTech]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[DRY]]></category>
		<category><![CDATA[osgi]]></category>
		<category><![CDATA[reuse]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://cwash.org/?p=66</guid>
		<description><![CDATA[It's easier to make progress on large-scale problems by breaking them down, and making a habit of looking for patterns and ways to combine, compose and chain smaller solutions.  Like we do with Agile problem solving approaches.  Can't we do that for the hairy reuse problem?]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://cwash.org/2009/07/24/the-elements-of-reusable-code/";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><p>Back in April, there was an interesting article quoting Ron Jeffries et al on InfoQ looking at <a href="http://www.infoq.com/news/2009/04/agile-code-reuse" target="_new">code reuse from an agile perspective</a>. The conversation steered toward explaining reuse as a concern that is very expensive, looking at it from a top-down, &#8220;enterprise&#8221;-wide lens.</p>
<p>But is code reuse a black and white issue?  My contention is there are varying degrees of reuse that are often neglected on a microscale within an even moderately-sized project.  If the code you produce isn&#8217;t going to be reused, is it even going to be looked at?  These are similar problems with similar solutions.<span id="more-66"></span></p>
<p>As far as producing reusable code, I believe there is a medium of full-fledged, framework or API degree of reuse and also a &#8220;just enough&#8221; degree of reuse.  The article touched on a similar concept:</p>
<blockquote><p>Adam Sroka called this &#8216;emergent reuse&#8217; and noted that this seemed more efficient than the &#8216;design for reuse&#8217; approach. A poster named Tim explains it to his business people this way: &#8220;The first time you have to pay for it to be coded, the second time you have to pay for it to be reusable, the third time it&#8217;s free.&#8221;</p></blockquote>
<p>A higher degree of reuse requires more of an investment in these principles and practices.  What&#8217;s more, I think the article fails to draw a distinction between the roles (or competing concerns) involved in reuse: not only should we be worried about reusing code we produce, but often we are presented with a very real problem that wasn&#8217;t talked about in the article which is how we <em>consume code</em> that we choose to reuse.</p>
<p>A project is itself a consumer of its own code, and many times you&#8217;ll find a codebase is not even internally consistent in how it reuses code within itself.  Quite often consumer-reuse happens in the form of depending on third party libraries, open source frameworks or APIs.  In this sense, reuse becomes an issue of code quality, or more aptly, <a href="http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882">code cleanliness</a>.  It&#8217;s hard to come up with solutions to big problems by just thinking about an ideal solution, and even if you are able to do it, odds are your solution will probably be rather academic.  It&#8217;s easier to make progress on large-scale problems by breaking them down, and making a habit of looking for patterns and ways to combine, compose and chain smaller solutions.  Like we do with Agile problem solving approaches.  Can&#8217;t we do that for the hairy reuse problem?</p>
<h3>Decomposing the Problem</h3>
<p>Code reuse is a tough problem to nail down, because it&#8217;s a high-level view composed of a whole host of smaller concerns.  With a nod to <a href="http://en.wikipedia.org/wiki/Separation_of_concerns" target="_new">separation of concerns</a>, I&#8217;ve tried to decompose these competing factors into their own categories and make some suggestions of things to look at if you&#8217;d like to have a higher degree of reuse within your team or code base.</p>
<h3>Communication Concerns</h3>
<h4>Communication Saturation</h4>
<p>Last night I listened to an Agile Richmond talk by Jeff Sutherland espousing the ideal of communication saturation.  Jeff was talking about Scrum, and wanted to draw a strong correlation between this saturation and &#8220;hyper-productive&#8221; teams.  The key, Jeff said, was the ability of the group to recognize what is the next most important thing to work on and come to a collective decision about it.  This resonated with my experience with agile.  Our group was able to defer stories where we saw reuse was going to be apparent in the future, and this not only did this save us time in the long run, but it made the problem of &#8220;discovery&#8221; and consumption a no brainer.  Everyone on the team knew a component we delivered one release was to be reused in future releases, even if we weren&#8217;t involved in developing it.  This, coulped with the idea of collective code ownership, opens all the right communication channels for a software development project in my opinion.</p>
<h4>Value cross training</h4>
<p>Don&#8217;t be afraid to ask for help or give it if you can provide help.  <em>Educate yourself and teach others</em>.  You&#8217;ll find that successful teams do this naturally through many different ways.  If it seems tough to approach individuals, a better course of action may be to schedule a weekly development meeting to discuss hard problems and solutions to those problems.  Record topics that come up regularly.  An informal retrospective after a release or milestone can go a long way to correcting the course of the ship.</p>
<h3>Design Concerns</h3>
<h4>Cross cutting concerns</h4>
<p>A whole class of reuse problems can be boiled down to <em>reinventing the wheel</em> instead of solving core business problems.  If you&#8217;re solving a technical problem, is it a cross-cutting concern?   If you can identify a problem that has been solved in more than one way, it&#8217;s a good idea to pull the team together and talk about a good way to solve it moving forward.</p>
<h4>Refactor mercilessly to remove duplication</h4>
<p>Refactoring to remove duplication is a skill that takes practice and diligence to do well.  But it&#8217;s often a quick win to spot and remove duplication; <em>Don&#8217;t Repeat Yourself</em> will go a long way to improving the quality of the code your team produces by coercing the design of the code into something more fundamentally sound and amenable to larger scale refactoring later.</p>
<p>Reuse problems are often a manifestation of not having &#8220;everything in its right place.&#8221; If you always know where to go to get a hammer, then the problem is reduced to that hammer being there when you go to get it.  The bigger problem is (rightfully so, now) knowing when you need that a hammer.  This is not necessarily going to solve the problem of discoverability or consumption for you (communication is needed for that), but it will help with organization tremendously.  Whether or not you achieve an enterprise degree of reuse may be a horse of a different color and have a lot to do with communication, but in the microscale refactoring is the weapon of choice.</p>
<h4>Names Matter</h4>
<p>If you are able to refactor using <a href="http://c2.com/ppr/wiki/WikiPagesAboutRefactoring/ComposedMethod.html" target="_new">composed methods</a>, naming becomes a lot easier.  Each method will aim to solve one single &#8220;atomic&#8221; concern, and larger methods will become more readable and logical as they get shorter and more maintainable.</p>
<h3>Technical Concerns</h3>
<h4>Know the language and learn standard APIs<em> </em></h4>
<p><em>Unit testing</em> can help you learn a new API fairly quickly and document some hard-earned pieces of knowledge going forward.  A handy construct that is available in many languages is a <em>shell interpreter</em> like IRB or BeanShell, which can be invaluable in helping to pick up the syntax and semantics of a language.  <a href="http://en.wikipedia.org/wiki/Shell_(computing)#Shells_for_programming_languages" target="_new">Wikipedia has a list</a> of interactive programming language shells.</p>
<h4>Modularize your code</h4>
<p>Modularization is important.  Few languages support full modularization OOTB.  At the base level, you need to be able to encapsulate your code and also package or namespace it.  This is all you really get OOTB in most languages.  Ideally, full modularization would provide you with the ability to bundle together code and provide a mechanism to have the code installed or uninstalled dynamically at runtime &#8211; in order to achieve this you often have to specify dependencies between modules and declare a version for your bundle.  (OSGi aims to do this in Java, and is definitely worth a look).</p>
<p>Modularization is important to code reuse because it presents a barrier to someone else using your code.  If I want to use a particular component or method that exists in your library, that means I am fundamentally absorbing the lowest-level dependencies that you have.  If your project is not properly modularized, I may have to eat all of your dependencies.  The larger your component is, the more complexity any consumer must absorb to reuse your code.  Modularization should help with this problem.</p>
<p>SOA was supposed to have solved this problem, right?  At least a subset of reuse problems – iteroperability.  We&#8217;ve found it&#8217;s a lot harder in practice than it is in theory.  You have to version services, and use some registry to look up and discover a version.  Not to mention the governance issues involved.  This is why I&#8217;m so excited about <a href="http://cwash.org/2009/04/15/osgi-ggity-giggity/">OSGi</a>.  This might be the way to take your microscale reuse to the enterprise, and the model (in practice, and arguably theory) is a good deal simpler than SOA, too.</p>
<h4>Documentation Matters?</h4>
<p>If you are writing an API or framework, you can&#8217;t argue that documentation will be incredibly important to the success of your project. Note, I&#8217;m not talking necessarily here about Java-doc style-documentation; rather, I&#8217;m talking about providing a would be <em>reuser</em> of your code with enough support to make them successful.  Sometimes that means concise, well written and up-to-date API documentation.  Quite often this will come in the form of a user or developer&#8217;s guide with a more narrative style or structure.  FAQs or troubleshooting guides are often very important tools.</p>
<blockquote><p>Reuse is something that is far easier to say than to do. Doing it requires both good design and very good documentation. Even when we see good design, which is still infrequently, we don&#8217;t see the components reused without good documentation.</p>
<p>-D.L. Parnas, Software Aging. <em>Proceedings of the 16th International Conference on Software Engineering, 1994</em></p></blockquote>
<h3>Going Forward</h3>
<p>We all know waste is a thief.  Rework is wasteful.  Solving the problem of reuse on a grand scale may seem intractible, but I think it&#8217;s just another problem, and arguably not even the hardest one we have to contend with in software development.  If we can break the reuse problem down from its mighty enterprise-wide incarnation into smaller, more digestable reuse problems, it becomes a lot easier to make progress.  And maybe one day we&#8217;ll find a way to get to that enterprise-wide reuse that may seem too good to be true at the moment.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://cwash.org/2009/07/24/the-elements-of-reusable-code/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://www.shareaholic.com/api/share/?title=The+Elements+of+Reusable+Code&amp;link=http://cwash.org/2009/07/24/the-elements-of-reusable-code/&amp;notes=It%27s%20easier%20to%20make%20progress%20on%20large-scale%20problems%20by%20breaking%20them%20down%2C%20and%20making%20a%20habit%20of%20looking%20for%20patterns%20and%20ways%20to%20combine%2C%20compose%20and%20chain%20smaller%20solutions.%20%20Like%20we%20do%20with%20Agile%20problem%20solving%20approaches.%20%20Can%27t%20we%20do%20that%20for%20the%20hairy%20reuse%20problem%3F&amp;short_link=http://bit.ly/akyatS&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=The+Elements+of+Reusable+Code&amp;link=http://cwash.org/2009/07/24/the-elements-of-reusable-code/&amp;notes=It%27s%20easier%20to%20make%20progress%20on%20large-scale%20problems%20by%20breaking%20them%20down%2C%20and%20making%20a%20habit%20of%20looking%20for%20patterns%20and%20ways%20to%20combine%2C%20compose%20and%20chain%20smaller%20solutions.%20%20Like%20we%20do%20with%20Agile%20problem%20solving%20approaches.%20%20Can%27t%20we%20do%20that%20for%20the%20hairy%20reuse%20problem%3F&amp;short_link=http://bit.ly/akyatS&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.shareaholic.com/api/share/?title=The+Elements+of+Reusable+Code&amp;link=http://cwash.org/2009/07/24/the-elements-of-reusable-code/&amp;notes=It%27s%20easier%20to%20make%20progress%20on%20large-scale%20problems%20by%20breaking%20them%20down%2C%20and%20making%20a%20habit%20of%20looking%20for%20patterns%20and%20ways%20to%20combine%2C%20compose%20and%20chain%20smaller%20solutions.%20%20Like%20we%20do%20with%20Agile%20problem%20solving%20approaches.%20%20Can%27t%20we%20do%20that%20for%20the%20hairy%20reuse%20problem%3F&amp;short_link=http://bit.ly/akyatS&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=24&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=The+Elements+of+Reusable+Code&amp;link=http://cwash.org/2009/07/24/the-elements-of-reusable-code/&amp;notes=It%27s%20easier%20to%20make%20progress%20on%20large-scale%20problems%20by%20breaking%20them%20down%2C%20and%20making%20a%20habit%20of%20looking%20for%20patterns%20and%20ways%20to%20combine%2C%20compose%20and%20chain%20smaller%20solutions.%20%20Like%20we%20do%20with%20Agile%20problem%20solving%20approaches.%20%20Can%27t%20we%20do%20that%20for%20the%20hairy%20reuse%20problem%3F&amp;short_link=http://bit.ly/akyatS&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-gmail">
			<a href="http://www.shareaholic.com/api/share/?title=The+Elements+of+Reusable+Code&amp;link=http://cwash.org/2009/07/24/the-elements-of-reusable-code/&amp;notes=It%27s%20easier%20to%20make%20progress%20on%20large-scale%20problems%20by%20breaking%20them%20down%2C%20and%20making%20a%20habit%20of%20looking%20for%20patterns%20and%20ways%20to%20combine%2C%20compose%20and%20chain%20smaller%20solutions.%20%20Like%20we%20do%20with%20Agile%20problem%20solving%20approaches.%20%20Can%27t%20we%20do%20that%20for%20the%20hairy%20reuse%20problem%3F&amp;short_link=http://bit.ly/akyatS&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=52&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Email this via Gmail">Email this via Gmail</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.shareaholic.com/api/share/?title=The+Elements+of+Reusable+Code&amp;link=http://cwash.org/2009/07/24/the-elements-of-reusable-code/&amp;notes=It%27s%20easier%20to%20make%20progress%20on%20large-scale%20problems%20by%20breaking%20them%20down%2C%20and%20making%20a%20habit%20of%20looking%20for%20patterns%20and%20ways%20to%20combine%2C%20compose%20and%20chain%20smaller%20solutions.%20%20Like%20we%20do%20with%20Agile%20problem%20solving%20approaches.%20%20Can%27t%20we%20do%20that%20for%20the%20hairy%20reuse%20problem%3F&amp;short_link=http://bit.ly/akyatS&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=74&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.shareaholic.com/api/share/?title=The+Elements+of+Reusable+Code&amp;link=http://cwash.org/2009/07/24/the-elements-of-reusable-code/&amp;notes=It%27s%20easier%20to%20make%20progress%20on%20large-scale%20problems%20by%20breaking%20them%20down%2C%20and%20making%20a%20habit%20of%20looking%20for%20patterns%20and%20ways%20to%20combine%2C%20compose%20and%20chain%20smaller%20solutions.%20%20Like%20we%20do%20with%20Agile%20problem%20solving%20approaches.%20%20Can%27t%20we%20do%20that%20for%20the%20hairy%20reuse%20problem%3F&amp;short_link=http://bit.ly/akyatS&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.shareaholic.com/api/share/?title=The+Elements+of+Reusable+Code&amp;link=http://cwash.org/2009/07/24/the-elements-of-reusable-code/&amp;notes=It%27s%20easier%20to%20make%20progress%20on%20large-scale%20problems%20by%20breaking%20them%20down%2C%20and%20making%20a%20habit%20of%20looking%20for%20patterns%20and%20ways%20to%20combine%2C%20compose%20and%20chain%20smaller%20solutions.%20%20Like%20we%20do%20with%20Agile%20problem%20solving%20approaches.%20%20Can%27t%20we%20do%20that%20for%20the%20hairy%20reuse%20problem%3F&amp;short_link=http://bit.ly/akyatS&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=207&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=The+Elements+of+Reusable+Code&amp;link=http://cwash.org/2009/07/24/the-elements-of-reusable-code/&amp;notes=It%27s%20easier%20to%20make%20progress%20on%20large-scale%20problems%20by%20breaking%20them%20down%2C%20and%20making%20a%20habit%20of%20looking%20for%20patterns%20and%20ways%20to%20combine%2C%20compose%20and%20chain%20smaller%20solutions.%20%20Like%20we%20do%20with%20Agile%20problem%20solving%20approaches.%20%20Can%27t%20we%20do%20that%20for%20the%20hairy%20reuse%20problem%3F&amp;short_link=http://bit.ly/akyatS&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.shareaholic.com/api/share/?title=The+Elements+of+Reusable+Code&amp;link=http://cwash.org/2009/07/24/the-elements-of-reusable-code/&amp;notes=It%27s%20easier%20to%20make%20progress%20on%20large-scale%20problems%20by%20breaking%20them%20down%2C%20and%20making%20a%20habit%20of%20looking%20for%20patterns%20and%20ways%20to%20combine%2C%20compose%20and%20chain%20smaller%20solutions.%20%20Like%20we%20do%20with%20Agile%20problem%20solving%20approaches.%20%20Can%27t%20we%20do%20that%20for%20the%20hairy%20reuse%20problem%3F&amp;short_link=http://bit.ly/akyatS&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=6&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.shareaholic.com/api/share/?title=The+Elements+of+Reusable+Code&amp;link=http://cwash.org/2009/07/24/the-elements-of-reusable-code/&amp;notes=It%27s%20easier%20to%20make%20progress%20on%20large-scale%20problems%20by%20breaking%20them%20down%2C%20and%20making%20a%20habit%20of%20looking%20for%20patterns%20and%20ways%20to%20combine%2C%20compose%20and%20chain%20smaller%20solutions.%20%20Like%20we%20do%20with%20Agile%20problem%20solving%20approaches.%20%20Can%27t%20we%20do%20that%20for%20the%20hairy%20reuse%20problem%3F&amp;short_link=http://bit.ly/akyatS&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=4&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=The+Elements+of+Reusable+Code&amp;link=http://cwash.org/2009/07/24/the-elements-of-reusable-code/&amp;notes=It%27s%20easier%20to%20make%20progress%20on%20large-scale%20problems%20by%20breaking%20them%20down%2C%20and%20making%20a%20habit%20of%20looking%20for%20patterns%20and%20ways%20to%20combine%2C%20compose%20and%20chain%20smaller%20solutions.%20%20Like%20we%20do%20with%20Agile%20problem%20solving%20approaches.%20%20Can%27t%20we%20do%20that%20for%20the%20hairy%20reuse%20problem%3F&amp;short_link=http://bit.ly/akyatS&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=The+Elements+of+Reusable+Code&amp;link=http://cwash.org/2009/07/24/the-elements-of-reusable-code/&amp;notes=It%27s%20easier%20to%20make%20progress%20on%20large-scale%20problems%20by%20breaking%20them%20down%2C%20and%20making%20a%20habit%20of%20looking%20for%20patterns%20and%20ways%20to%20combine%2C%20compose%20and%20chain%20smaller%20solutions.%20%20Like%20we%20do%20with%20Agile%20problem%20solving%20approaches.%20%20Can%27t%20we%20do%20that%20for%20the%20hairy%20reuse%20problem%3F&amp;short_link=http://bit.ly/akyatS&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=The+Elements+of+Reusable+Code&amp;link=http://cwash.org/2009/07/24/the-elements-of-reusable-code/&amp;notes=It%27s%20easier%20to%20make%20progress%20on%20large-scale%20problems%20by%20breaking%20them%20down%2C%20and%20making%20a%20habit%20of%20looking%20for%20patterns%20and%20ways%20to%20combine%2C%20compose%20and%20chain%20smaller%20solutions.%20%20Like%20we%20do%20with%20Agile%20problem%20solving%20approaches.%20%20Can%27t%20we%20do%20that%20for%20the%20hairy%20reuse%20problem%3F&amp;short_link=http://bit.ly/akyatS&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=The+Elements+of+Reusable+Code&amp;link=http://cwash.org/2009/07/24/the-elements-of-reusable-code/&amp;notes=It%27s%20easier%20to%20make%20progress%20on%20large-scale%20problems%20by%20breaking%20them%20down%2C%20and%20making%20a%20habit%20of%20looking%20for%20patterns%20and%20ways%20to%20combine%2C%20compose%20and%20chain%20smaller%20solutions.%20%20Like%20we%20do%20with%20Agile%20problem%20solving%20approaches.%20%20Can%27t%20we%20do%20that%20for%20the%20hairy%20reuse%20problem%3F&amp;short_link=http://bit.ly/akyatS&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul><div style="clear: both;"></div></div>

<h3  class="related_post_title">More Related Content</h3><ul class="related_post"><li>August 2, 2008 -- <a href="http://cwash.org/2008/08/02/how-i-escape-the-reuse-trap/" title="How I Escape the &#8220;Reuse Trap&#8221;">How I Escape the &#8220;Reuse Trap&#8221;</a> (0)</li><li>July 8, 2010 -- <a href="http://cwash.org/2010/07/08/transactions-part-1/" title="Transactions, Part 1">Transactions, Part 1</a> (0)</li><li>January 7, 2010 -- <a href="http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/" title="Eliminate Branching (IF Statements) to Produce Better Code">Eliminate Branching (IF Statements) to Produce Better Code</a> (0)</li><li>July 29, 2009 -- <a href="http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/" title="What I&#8217;d Tell Myself About Design If I Were Just Beginning">What I&#8217;d Tell Myself About Design If I Were Just Beginning</a> (5)</li><li>April 15, 2009 -- <a href="http://cwash.org/2009/04/15/osgi-ggity-giggity/" title="OSGi-ggity-Giggity">OSGi-ggity-Giggity</a> (4)</li><li>June 9, 2009 -- <a href="http://cwash.org/2009/06/09/mocking-with-jmockit/" title="Mocking with JMockit">Mocking with JMockit</a> (5)</li><li>June 3, 2009 -- <a href="http://cwash.org/2009/06/03/what-is-hamcrest/" title="What is Hamcrest?">What is Hamcrest?</a> (0)</li><li>January 31, 2009 -- <a href="http://cwash.org/2009/01/31/in-response-to-stackoverflow-38quality-doesnt-matter-that-much-jeff-and-joel/" title="In response to Stackoverflow #38/&#8221;Quality Doesn&#8217;t Matter That Much&#8221; &#8212; Jeff and Joel ">In response to Stackoverflow #38/&#8221;Quality Doesn&#8217;t Matter That Much&#8221; &#8212; Jeff and Joel </a> (3)</li><li>January 13, 2009 -- <a href="http://cwash.org/2009/01/13/on-software-quality/" title="On Software Quality">On Software Quality</a> (8)</li><li>December 1, 2008 -- <a href="http://cwash.org/2008/12/01/24ways/" title="24ways">24ways</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cwash.org/2009/07/24/the-elements-of-reusable-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mocking with JMockit</title>
		<link>http://cwash.org/2009/06/09/mocking-with-jmockit/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=mocking-with-jmockit</link>
		<comments>http://cwash.org/2009/06/09/mocking-with-jmockit/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 05:09:58 +0000</pubDate>
		<dc:creator>Chris Wash</dc:creator>
				<category><![CDATA[Developer Testing]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[CapTech]]></category>
		<category><![CDATA[developer testing]]></category>
		<category><![CDATA[hamcrest]]></category>
		<category><![CDATA[JMockit]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[mock objects]]></category>
		<category><![CDATA[testng]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://cwash.org/?p=322</guid>
		<description><![CDATA[JMockit - overview and example]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://cwash.org/2009/06/09/mocking-with-jmockit/";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><p><strong>Update: </strong> I cleaned up the example based on Rogerio&#8217;s comments.</p>
<p>Recently I stumbled onto <a href="https://jmockit.dev.java.net/">JMockit</a> and have been pretty impressed with the flexibility of the approach it takes.</p>
<p>Many mocking frameworks seem to take an elitist attitude toward testable code, not attempting to solve certain problems in favor of guiding one toward a more testable design.  It appears JMockit is a response to this.<span id="more-322"></span></p>
<p>There&#8217;s no getting around the fact that some frameworks, especially legacy or proprietary third party modules, are not coded in such a way that it&#8217;s easy to write testable code against them.  Common pain points include:</p>
<ul>
<li>Pervasive use of statics</li>
<li>Lack of dependency injection mechanisms</li>
<li>Creating dependencies inline with the &#8220;new&#8221; keyword</li>
</ul>
<p>All of these issues will pose problems when trying to double-out dependent code for testing purposes.  One approach to solve these problems is to use a dynamic language and metaprogramming constructs to do this kind of doubling.  Testing Java code with JRuby or Groovy has become more and more popular for this very reason.</p>
<p>But a lot of these will introduce a level of language abstraction between your test code and the code under test, and you&#8217;ll have to have developers maintaining a test suite sign on to learn the language being used.</p>
<p>In comes JMockit, which uses the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/instrument/Instrumentation.html">instrumentation</a> features provided with Java5 to perform a lot of the same magic tricks for you.</p>
<p>This also allows you to test things that weren&#8217;t otherwise possible (or are very difficult) and overall I think the programming model is much more in-tune with standard Java programming idioms.  It doesn&#8217;t discriminate against you if you don&#8217;t use dependency injection, but will work with you if you do.  Here&#8217;s a quick example that colleagues <a href="http://www.andypemberton.com">Andy Pemberton</a> and Patrick Cox worked through with me trying out JMockit.</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@RunWith<span style="color: #009900;">&#40;</span>JMockit.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ControllerTest <span style="color: #009900;">&#123;</span><br />
&nbsp;<br />
&nbsp; &nbsp; @Mocked<br />
&nbsp; &nbsp; HttpServletRequest mockHttpServletRequest<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; @Mocked<br />
&nbsp; &nbsp; ServiceRemote mockServiceRemote<span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> test_execute_expectations<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> Expectations<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">/* define in static block */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> Model m <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Model<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.<span style="color: #006633;">setId</span><span style="color: #009900;">&#40;</span>12345l<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mockHttpServletRequest.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;modelId&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> returns<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;12345&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mockServiceRemote.<span style="color: #006633;">getModel</span><span style="color: #009900;">&#40;</span>12345l<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> returns<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mockHttpServletRequest.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;model&quot;</span>, m<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/* simulate setter injection */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Controller c <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Controller<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; c.<span style="color: #006633;">setHttpServletRequest</span><span style="color: #009900;">&#40;</span>mockHttpServletRequest<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; c.<span style="color: #006633;">setServiceRemote</span><span style="color: #009900;">&#40;</span>mockServiceRemote<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/* call code under test */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; c.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/* strict mode will throw exceptions! */</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>You can get the whole project from my <a href="http://github.com/cwash/testJmockitDemo/tree/master">GitHub account</a>.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://cwash.org/2009/06/09/mocking-with-jmockit/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=24&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-gmail">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=52&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Email this via Gmail">Email this via Gmail</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=74&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=207&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=6&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=4&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul><div style="clear: both;"></div></div>

<h3  class="related_post_title">More Related Content</h3><ul class="related_post"><li>February 17, 2009 -- <a href="http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/" title="Don&#8217;t Unit Test Anymore&#8230; No, Really!">Don&#8217;t Unit Test Anymore&#8230; No, Really!</a> (6)</li><li>June 3, 2009 -- <a href="http://cwash.org/2009/06/03/what-is-hamcrest/" title="What is Hamcrest?">What is Hamcrest?</a> (0)</li><li>January 31, 2009 -- <a href="http://cwash.org/2009/01/31/in-response-to-stackoverflow-38quality-doesnt-matter-that-much-jeff-and-joel/" title="In response to Stackoverflow #38/&#8221;Quality Doesn&#8217;t Matter That Much&#8221; &#8212; Jeff and Joel ">In response to Stackoverflow #38/&#8221;Quality Doesn&#8217;t Matter That Much&#8221; &#8212; Jeff and Joel </a> (3)</li><li>November 28, 2008 -- <a href="http://cwash.org/2008/11/28/must-havesreferences-for-modern-java-ee-developers/" title="Must Haves/References For Modern Java EE Developers">Must Haves/References For Modern Java EE Developers</a> (1)</li><li>July 8, 2010 -- <a href="http://cwash.org/2010/07/08/transactions-part-1/" title="Transactions, Part 1">Transactions, Part 1</a> (0)</li><li>January 7, 2010 -- <a href="http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/" title="Eliminate Branching (IF Statements) to Produce Better Code">Eliminate Branching (IF Statements) to Produce Better Code</a> (0)</li><li>July 29, 2009 -- <a href="http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/" title="What I&#8217;d Tell Myself About Design If I Were Just Beginning">What I&#8217;d Tell Myself About Design If I Were Just Beginning</a> (5)</li><li>July 24, 2009 -- <a href="http://cwash.org/2009/07/24/the-elements-of-reusable-code/" title="The Elements of Reusable Code">The Elements of Reusable Code</a> (0)</li><li>April 15, 2009 -- <a href="http://cwash.org/2009/04/15/osgi-ggity-giggity/" title="OSGi-ggity-Giggity">OSGi-ggity-Giggity</a> (4)</li><li>November 19, 2008 -- <a href="http://cwash.org/2008/11/19/java-6-and-maven-209-on-leopard/" title="Java 6 and Maven 2.0.9 on Leopard">Java 6 and Maven 2.0.9 on Leopard</a> (7)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cwash.org/2009/06/09/mocking-with-jmockit/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>What is Hamcrest?</title>
		<link>http://cwash.org/2009/06/03/what-is-hamcrest/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=what-is-hamcrest</link>
		<comments>http://cwash.org/2009/06/03/what-is-hamcrest/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 10:37:54 +0000</pubDate>
		<dc:creator>Chris Wash</dc:creator>
				<category><![CDATA[Developer Testing]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[assertThat]]></category>
		<category><![CDATA[CapTech]]></category>
		<category><![CDATA[developer testing]]></category>
		<category><![CDATA[hamcrest]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://cwash.org/?p=220</guid>
		<description><![CDATA[What is Hamcrest?  An introduction to the constraint/matcher/predicate framework for Java.]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://cwash.org/2009/06/03/what-is-hamcrest/";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><p>As of JUnit 4.4, if you happen to peek into the distributed JAR you&#8217;ll notice something a little off: in addition to the org.junit.* packages there is this a funny-looking org.hamcrest.* package sticking out like a sore thumb.  You may have seen other projects pick up a dependency on Hamcrest lately as well, and I bet you&#8217;re wondering what it is.  Let&#8217;s get to the bottom of it.<span id="more-220"></span></p>
<h3>Introduction</h3>
<p>The &#8220;I&#8217;m Feeling Lucky&#8221; Google search for the term <em>hamcrest</em> currently takes you to the project&#8217;s Google Code page that states Hamcrest:</p>
<blockquote><p>Provides a library of matcher objects (also known as constraints or predicates) allowing &#8216;match&#8217; rules to be defined declaratively, to be used in other frameworks.</p></blockquote>
<p>That&#8217;s a pretty good description if you already know what a Matcher is&#8230; but it doesn&#8217;t explain much for a beginner.  So what does Hamcrest really do for you?  </p>
<div id="attachment_221" class="wp-caption alignright" style="width: 210px"><a href="http://cwash.org/wp-content/uploads/2009/05/sammy.jpg"><img class="size-full wp-image-221" title="Sammy Stevens" src="http://cwash.org/wp-content/uploads/2009/05/sammy.jpg" alt="Sammy Stevens - Flea Market!" width="200" height="157" /></a><p class="wp-caption-text">It&#39;s just like... It&#39;s just like... A mini... REGEX!</p></div>
<p>A good way to think of it is that <strong>Hamcrest is to objects what regular expressions are to text</strong>.<br />
 Hamcrest provides you with a set of methods that effectively define a <strong>DSL</strong> to do <em>pattern-matching on objects</em>.</p>
<p>What can you use it for?  Well, like regular expressions, the possibilities are endless.  Some really novel uses have come about recently, but the most prominent use has been in the area of testing &#8211; hence the JUnit dependency.  </p>
<h3>A Brief History</h3>
<p>Hamcrest evolved out of the library JMock, being used to write specialized constraints on what you expected to happen to a Mock object when standing in for a real implementation.  A more fluent assertion syntax arose from these constraints which allowed you to chain together main constraint calls under a single <strong>assertThat</strong> method.  Later JMock&#8217;s author, Joe Walnes, refactored the constraint API out into its own library called Hamcrest as people became interested in using it outside of a testing framework for all kinds of different things.  He also started calling the constraints &#8220;matchers&#8221; though they can go by both names or, in some circles, &#8220;predicates.&#8221;  Other testing frameworks and even JUnit, notorious for not requiring any dependencies and staying conceptually small, have picked it up.</p>
<p>All those old friendly org.junit.Assert.* static methods are still around in JUnit 4.4+.  They&#8217;re useful for certain things, but by and large they&#8217;ve fallen out of style.  The new kid on the block, JUnit&#8217;s <strong>assertThat</strong> method allows you to pass a Matcher in to create an assertion that you would otherwise use a specialized-assert method.  Why is assertThat so useful?  A few different reasons.  Not only does it provide a more generic way to specify assertions, it also alows the framework to know specifically what you&#8217;re trying to assert.  That allows the framework to generate failure messages for you.</p>
<h3>Fail message&#8230; win?</h3>
<p>It&#8217;s always been a good idea to use the overridden version of assertX method that also takes a String for a failure message, for example:</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">assertTrue<span style="color: #009900;">&#40;</span>blackbeard.<span style="color: #006633;">getOccupations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pirate&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <br />
&nbsp; &nbsp; blackbeard.<span style="color: #006633;">getOccupations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;captain&quot;</span><span style="color: #009900;">&#41;</span>, <br />
&nbsp; &nbsp; <span style="color: #0000ff;">&quot;Expected Blackbeard to be a pirate or a captain&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>If we passed in an object representing a childhood version of Blackbeard, say, (before he took to the high-seas) then we&#8217;d expect to see our failure message:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">java.lang.AssertionError: Expected Blackbeard to be a pirate or a captain</div></div>
<p>It&#8217;s always been a good idea to provide these messages.  But there&#8217;s no getting around the fact that they&#8217;re repetitive, tedious, and redundant.</p>
<p>Using Hamcrest matchers alongside assertThat makes JUnit smart enough to generate meaningful failure messages for you: writing your assertion with <strong>Hamcrest matchers provides the framework with enough contextual information to generate meaningful failure messages on your behalf</strong>.  No need to maintain an extra arbitrary String to make sense of your failed assertions:</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">assertThat<span style="color: #009900;">&#40;</span>blackbeard.<span style="color: #006633;">getOccupations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; anyOf<span style="color: #009900;">&#40;</span>hasItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pirate&quot;</span><span style="color: #009900;">&#41;</span>,hasItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;captain&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>This may not look like much of an improvement, but it also doesn&#8217;t read any worse.    We can ditch the extra String parameter as well.  When this fails we&#8217;ll get a pretty cool failure message for free:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">java.lang.AssertionError: <br />
Expected: (a collection containing &quot;pirate&quot; or a collection containing &quot;captain&quot;)<br />
&nbsp; &nbsp; &nbsp;got: &lt; []&gt;</div></div>
<p><em>Note: I&#8217;m using JUnit 4.6 &#8211; in order to get this syntax to work you need to do some static imports:</em></p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">import static org.junit.Assert.*;<br />
import static org.hamcrest.CoreMatchers.*;<br />
import static org.junit.matchers.JUnitMatchers.*;</div></div>
<p>It looks like there&#8217;s still work being done to make this DSL read more fluently in JUnit; something like:</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">assertThat<span style="color: #009900;">&#40;</span>blackbeard.<span style="color: #006633;">getOccupations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; either<span style="color: #009900;">&#40;</span>hasItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pirate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">or</span><span style="color: #009900;">&#40;</span>hasItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;captain&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>Other Cool Stuff</h3>
<h5>Getting Reused</h5>
<p>TestNG, JMock and many other testing frameworks will certainly benefit from what Hamcrest offers, but we&#8217;re starting to see this being used in many other places as well.  I&#8217;ll be interested to see how well matchers work together or are maintained seeing that they&#8217;re relatively easy to create.  Hopefully frameworks will contribute useful matchers back to the project.</p>
<h5>Chaining</h5>
<p>Because the matchers are implemented as a DSL using the <a href="http://martinfowler.com/dslwip/MethodChaining.html">method chaining idiom</a>, they&#8217;re extremely easy to combine and extend.</p>
<h5>Matchers For Regular Expressions</h5>
<p>To bring the blog post full circle, I found this earlier tonight: the <a href="http://code.google.com/p/hamcrest-text-patterns/">Hamcrest Text Patterns project</a>.  Its goal is to have you write Hamcrest-style regular expressions using matchers which will produce much more readable regular expression code.  For example, have a look at this test that I found in the project:</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">PatternMatcher emailAddressMatcher <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PatternMatcher<span style="color: #009900;">&#40;</span>sequence<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; capture<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user&quot;</span>, oneOrMore<span style="color: #009900;">&#40;</span>anyCharacter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, <br />
&nbsp; &nbsp; <span style="color: #0000ff;">&quot;@&quot;</span>, <br />
&nbsp; &nbsp; capture<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;host&quot;</span>, oneOrMore<span style="color: #009900;">&#40;</span>anyCharacter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
PatternMatcher mailToURLMatcher <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PatternMatcher<span style="color: #009900;">&#40;</span>sequence<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; capture<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;scheme&quot;</span>, text<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mailto&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, <br />
&nbsp; &nbsp; <span style="color: #0000ff;">&quot;:&quot;</span>, <br />
&nbsp; &nbsp; capture<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;email&quot;</span>, emailAddressMatcher<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
assertThat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mailto:npryce@users.sf.net&quot;</span>, matchesPattern<span style="color: #009900;">&#40;</span>mailToURLMatcher<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>By providing a friendly name for each capture (user, host, scheme, email) you can provide meaningful messages programmatically about what doesn&#8217;t match up when something doesn&#8217;t match.</p>
<h3>Conclusion</h3>
<p>I hope you choose to take a closer look at Hamcrest because it brings a big bang-for-the-buck and has the potential to guide us to more human-friendly, readable code.</p>
<h3>Further Reading</h3>
<ul>
<li>Have a look at Joe Walnes&#8217; blog for a great <a href="http://joe.truemesh.com/blog/000511.html">introduction to using assertThat</a> as well as other <a href="http://joe.truemesh.com/blog/000705.html">creative uses of Hamcrest matchers</a>.</li>
<li>Also take a look at Nat Pryce&#8217;s blog for <a href="http://www.natpryce.com/articles/000662.html">more info on Hamcrest</a> and Testing in general.</li>
</ul>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;</div></div>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://cwash.org/2009/06/03/what-is-hamcrest/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=24&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-gmail">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=52&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Email this via Gmail">Email this via Gmail</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=74&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=207&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=6&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=4&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul><div style="clear: both;"></div></div>

<h3  class="related_post_title">More Related Content</h3><ul class="related_post"><li>June 9, 2009 -- <a href="http://cwash.org/2009/06/09/mocking-with-jmockit/" title="Mocking with JMockit">Mocking with JMockit</a> (5)</li><li>April 15, 2009 -- <a href="http://cwash.org/2009/04/15/osgi-ggity-giggity/" title="OSGi-ggity-Giggity">OSGi-ggity-Giggity</a> (4)</li><li>November 19, 2008 -- <a href="http://cwash.org/2008/11/19/java-6-and-maven-209-on-leopard/" title="Java 6 and Maven 2.0.9 on Leopard">Java 6 and Maven 2.0.9 on Leopard</a> (7)</li><li>July 8, 2010 -- <a href="http://cwash.org/2010/07/08/transactions-part-1/" title="Transactions, Part 1">Transactions, Part 1</a> (0)</li><li>January 7, 2010 -- <a href="http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/" title="Eliminate Branching (IF Statements) to Produce Better Code">Eliminate Branching (IF Statements) to Produce Better Code</a> (0)</li><li>July 29, 2009 -- <a href="http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/" title="What I&#8217;d Tell Myself About Design If I Were Just Beginning">What I&#8217;d Tell Myself About Design If I Were Just Beginning</a> (5)</li><li>July 24, 2009 -- <a href="http://cwash.org/2009/07/24/the-elements-of-reusable-code/" title="The Elements of Reusable Code">The Elements of Reusable Code</a> (0)</li><li>February 17, 2009 -- <a href="http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/" title="Don&#8217;t Unit Test Anymore&#8230; No, Really!">Don&#8217;t Unit Test Anymore&#8230; No, Really!</a> (6)</li><li>November 28, 2008 -- <a href="http://cwash.org/2008/11/28/must-havesreferences-for-modern-java-ee-developers/" title="Must Haves/References For Modern Java EE Developers">Must Haves/References For Modern Java EE Developers</a> (1)</li><li>September 10, 2008 -- <a href="http://cwash.org/2008/09/10/axis2-client-using-adb-runtime-dependencies/" title="Axis2 client using ADB &#8211; runtime dependencies">Axis2 client using ADB &#8211; runtime dependencies</a> (6)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cwash.org/2009/06/03/what-is-hamcrest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OSGi-ggity-Giggity</title>
		<link>http://cwash.org/2009/04/15/osgi-ggity-giggity/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=osgi-ggity-giggity</link>
		<comments>http://cwash.org/2009/04/15/osgi-ggity-giggity/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 21:40:01 +0000</pubDate>
		<dc:creator>Chris Wash</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[CapTech]]></category>
		<category><![CDATA[introduction]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[modularity]]></category>
		<category><![CDATA[osgi]]></category>
		<category><![CDATA[software development]]></category>

		<guid isPermaLink="false">http://cwash.org/?p=205</guid>
		<description><![CDATA[Thoughts on the OSGi service platform for modularity.]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://cwash.org/2009/04/15/osgi-ggity-giggity/";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><p><strong>Update:</strong> A great article on OSGi popped up on Javalobby today.  <a href="http://java.dzone.com/articles/dozen-osgi-myths-and">Check it out</a>.</p>
<p>I haven&#8217;t yet written any thoughts about OSGi but it&#8217;s something that&#8217;s increasingly found its way on to my radar over the past year and a half or so.  I&#8217;ve been doing a little bit of reading and research on it lately, (a quick introduction can be found via <a href="http://www.infoq.com/interviews/osgi-adrian-colyer">Adrian Colyer&#8217;s talks on InfoQ</a> about it).  Needless to say it&#8217;s got me excited.  Really excited &#8211; to the point where I&#8217;m catching myself geeking out uncontrollably like Quagmire from Family Guy.  What&#8217;s got me all giggity? <span id="more-205"></span> Let&#8217;s take a step back first.</p>
<p>With Java there has always been a focus on modularity, but it&#8217;s come up in different intermediate forms.  Any typical Java developer can go on for hours about the importance for layering and properly isolating, decoupling and packaging components and subsystems.  Java has the idea of a package, and the idea of JARs came about fairly early on in the process.  But what became difficult was that often each layer had to have its own supporting infrastructure in place to be built independently, and someone needed to know how all of those JAR dependencies mapped out at build time and runtime.  This was a necessary evil so that we could have individually deployable modules or subsystems.  Once you got there, it was all worth it.</p>
<p>The problem was that while you may have been diligent and rigorous in your approach to applying how your projects were laid out and built, quite often the framework space solved this problem over and over again in many different ways.  Going to upgrade a library was not an easy task, because you had to typically upgrade it across each of your layers, and what&#8217;s more, there was no consistent approach to manage transitive dependencies amongst the modules each library used.  Library selection and dependency management was the work of a greybeard within your project and it took a while to change a library or upgrade.  Not to mention a near certainty that you&#8217;d need to execute your entire suite of regression tests to make sure nothing broke.</p>
<p>After seeing many Java frameworks being cajoled into a Maven build in order to manage builds and transitive dependencies, there has been increased awareness on the part of the vendor (be it commercial or open source) space on modularity.  But we&#8217;ve only focused on the problem of solving modularity from a build-centric view.  In other words, &#8220;How can I package and manage these build artifacts in such a way that consumers can easily integrate them into their project in a consistent and automated way?&#8221;  This was the pie-in-the-sky idea behind Maven as I see it, and they pretty much made it happen.  You&#8217;d be hard pressed to find a framework today that doesn&#8217;t support it in some fashion.</p>
<p>But there&#8217;s a whole separate piece of modularity that takes place at <em>runtime</em>.  In the context of my historical explanation of modularity within Java, you can look at OSGi&#8217;s ability to manage dependencies dynamically at runtime (via a header in your JAR MANIFEST file) as a &#8220;modularity win&#8221; and icing on the cake.  But OSGi indeed goes farther than that.  OSGi gives you the ability to install/uninstall, start and stop bundles dynamically (think Eclipse plugins) via its lifecycle, and that just plain gives me the &#8220;giggities.&#8221;  To account for truly dynamic modularity OSGi also gives you requires and provides semantics (also declared via headers) akin to the requires and provides semantics you see in dynamic languages like Ruby and some Javascript packaging/dependency management frameworks.  While this may seem like its strength is only in making a clear separation between APIs you use and export, its real strength is that this allows you to truly isolate your modules at runtime from trickery that can (and has) happened on a classloader level.  &#8220;That couldn&#8217;t possibly work on an App server,&#8221; you say?  I was skeptical, too, but not so fast&#8230;</p>
<p>If you&#8217;re the type that keeps up with the JCP space, you probably know there is a JSR &#8220;trinity&#8221; (equally as mysterious as its holy counterpart to the outside viewer) to provide a more robust modularization mechanism in varying degrees.  That &#8220;quagmire&#8221; withstanding, OSGi has been mounting a considerable lead as <em>the</em> modularization system of choice in the JavaSE and EE/App server space.  (It was already a formidable player in the embedded/mobile space, where it evolved from.)  Nearly all major App server vendors have integrated the OSGi model into their core, which means they&#8217;ve probably cleaned up any classloader nastiness they&#8217;ve resorted to in the past.  And now you&#8217;re seeing some of the leading frameworks like Spring and Web Beans/Seam 3 (and subsequently have signed up to publish themselves as OSGi bundles.)  Not to mention the success that Eclipse has seen since refactoring itself to work off its OSGi implementation, Equinox.</p>
<p>If OSGi can continue to make inroads in the framework space,  I think its prospects to the &#8220;in the trenches&#8221; Java developer are very exciting indeed.  Expect me to babble on more about this in the future&#8230;</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;</div></div>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;</div></div>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://cwash.org/2009/04/15/osgi-ggity-giggity/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://www.shareaholic.com/api/share/?title=OSGi-ggity-Giggity&amp;link=http://cwash.org/2009/04/15/osgi-ggity-giggity/&amp;notes=Thoughts%20on%20the%20OSGi%20service%20platform%20for%20modularity.&amp;short_link=http://bit.ly/99Hc01&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=OSGi-ggity-Giggity&amp;link=http://cwash.org/2009/04/15/osgi-ggity-giggity/&amp;notes=Thoughts%20on%20the%20OSGi%20service%20platform%20for%20modularity.&amp;short_link=http://bit.ly/99Hc01&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.shareaholic.com/api/share/?title=OSGi-ggity-Giggity&amp;link=http://cwash.org/2009/04/15/osgi-ggity-giggity/&amp;notes=Thoughts%20on%20the%20OSGi%20service%20platform%20for%20modularity.&amp;short_link=http://bit.ly/99Hc01&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=24&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=OSGi-ggity-Giggity&amp;link=http://cwash.org/2009/04/15/osgi-ggity-giggity/&amp;notes=Thoughts%20on%20the%20OSGi%20service%20platform%20for%20modularity.&amp;short_link=http://bit.ly/99Hc01&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-gmail">
			<a href="http://www.shareaholic.com/api/share/?title=OSGi-ggity-Giggity&amp;link=http://cwash.org/2009/04/15/osgi-ggity-giggity/&amp;notes=Thoughts%20on%20the%20OSGi%20service%20platform%20for%20modularity.&amp;short_link=http://bit.ly/99Hc01&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=52&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Email this via Gmail">Email this via Gmail</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.shareaholic.com/api/share/?title=OSGi-ggity-Giggity&amp;link=http://cwash.org/2009/04/15/osgi-ggity-giggity/&amp;notes=Thoughts%20on%20the%20OSGi%20service%20platform%20for%20modularity.&amp;short_link=http://bit.ly/99Hc01&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=74&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.shareaholic.com/api/share/?title=OSGi-ggity-Giggity&amp;link=http://cwash.org/2009/04/15/osgi-ggity-giggity/&amp;notes=Thoughts%20on%20the%20OSGi%20service%20platform%20for%20modularity.&amp;short_link=http://bit.ly/99Hc01&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.shareaholic.com/api/share/?title=OSGi-ggity-Giggity&amp;link=http://cwash.org/2009/04/15/osgi-ggity-giggity/&amp;notes=Thoughts%20on%20the%20OSGi%20service%20platform%20for%20modularity.&amp;short_link=http://bit.ly/99Hc01&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=207&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=OSGi-ggity-Giggity&amp;link=http://cwash.org/2009/04/15/osgi-ggity-giggity/&amp;notes=Thoughts%20on%20the%20OSGi%20service%20platform%20for%20modularity.&amp;short_link=http://bit.ly/99Hc01&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.shareaholic.com/api/share/?title=OSGi-ggity-Giggity&amp;link=http://cwash.org/2009/04/15/osgi-ggity-giggity/&amp;notes=Thoughts%20on%20the%20OSGi%20service%20platform%20for%20modularity.&amp;short_link=http://bit.ly/99Hc01&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=6&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.shareaholic.com/api/share/?title=OSGi-ggity-Giggity&amp;link=http://cwash.org/2009/04/15/osgi-ggity-giggity/&amp;notes=Thoughts%20on%20the%20OSGi%20service%20platform%20for%20modularity.&amp;short_link=http://bit.ly/99Hc01&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=4&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=OSGi-ggity-Giggity&amp;link=http://cwash.org/2009/04/15/osgi-ggity-giggity/&amp;notes=Thoughts%20on%20the%20OSGi%20service%20platform%20for%20modularity.&amp;short_link=http://bit.ly/99Hc01&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=OSGi-ggity-Giggity&amp;link=http://cwash.org/2009/04/15/osgi-ggity-giggity/&amp;notes=Thoughts%20on%20the%20OSGi%20service%20platform%20for%20modularity.&amp;short_link=http://bit.ly/99Hc01&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=OSGi-ggity-Giggity&amp;link=http://cwash.org/2009/04/15/osgi-ggity-giggity/&amp;notes=Thoughts%20on%20the%20OSGi%20service%20platform%20for%20modularity.&amp;short_link=http://bit.ly/99Hc01&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=OSGi-ggity-Giggity&amp;link=http://cwash.org/2009/04/15/osgi-ggity-giggity/&amp;notes=Thoughts%20on%20the%20OSGi%20service%20platform%20for%20modularity.&amp;short_link=http://bit.ly/99Hc01&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul><div style="clear: both;"></div></div>

<h3  class="related_post_title">More Related Content</h3><ul class="related_post"><li>July 24, 2009 -- <a href="http://cwash.org/2009/07/24/the-elements-of-reusable-code/" title="The Elements of Reusable Code">The Elements of Reusable Code</a> (0)</li><li>June 3, 2009 -- <a href="http://cwash.org/2009/06/03/what-is-hamcrest/" title="What is Hamcrest?">What is Hamcrest?</a> (0)</li><li>November 28, 2008 -- <a href="http://cwash.org/2008/11/28/must-havesreferences-for-modern-java-ee-developers/" title="Must Haves/References For Modern Java EE Developers">Must Haves/References For Modern Java EE Developers</a> (1)</li><li>November 19, 2008 -- <a href="http://cwash.org/2008/11/19/java-6-and-maven-209-on-leopard/" title="Java 6 and Maven 2.0.9 on Leopard">Java 6 and Maven 2.0.9 on Leopard</a> (7)</li><li>March 19, 2008 -- <a href="http://cwash.org/2008/03/19/new-wave-logging/" title="New Wave Logging">New Wave Logging</a> (0)</li><li>July 8, 2010 -- <a href="http://cwash.org/2010/07/08/transactions-part-1/" title="Transactions, Part 1">Transactions, Part 1</a> (0)</li><li>January 7, 2010 -- <a href="http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/" title="Eliminate Branching (IF Statements) to Produce Better Code">Eliminate Branching (IF Statements) to Produce Better Code</a> (0)</li><li>July 29, 2009 -- <a href="http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/" title="What I&#8217;d Tell Myself About Design If I Were Just Beginning">What I&#8217;d Tell Myself About Design If I Were Just Beginning</a> (5)</li><li>June 9, 2009 -- <a href="http://cwash.org/2009/06/09/mocking-with-jmockit/" title="Mocking with JMockit">Mocking with JMockit</a> (5)</li><li>January 31, 2009 -- <a href="http://cwash.org/2009/01/31/in-response-to-stackoverflow-38quality-doesnt-matter-that-much-jeff-and-joel/" title="In response to Stackoverflow #38/&#8221;Quality Doesn&#8217;t Matter That Much&#8221; &#8212; Jeff and Joel ">In response to Stackoverflow #38/&#8221;Quality Doesn&#8217;t Matter That Much&#8221; &#8212; Jeff and Joel </a> (3)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cwash.org/2009/04/15/osgi-ggity-giggity/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

