<?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>Eric Feminella &#187; Code Review</title>
	<atom:link href="http://www.ericfeminella.com/blog/category/code-review/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ericfeminella.com/blog</link>
	<description>Thoughts on Software Design and Development</description>
	<lastBuildDate>Mon, 06 Feb 2012 06:00:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Circumventing Conditional Comparisons</title>
		<link>http://www.ericfeminella.com/blog/2011/10/14/circumventing-conditional-comparisons/</link>
		<comments>http://www.ericfeminella.com/blog/2011/10/14/circumventing-conditional-comparisons/#comments</comments>
		<pubDate>Fri, 14 Oct 2011 23:36:37 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[APIs]]></category>
		<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Object Oriented Design]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[conditional comparison]]></category>
		<category><![CDATA[equality operator]]></category>
		<category><![CDATA[identity operator]]></category>
		<category><![CDATA[javascript equality operator]]></category>
		<category><![CDATA[javascript identity operator]]></category>

		<guid isPermaLink="false">http://www.ericfeminella.com/blog/?p=2800</guid>
		<description><![CDATA[Often during the course of my day I come across code which evaluates the same conditional comparisons in multiple contexts. Understandably, this is rather typical of most software systems, and while it may only introduce a negligible amount of technical dept (in the form of redundancy) for smaller systems, that dept can grow considerably in [...]]]></description>
			<content:encoded><![CDATA[<p>Often during the course of my day I come across code which evaluates the same conditional comparisons in multiple contexts. Understandably, this is rather typical of most software systems, and while it may only introduce a negligible amount of technical dept (in the form of redundancy) for smaller systems, that dept can grow considerably in more complex, large scale applications. From a design perspective, this issue is applicable to nearly every language.</p>
<p>For example, consider a simple <code>Compass</code> class which defines just one public property, &#8220;direction&#8221; and, four constants representing each cardinal direction: North, East, South and West, respectively. In JavaScript, this could be defined simply as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">var</span> Compass = <span class="kw2">function</span><span class="br0">&#40;</span>direction<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">direction</span> = direction;<br />
<span class="br0">&#125;</span><br />
Compass.<span class="me1">NORTH</span> = <span class="st0">&quot;North&quot;</span>;<br />
Compass.<span class="me1">EAST</span> &nbsp;= <span class="st0">&quot;East&quot;</span>;<br />
Compass.<span class="me1">SOUTH</span> = <span class="st0">&quot;South&quot;</span>;<br />
Compass.<span class="me1">WEST</span> &nbsp;= <span class="st0">&quot;West&quot;</span>;</div>
<p>Technically, there is nothing problematic with the above class signature; the defined constants certainly provide a much better design than conditional comparisons against literal strings throughout implementation code. That being said, this design does lead to redundancy as every instance of <code>Compass</code> which needs to evaluate the state of <code>direction</code> requires conditional comparisons. </p>
<p>For example, to test for <code>Compass.North</code>, typically, client code must be implemented as follows: </p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">if</span> <span class="br0">&#40;</span>compass.<span class="me1">direction</span> === Compass.<span class="me1">NORTH</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <br />
&nbsp; &nbsp; <span class="co1">//&#8230;</span><br />
<span class="br0">&#125;</span></div>
<p>Likewise, simular comparisons would need to be implemented for each cardinal direction. And, while this may seem trivial for a class as simple as the <code>Compass</code> example, it does become a maintenance issue for more complex implementations. </p>
<p>With this in mind, we can simplify client code by defining each state as a specific method of <code>Compass</code>. In doing so, we afford our code the benefit of exercising (unit testing) <code>Compass</code> exclusively. This alone improves maintainability while also simplifying client code which depends on <code>Compass</code>. As such, <code>Compass</code> could be refactored to:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">var</span> Compass = <span class="kw2">function</span><span class="br0">&#40;</span>direction<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">direction</span> = direction;<br />
<span class="br0">&#125;</span><br />
Compass.<span class="me1">prototype</span> = <span class="br0">&#123;</span><br />
&nbsp; &nbsp; isNorth: <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">direction</span> === Compass.<span class="me1">NORTH</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; isWest: <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">direction</span> === Compass.<span class="me1">WEST</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; isSouth: <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">direction</span> === Compass.<span class="me1">SOUTH</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; isEast: <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">direction</span> === Compass.<span class="me1">EAST</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>Based on the above implementation of <code>Compass</code>, the previous conditional comparison can be refactored as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">if</span> <span class="br0">&#40;</span>compass.<span class="me1">isNorth</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <br />
&nbsp; &nbsp; <span class="co1">//&#8230;</span><br />
<span class="br0">&#125;</span></div>
<h3>Comparator API</h3>
<p>To simplify implementing conditional comparisons, I have provided a simple <code>Comparator</code> API that defines a single static method: <code>Comparator.each</code>, which allows for augmenting existing objects with comparison methods. <code>Comparator.each</code> can be invoked with three arguments as follows:</p>
<table class="api-doc-table" cellspacing="0">
<thead></thead>
<tbody>
<tr class="api-method">
<td>type
<td class="default"></td>
</tr>
<tr>
<td colspan="2" class="desc">The Class to which the comparison methods are to be added.</td>
</tr>
<tr class="api-method">
<td>property</td>
<td class="default"></td>
</tr>
<tr>
<td colspan="2" class="desc">The property against which the comparisons are to be made. If the property has not been defined it, too, will be added.</td>
</tr>
<tr class="api-method">
<td>values</td>
<td class="default"></td>
</tr>
<tr>
<td colspan="2" class="desc">An <code>Array</code> of constants where each value will be used to create a new comparison method (prefixed with &#8220;is&#8221;). If the constants specified are Strings, typically an <code>Array</code> containing each constant should suffice. For example, passing <code>[Foo.BAR]</code> where <code>BAR</code> equals &#8220;Bar&#8221; would result in an <code>isBar()</code> method being created. To specify custom comparison method names, an <code>Object</code> of name/value pairs can be used where each name defines the name of the method added and the value is the constant evaluated by the method. This is useful for constants which are not strings. For example, <code>{isIOS421: DeviceVersion.IOS_4_2_1}</code> where <code>IOS_4_2_1</code> equals 4.2.1 would result in an <code>isIOS421()</code> method being created.</td>
</tr>
</table>
<p>Taking the <code>Compass</code> example, the previous comparison methods could be augmented without the need to explicitly define them via <code>Comparator.each</code>:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">var</span> Compass = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">direction</span> = direction;<br />
<span class="br0">&#125;</span><br />
Compass.<span class="me1">NORTH</span> = <span class="st0">&quot;North&quot;</span>;<br />
Compass.<span class="me1">EAST</span> &nbsp;= <span class="st0">&quot;East&quot;</span>;<br />
Compass.<span class="me1">SOUTH</span> = <span class="st0">&quot;South&quot;</span>;<br />
Compass.<span class="me1">WEST</span> &nbsp;= <span class="st0">&quot;West&quot;</span>;<br />
Comparator.<span class="me1">each</span><span class="br0">&#40;</span> Compass, <span class="st0">&quot;direction&quot;</span>, <span class="br0">&#91;</span>Compass.<span class="me1">NORTH</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Compass.<span class="me1">WEST</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Compass.<span class="me1">SOUTH</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Compass.<span class="me1">EAST</span><span class="br0">&#93;</span> <span class="br0">&#41;</span>;</div>
<p>The above results in the comparison methods <code>isNorth</code>, <code>isEast</code>, <code>isSouth</code> and <code>isWest</code> being added to the <code>Compass</code> type.</p>
<p><strong>Comparator</strong>: <span class="figure"><a href="http://code.ericfeminella.com/apis/javascript/comparator/js/comparator.js" target="_blank">source</a> | <a href="http://code.ericfeminella.com/apis/javascript/comparator/js/comparator-min.js" target="_blank">min</a> | <a href="http://code.ericfeminella.com/apis/javascript/comparator/tests/comparator-test.js" target="_blank">test</a> (<a href="http://code.ericfeminella.com/apis/javascript/comparator" target="_blank">run</a>)</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ericfeminella.com/blog/2011/10/14/circumventing-conditional-comparisons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multiscreen Software Design</title>
		<link>http://www.ericfeminella.com/blog/2011/03/06/multiscreen-software-design/</link>
		<comments>http://www.ericfeminella.com/blog/2011/03/06/multiscreen-software-design/#comments</comments>
		<pubDate>Sun, 06 Mar 2011 16:21:10 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[APIs]]></category>
		<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Object Oriented Design]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[Tablets]]></category>
		<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[User Experience Design]]></category>

		<guid isPermaLink="false">http://www.ericfeminella.com/blog/?p=1852</guid>
		<description><![CDATA[&#8220;We are in the midst of a revolution across a variety of screens&#8221; You may recall first hearing the notion of a &#8220;Multiscreen Revolution&#8221; during the keynote at Max 2010. If you take a moment and think about it that&#8217;s a rather profound statement, and by all apparent indications a very true one. This is [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>&#8220;We are in the midst of a revolution across a variety of screens&#8221;</p></blockquote>
<p>You may recall first hearing the notion of a &#8220;Multiscreen Revolution&#8221; during the keynote at Max 2010. If you take a moment and think about it that&#8217;s a rather profound statement, and by all apparent indications a very true one.</p>
<p>This is also how <a href="http://www.adobe.com/aboutadobe/pressroom/executivebios/kevinlynch.html" target="_blank">Kevin Lynch</a>, CTO at Adobe, begins his recent article, aptly titled  &#8220;<a href="http://blogs.adobe.com/conversations/2011/02/the-multiscreen-revolution.html" target="_blank">The Multiscreen Revolution</a>&#8221; in which Kevin provides a succinct breakdown covering the driving forces behind this revolution and how it is guiding the future of the Flash Platform. </p>
<p>Allow me to digress for a moment as, in a way, for me at least, the &#8220;Multiscreen Revolution&#8221; tends to conjure up the hypothetical notion of a <a href="http://www.sciencedaily.com/releases/2010/01/100112165249.htm" target="_blank">Multiverse</a>. This may be a suitable analogy I suppose as  we have been focused in a predominantly Personal Computer based reality for many years, and while this remains relevant, it is no longer exclusive. </p>
<p>I have been giving a lot of thought lately regarding designing software in a multi form factor paradigm and felt I would share some initial thoughts on the subject. Keep in mind much of this is still quite new and subject to change; however, I have made an attempt to isolate what I feel will remain constant moving forward, particularly in the context of developing native applications with the Flash Platform across a variety of screens. </p>
<h3>First, User Experience Design</h3>
<p>My initial thoughts on the implications of what a Multiscreen paradigm will have on the way we think about the design of software are primarily concerned with <a href="http://en.wikipedia.org/wiki/User_experience_design" target="_blank">User Experience Design</a> (UX Design). While simply using <a href="http://www.w3.org/TR/css3-mediaqueries/" target="_blank">CSS3 media queries</a> to facilitate dynamic runtime layouts will be needed for most HTML based web applications, I do not believe these types of solutions alone will allow for the kinds of compelling experiences users have come to expect. Sure they are useful, and for some HTML based websites they may suffice. However, in the context of more complex applications, RIAs in particular, but also just about every application developed specifically for a PC, too, I believe UX Design will need to encompass the best of what a particular form factor, &#8220;screen&#8221; or whatever you prefer to call it, has to offer, be it a <a href="http://en.wikipedia.org/wiki/Personal_computer" target="_blank">PC</a>, <a href="http://en.wikipedia.org/wiki/Smartphone" target="_blank">smartphone</a>, <a href="http://en.wikipedia.org/wiki/Tablet_device" target="_blank">tablet</a> or <a href="http://en.wikipedia.org/wiki/Internet_television" target="_blank">TV</a>. </p>
<p>For example, it is extremely rare that a UX Design intended for users of a PC will translate directly to a Mobile or Tablet User Experience. The interactions of a traditional physical keyboard and mouse do not equate to those of <a href="http://www.mobiledia.com/glossary/228.html" target="_blank">soft keys</a>, <a href="http://en.m.wikipedia.org/wiki/Virtual_keyboard" target="_blank">virtual keyboards</a> and <a href="http://www.lukew.com/touch/TouchGestureGuide.pdf" target="_blank">touch gesture</a> interactions. Moreover, the navigation and transitions between different views and even certain concepts and metaphors are completely different. In simplest terms; it&#8217;s not &#8220;Apples to Apples&#8221;, as the expression goes.</p>
<p>With this in mind, UX Design should remain at the forefront of Software Design, and in order for that to happen UX Design will not only need to continue to reflect the needs of users, but also by leverage the capabilities of the particular devices and screens on which an application will run. This is necessary as it better serves the needs of users in new and useful ways. Likewise, UX Design will need to account for the limitations (resources in particular) of those same form factors.</p>
<h3>Second, Architecture</h3>
<p>Mulitiscreen design obviously poses some new challenges considering the growing number of form factors which will need to be taken into account. The good news is most existing well designed software architectures have been designed with this in mind all along. That is, the key factor in managing this complexity I believe will be code reuse. A common theme amongst many of my posts, code reuse has many obvious benefits, and in the context of Multiscreen concerns it will allow for different screen specific applications to leverage general, well defined and well tested APIs. Code reuse will certainly be of tremendous value when considering the complexities encountered with Multiscreen design. Such shared APIs can be reused across applications which are designed for particular screens or extended to provide screen / device specific implementations.</p>
<p>For example, the Architectures I have worked on are designed such that each is broken out into specific modules (libraries) which, on a high level, are typically as follows:</p>
<ul>
<li><strong>unittesting-support</strong>: Provides convenience extensions of unit testing and mock frameworks.</li>
<li><strong>commons:</strong> Provides all generic, reusable APIs which have no dependencies outside of those of the Flex Framework and Flash Player API. </li>
<li><strong>framework-support:</strong> Provides reusable extensions of a particular framework. There can be multiple framework-support libraries, each of which would be specific to an individual framework; e.g. parsley-support, swiz-support, robotlegs-support, cairngorm-support, springactionscript-support etc.</li>
<li><strong>business:</strong> Provides domain dependent, business specific reusable APIs which are common amongst all projects within the business domain. This includes domain models, shared services, Presentation Models, UI components and anything else which is specific to the business domain. While all of the previously listed libraries could be used with any AS3 / Flex project, the business library is intentionally coupled to the domain.</li>
</ul>
<p>All of the above projects are used by the different business applications, allowing for significantly reduced complexity of each individual application. Moreover, each library provides isolated test coverage which allows for a greater sense of confidence when building applications which are dependent on them. This type of structure also lends itself well with common SCM and build conventions, allowing for simplified branching and versioning.</p>
<p>Architectures designed similar to what I have described above I would consider to be &#8220;multiscreen ready&#8221; (provided they are optimized and efficient) in that much of the underlying implementation has already been completed, tested and proven. What&#8217;s left is the mobile, tablet or TV application designs which should be mainly concerned with UX, particularly interactions, navigation and device capabilities. From these screen specific UX Designs the application architecture is mainly focused around view concerns specific to those devices; leveraging the existing APIs as needed. This is where the <a href="http://www.martinfowler.com/eaaDev/PresentationModel.html" target="_blank">Presentation Model Pattern</a> (which I have been recommending for years) or similar patterns will be of great value. </p>
<p>I also anticipate additional libraries being abstracted in addition to those I have listed above as a result of these device specific projects being developed. For example, I could easily imagine &#8220;device-support&#8221;, &#8220;mobile-support&#8221; and &#8220;tablet-support&#8221; projects which would provide reusable APIs specific for those screens so as to be leveraged across applications. In fact, I am working on libraries such as these at the moment.</p>
<p>Reusable libraries are nothing new; however, their role now is perhaps even more important as it is quite likely that multiple implementations of the same application will be needed for the various form factors and contexts. Existing reusable libraries may also need to be further optimized to provide the best performance against the slowest devices in order to be considered suitable for devices with the most limited resources. A natural side effect of such optimizations is that implementations of an application targeting the fastest form factors (typically, PCs) will benefit greatly.</p>
<h3>Some Concluding Thoughts</h3>
<p>In short, I believe both users and developers alike will be best served by providing unique User Experiences for specific screens as opposed to attempting to adapt the same application across multiple screens. One of the easiest ways of managing the complexity of multiscreen design and development will inevitably be code reuse. </p>
<p>I also believe the main point of focus should be on the medium and small form factors; i.e. Tablets and Smart phones. Not only for the more <a href="http://www.lukew.com/ff/entry.asp?933" target="_blank">common reasons</a> but, also because I believe PCs and Laptops will eventually be used almost exclusively for developing the applications which run on the other form factors. In fact, I can say this from my own experiences already.</p>
<p>While there is still much to learn in the area of Multiscreen Design, I feel the ideas I&#8217;ve expressed here will remain relevant. Over the course of the coming months I plan to dedicate much of my time towards further exploration of this topic and will certainly continue to share my findings.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ericfeminella.com/blog/2011/03/06/multiscreen-software-design/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Practices of an Agile Developer</title>
		<link>http://www.ericfeminella.com/blog/2011/02/10/practices-of-an-agile-developer/</link>
		<comments>http://www.ericfeminella.com/blog/2011/02/10/practices-of-an-agile-developer/#comments</comments>
		<pubDate>Thu, 10 Feb 2011 22:24:04 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[APIs]]></category>
		<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Continuous Integration]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Object Oriented Design]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[Test Driven Development]]></category>

		<guid isPermaLink="false">http://www.ericfeminella.com/blog/?p=1753</guid>
		<description><![CDATA[Of the many software engineering books I have read over the years, Practices of an Agile Developer in particular continues to be one book I find myself turning to time and time again for inspiration. Written by two of my favorite technical authors, Andy Hunt and Venkat Subramaniam, and published as part of the Pragmatic [...]]]></description>
			<content:encoded><![CDATA[<p>Of the many software engineering books I have read over the years, <a href="http://pragprog.com/titles/pad/practices-of-an-agile-developer" target="_blank">Practices of an Agile Developer</a> in particular continues to be one book I find myself turning to time and time again for inspiration.</p>
<p>Written by two of my favorite technical authors, <a href="http://blog.toolshed.com/" target="_blank">Andy Hunt</a> and <a href="http://www.agiledeveloper.com/aboutus.html" target="_blank">Venkat Subramaniam</a>, and published as part of the <a href="http://pragprog.com/" target="_blank">Pragmatic Bookshelf</a>, Practices of an Agile Developer provides invaluable, practical and highly inspirational solutions to the most common challenges we as software engineers face project after project.</p>
<p>What makes Practices of an Agile Developer something truly special is the simplicity and easy to digest format in which it is written; readers can jump in at any chapter, or practically any page for that matter, and easily learn something new and useful in a matter of minutes. </p>
<p>While covering many of the most common subjects on software development, as well as many particularly unique subjects, it is the manner in which the subjects are presented that makes the book itself quite unique. The chapters are formatted such that each provides an &#8220;<em>Angel vs. Devil on your shoulders</em>&#8221; perspective of each topic. This is quite useful as one can briefly reference any topic to take away something useful by simply reading the chapters title and the &#8220;Angel vs. Devil&#8221; advice, and from that come to a quick understanding of the solution. Moreover, each chapter also provides tips on &#8220;<em>How it Feels</em>&#8221; when following one of the prescribed approaches. The &#8220;How it feels&#8221; approach is very powerful in that it instantly draws readers in for more detailed explanations. Complimentary to this is the &#8220;<em>Keeping your balance</em>&#8221; suggestions which provide useful insights to many of the challenges one might face when trying to apply the learnings of a particular subject. &#8220;Keeping your Balance&#8221; tips answer questions which would otherwise be left to the reader to figure out.</p>
<p>I first read Practices of an Agile Developer almost 4 years ago, and to this day I regularly find myself returning to it time and time again for inspiration. A seminal text by all means, I highly recommend it as a must read for Software Developers of all levels and disciplines.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ericfeminella.com/blog/2011/02/10/practices-of-an-agile-developer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Domain Models and Value Objects</title>
		<link>http://www.ericfeminella.com/blog/2010/12/05/domain-models-and-value-objects/</link>
		<comments>http://www.ericfeminella.com/blog/2010/12/05/domain-models-and-value-objects/#comments</comments>
		<pubDate>Sun, 05 Dec 2010 20:19:10 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Object Oriented Design]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[Test Driven Development]]></category>

		<guid isPermaLink="false">http://www.ericfeminella.com/blog/?p=1658</guid>
		<description><![CDATA[The other day a friend asked me what the difference between a VO and a Domain Model was, and when I would suggest using one over the other? Since I actually get asked this very same question quite often, I thought it would be useful to provide a brief definition in the context of the [...]]]></description>
			<content:encoded><![CDATA[<p>The other day a friend asked me what the difference between a VO and a Domain Model was, and when I would suggest using one over the other? Since I actually get asked this very same question quite often, I thought it would be useful to provide a brief definition in the context of the Flex idiom which could serve as a point of reference for others moving forward.</p>
<p>In general, it is much easier to understand what a <strong>VO</strong> <em>is not</em> by first understanding what a <strong>Domain Model</strong> <em>is</em>; therefore, I will begin by providing a general definition of a Domain Model.</p>
<h3>Domain Models</h3>
<p>A Domain Model is anything of significance which represents a specific business concept within a problem domain. Domain Models are simply classes which represents such concepts by defining all of the state, behavior, constraints and relationships to other Domain Models needed to do so. Essentially, a Domain Model <em>models</em> a domain concept, such as a Product, a User, or anything which could be defined within the problem domain outside the context of code. Domain Models promote reuse and eliminate redundancy by defining specific classes which encapsulate business logic, state and relationships. As the domain concepts change, so to do the implementations of the Domain Models. </p>
<h3>Value Objects (VOs)</h3>
<p>As the name implies, a Value Object, more commonly referred to as a VO, is an object which simply provides values. VOs are entirely immutable; that is, all properties of a VO are read-only and assignments to those properties are specified only during object construction; after which, the properties of the VO can not be modified and, by design, should not require changes.</p>
<p>VOs are typically used to provide an aggregation of conceptually related properties whose values describe the state of the object when instantiated and do not require any real concept of identity or uniqueness. While there are some edge cases (such as validation), more commonly than not, VOs do not implement any specific behavior. Conceptually, think of a VO as being nothing more than an object which holds a value or series of related values which describe something about the object when created. A typical example of a VO could be a UserLogin object which simply holds the values of the specified username and password when created. It is important to make the distinction between a VO and a Domain Model as, a VO is not a Model, but rather, a VO is nothing more than an object which holds values and could be used to describe any context.  </p>
<h3>And that&#8217;s it</h3>
<p>Hopefully the above descriptions of both Domain Models and Value Objects will clear up any confusion surrounding the two concepts; ideally, making it easier to understand when to use each. </p>
<p>The point to keep in mind is that <b>Domain Models</b> simply <em>model</em> the domain, while <b>VOs</b> simply describe a contextual state.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ericfeminella.com/blog/2010/12/05/domain-models-and-value-objects/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Guiding Design with Behavior Verification and Mock Objects</title>
		<link>http://www.ericfeminella.com/blog/2010/11/24/guiding-design-with-behavior-verification-and-mock-objects/</link>
		<comments>http://www.ericfeminella.com/blog/2010/11/24/guiding-design-with-behavior-verification-and-mock-objects/#comments</comments>
		<pubDate>Wed, 24 Nov 2010 13:00:43 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Object Oriented Design]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[Test Driven Development]]></category>

		<guid isPermaLink="false">http://www.ericfeminella.com/blog/?p=1246</guid>
		<description><![CDATA[At some point every developer who has disciplined themselves in the ritualistic like art and science of Test Driven Development soon discovers that the collaborators on which a class under test depend introduce an additional layer of complexity to consider when writing your tests &#8211; and designing your APIs. For example, consider a simple test [...]]]></description>
			<content:encoded><![CDATA[<p>At some point every developer who has disciplined themselves in the ritualistic like art and science of <a href="http://en.wikipedia.org/wiki/Test-driven_development" target="_blank">Test Driven Development</a> soon discovers that the collaborators on which a class under test depend introduce an additional layer of complexity to consider when writing your tests &#8211; and designing your APIs.</p>
<p>For example, consider a simple test against a class <code>Car</code> which has an instance of class <code>Engine</code>. <code>Car</code> implements a <code>start</code> method which, when invoked, calls the <code>Engine</code> object&#8217;s <code>run</code> method. The challenge here lies in testing the dependency <code>Car</code> has on <code>Engine</code>, specifically, how one verifies that an invocation of <code>Car.start</code> results in the <code>Engine</code> object&#8217;s <code>run</code> method being called.</p>
<p>There are two ways of testing the above example of <code>Car</code>, which in unit testing nomenclature is called the <a href="http://xunitpatterns.com/SUT.html" target="_blank">System Under Test</a> (SUT), and it&#8217;s <code>Engine</code> instance which is <code>Car's</code> <a href="http://xunitpatterns.com/DOC.html" target="_blank">Depended-on Component</a> (DOC). The most common approach is to define assertions based on the state of both the SUT and it&#8217;s DOC after being exercised. This style of testing is commonly referred to as <a href="http://xunitpatterns.com/State%20Verification.html" target="_blank">State Verification</a>, and is typically the approach most developers initially use when writing tests.</p>
<p>Using the above Car example, a typical State Verification test would be implemented as follows:<br />
</p>
<div class="dean_ch" style="white-space: wrap;">
<p><span class="kw3">public</span> <span class="kw2">class</span> CarTest<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">private</span> <span class="kw2">var</span> _car:Car = <span class="kw2">null</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#91;</span>Before<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">public</span> <span class="kw2">function</span> setUp<span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_car = <span class="kw2">new</span> Car<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#91;</span>Test<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">public</span> <span class="kw2">function</span> testStart<span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_car.<span class="kw3">start</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;assertTrue<span class="br0">&#40;</span> _car.<span class="me1">isStarted</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;assertTrue<span class="br0">&#40;</span> _car.<span class="me1">engine</span>.<span class="me1">isRunning</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#91;</span>After<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">public</span> <span class="kw2">function</span> tearDown<span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_car = <span class="kw2">null</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p><strong><em>Figure 1</em></strong>. CarTest, State Verification.</p>
<p>From a requirements perspective and therefore a testing and implementation perspective as well, the expectation of calling <code>start</code> on <code>Car</code> is that it will A.) change it&#8217;s running state to true, and B.) invoke <code>run</code> on it&#8217;s <code>Engine</code> instance. As you can see in <em>Figure 1</em>, in order to test the start method on <code>Car</code> the <code>Engine</code> object must also be tested. In the example, using the State Verification style of testing, <code>Car</code> exposes the <code>Engine</code> instance in order to allow the state of <code>Engine</code> to be verified. This has lead to a less than ideal design as it breaks encapsulation and violates <a href="http://www.ericfeminella.com/blog/?s=Principle+of+Least+Knowledge&#038;x=0&#038;y=0" target="_blank">Principle of Least Knowledge</a>. Obviously, a better design of <code>Car.isStarted</code> could be implemented such that it determines if it&#8217;s <code>Engine</code> instance is also in a running state; however, realistically, <code>Engine.run</code> will likely need to do more than just set its running state to true; conceivable, it could need to do much, much more. More importantly, while testing <code>Car</code> one should only be concerned with the state and behavior of <code>Car</code> &#8211; and not that of its dependencies. As such, it soon becomes apparent that what really needs to be tested with regards to <code>Engine</code> in <code>Car.start</code> is that <code>Engine.run</code> is invoked, and nothing more.</p>
<p>With this in mind, the implementation details of <code>Engine.run</code> are decidedly of less concern when testing <code>Car</code>; in fact, a &#8220;real&#8221; concrete implementation of <code>Engine</code> need not even exist in order to test <code>Car</code>; only the contract between <code>Car</code> and <code>Engine</code> should be of concern. Therefore, State Verification alone is not sufficient for testing <code>Car.start</code> as, at best, this approach unnecessarily requires a real <code>Engine</code> implementation or, at worst, as illustrated in <em>Figure 1</em>, can negatively guide design as it would require exposing the DOC in order to verify its state; effectively breaking encapsulation and unnecessarily complicating implementation. To reiterate an important point: State Verification requires an implementation of <code>Engine</code> and, assuming <a href="http://www.extremeprogramming.org/rules/testfirst.html" target="_blank">Test First</a> is being followed (ideally, it is), the concern when testing <code>Car</code> should be focused exclusively on <code>Car</code> and it&#8217;s interactions with its DOC; not on their specific implementations. And this is where the second style of testing &#8211; <strong>Behavior Verification</strong> &#8211; plays an important role in TDD.</p>
<p>The <a href="http://xunitpatterns.com/Behavior%20Verification.html" target="_blank">Behavior Verification</a> style of testing relies on the use of <a href="http://www.mockobjects.com/" target="_blank">Mock Objects</a> in order to test the expectations of an SUT; that is, that the expected methods are called on it&#8217;s DOC with the expected parameters. Behavior Verification is most useful where State Verification alone would otherwise negatively influence design by requiring the implementation of needless state if only for the purpose of providing a more convenient means of testing. For example, many times an object may not need to be stateful or the behavior of an object may not always require a change in it&#8217;s state after exercising the SUT. In such cases, Behavior Verification with Mock Objects will lead to a simpler, more cohesive design as it requires careful design considerations of the SUT and it&#8217;s interactions with its DOC. A rather natural side-effect of this is promoting the use of interfaces over implementations as well as maintaining encapsulation.</p>
<p>For testing with Behavior Verification in Flex, there are numerous <a href="http://en.wikipedia.org/wiki/List_of_mock_object_frameworks" target="_blank">Mock Object frameworks</a> available, all of which are quite good in their own right and more or less provide different implementations of the same fundamental concepts. To name just a few, in no particular order, there are <a href="http://asmock.sourceforge.net/" target="_blank">asMock</a>, <a href="http://bitbucket.org/loomis/mockito-flex/wiki/Home" target="_blank">mockito-flex</a>, <a href="http://mockolate.org/" target="_blank">mockolate</a> and <a href="http://code.google.com/p/mock4as/" target="_blank">mock4as</a>.</p>
<p>While any of the above Mock Testing Frameworks will do, for the sake of simplicity I will demonstrate re-writing the <code>Car</code>test using Behavior Verification based on mock4as &#8211; if for nothing other than the fact that it requires implementing the actual Mock, which helps illustrate how everything comes together. Moreover, the goal of this essay is to help developers understand the design concepts surrounding TDD with Behavior Verification and Mock Objects by focusing on the basic design concepts; not the implementation specifics of any individual Mock Framework.</p>
<p></p>
<div class="dean_ch" style="white-space: wrap;">
<p><span class="kw3">public</span> <span class="kw2">class</span> CarTest<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">private</span> <span class="kw2">var</span> _car:Car = <span class="kw2">null</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">private</span> <span class="kw2">var</span> _mock:MockEngine = <span class="kw2">null</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#91;</span>Before<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">public</span> <span class="kw2">function</span> setUp<span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_mock = <span class="kw2">new</span> MockEngine<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_car &nbsp;= <span class="kw2">new</span> Car<span class="br0">&#40;</span> _mock <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#91;</span>Test<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">public</span> <span class="kw2">function</span> testStartChangesState<span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_car.<span class="kw3">start</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;assertTrue<span class="br0">&#40;</span> _car.<span class="me1">isRunning</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#91;</span>Test<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">public</span> <span class="kw2">function</span> testStartInvokesEngineRun<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_mock.<span class="me1">expects</span><span class="br0">&#40;</span> <span class="st0">&quot;run&quot;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_car.<span class="kw3">start</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;assertTrue<span class="br0">&#40;</span>_mock.<span class="me1">errorMessage</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_mock.<span class="me1">success</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#91;</span>After<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">public</span> <span class="kw2">function</span> tearDown<span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_mock = <span class="kw2">null</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_car &nbsp;= <span class="kw2">null</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p><strong><em>Figure 2</em></strong>. CarTest, Behavior Verification approach.</p>
<p>Let&#8217;s go through what has changed in <code>CarTest</code> now that it leverages Behavior Verification. First, <code>Car's</code> constructor has been refactored to require an <code>Engine</code> object, which now implements an <code>IEngine</code> interface, which is defined as follows.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw3">public</span> <span class="kw3">interface</span> IEngine<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">function</span> run<span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">void</span>;<br />
<span class="br0">&#125;</span></div>
<p><strong><em>Figure 3</em></strong>. IEngine interface.</p>
<p>Note <code>Engine.isRunning</code> is no longer tested, or even defined as, it is simply not needed when testing <code>Car</code>: only the call to <code>Engine.run</code> is to be verified in the context of calling <code>Car.start</code>. Since focus is exclusively on the SUT, only the interactions between <code>Car</code> and <code>Engine</code> are of importance and should be defined. The goal is to focus on the testing of the SUT and not be distracted with design or implementation details of it&#8217;s DOC outside of that which is needed by the SUT.</p>
<p><code>MockEngine</code> provides the actual implementation of <code>IEngine</code>, and, as you may have guessed, is the actual Mock object implementation of <code>IEngine</code>. <code>MockEngine</code> simply serves to provide a means of verifing that when <code>Car.start</code> is exercised it successfully invokes <code>Engine.run</code>; effectively satisfiying the contract between <code>Car</code> and <code>Engine</code>. <code>MockEngine</code> is implemented as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw3">import</span> org.<span class="me1">mock4as</span>.<span class="me1">Mock</span>;</p>
<p><span class="kw3">public</span> <span class="kw2">class</span> MockEngine <span class="kw3">extends</span> Mock <span class="kw3">implements</span> IEngine<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> run<span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; record<span class="br0">&#40;</span> <span class="st0">&quot;run&quot;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p><strong><em>Figure 4</em></strong>. MockEngine implementation.</p>
<p><code>MockEngine</code> extends <code>org.mock4as.Mock</code> from which it inherits all of the functionality needed to &#8220;Mock&#8221; an object, in this case, an <code>IEngine</code> implementation. You&#8217;ll notice that <code>MockEngine.run</code> does not implement any &#8220;real&#8221; functionality, but rather it simply invokes the inherited <code>record</code> method, passing in the method name to record for verification when called. This is the mechanism which allows a <code>MockEngine</code> instance to be verified once <code>run</code> is invoked.</p>
<p><code>CarTest</code> has been refactored to now provide two distinct tests against <code>Car.start</code>. The first, <code>testStartChangesState()</code>, provides the State Verification test of <code>Car</code>; which tests the expected state of <code>Car</code> after being exercised. The second test, <code>testStartInvokesEngineRun()</code>, provides the actual Behavior Verification test which defines the expectations of the SUT and verification of those expectations on the DOC; that is, Behavior Verification tests are implemented such that they first define expectations, then exercise the SUT, and finally, verify that the expectations have been met. In effect, this verifies that the contract between an SUT and its DOC has been satisfied.</p>
<p>Breaking down the <code>testStartInvokesEngineRun()</code> test, it is quite easy to follow the steps used when writing a Behavior Verification test.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="br0">&#91;</span>Test<span class="br0">&#93;</span><br />
<span class="kw3">public</span> <span class="kw2">function</span> testStartInvokesEngineStart<span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// Set expectations on the mock; essentially, what</span><br />
&nbsp; &nbsp; &nbsp;<span class="co1">// method the SUT is expected to invoke on it&#8217;s DOC</span><br />
&nbsp; &nbsp; &nbsp; _mock.<span class="me1">expects</span><span class="br0">&#40;</span> <span class="st0">&quot;run&quot;</span> <span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; <span class="co1">// Exercise the SUT</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;_car.<span class="kw3">start</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span class="co1">// Verify expectations have been met</span><br />
&nbsp; &nbsp; &nbsp; assertTrue<span class="br0">&#40;</span>_mock.<span class="me1">errorMessage</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, mock.<span class="me1">success</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>And that&#8217;s basically it. While much more can be accomplished with the many Mock Testing frameworks available for Flex, and plenty of information is available on the specifics of the subject, this essay quite necessarily aims to focus on the design benefits of testing with Behavior Verification; that is, the design considerations one must make while doing so. </p>
<p>With Behavior Verification and Mock Objects, design can be guided into existence based on necessity rather than pushed into existence based on implementation.</p>
<p>The example can be downloaded <a href="http://code.ericfeminella.com/downloads/MockObjectsExample.fxp" target="_blank">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ericfeminella.com/blog/2010/11/24/guiding-design-with-behavior-verification-and-mock-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Context is key: Test Coverage</title>
		<link>http://www.ericfeminella.com/blog/2010/08/07/context-is-key-test-coverage/</link>
		<comments>http://www.ericfeminella.com/blog/2010/08/07/context-is-key-test-coverage/#comments</comments>
		<pubDate>Sat, 07 Aug 2010 12:39:46 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[code coverage]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test coverage]]></category>
		<category><![CDATA[test driven design]]></category>
		<category><![CDATA[test driven development]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.ericfeminella.com/blog/?p=1026</guid>
		<description><![CDATA[The notion that Test Coverage alone can provide an adequate metric for determining how well a particular piece of code, or worse, an entire codebase, is being tested has always troubled me as, in my experience, this can be a very misleading assumption. Like so many of my previous themes, with Test Coverage context trully [...]]]></description>
			<content:encoded><![CDATA[<p>The notion that <a href="http://en.wikipedia.org/wiki/Code_coverage" target="_blank">Test Coverage</a> alone can provide an adequate metric for determining how well a particular piece of code, or worse, an entire codebase, is being tested has always troubled me as, in my experience, this can be a very misleading assumption.</p>
<p>Like so many of my previous themes, with Test Coverage context trully is key as, relying on a predetermined percentage threshold as a true measure of successful testing fails to take into account the numerous factors involved. For example, the preceived thoroughness of a systems tests can easily be increased &#8211; beit mistakenly or intentionally &#8211; by testing parts of the system which could be considered irrelevant; such as getters, setters and the like.</p>
<p>Personally, I prefer (and advocate) focusing first on testing the most critical behaviors and state of a particular piece of code. The tests should not be limited to just testing the expected cases but also, and of equal importance, the testing of exceptional and <a href="http://www.workroom-productions.com/papers/PVoNT_paper.pdf" target="_blank">negative tests</a>.</p>
<p>I could go on about this in great detail however I recently came across a really good post from <a HREF="http://googletesting.blogspot.com/2010/07/code-coverage-goal-80-and-no-less.html?utm_source=feedburner&#038;utm_medium=feed&#038;utm_campaign=Feed:+blogspot/RLXA+(Google+Testing+Blog)" target="_blank">googletesting</a> (which I found via <a href="http://www.oreillynet.com/pub/au/3450" target="_blank">Mike Labriola</a>) which I think pretty much sums it up.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ericfeminella.com/blog/2010/08/07/context-is-key-test-coverage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Misplaced Code</title>
		<link>http://www.ericfeminella.com/blog/2010/05/31/misplaced-code/</link>
		<comments>http://www.ericfeminella.com/blog/2010/05/31/misplaced-code/#comments</comments>
		<pubDate>Mon, 31 May 2010 14:05:55 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Object Oriented Design]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[Test Driven Development]]></category>

		<guid isPermaLink="false">http://www.ericfeminella.com/blog/?p=802</guid>
		<description><![CDATA[Often I come across what I like to call &#8220;Misplaced Code&#8221;, that is, code which should be refactored to a specific, independent concern rather than mistakenly being defined in an incorrect context. For instance, consider the following example to get a better idea of what I mean: var url:String = Application.application.url; if &#40; url.indexOf&#40; &#34;localhost&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>Often I come across what I like to call &#8220;Misplaced Code&#8221;, that is, code which should be refactored to a specific, independent concern rather than mistakenly being defined in an incorrect context. </p>
<p>For instance, consider the following example to get a better idea of what I mean:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw2">var</span> <span class="kw3">url</span>:<span class="kw3">String</span> = Application.<span class="me1">application</span>.<span class="kw3">url</span>;</p>
<p><span class="kw1">if</span> <span class="br0">&#40;</span> <span class="kw3">url</span>.<span class="kw3">indexOf</span><span class="br0">&#40;</span> <span class="st0">&quot;localhost&quot;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&#8230;<br />
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="kw3">url</span>.<span class="kw3">indexOf</span><span class="br0">&#40;</span> <span class="st0">&quot;dev&quot;</span> <span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&#8230;<br />
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="kw3">url</span>.<span class="kw3">indexOf</span><span class="br0">&#40;</span> <span class="st0">&quot;staging&quot;</span> <span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&#8230;<br />
<span class="br0">&#125;</span><br />
etc&#8230;</div>
<p>Taking the above example into a broader context, it is quite common to see code such as this scattered throughout a codebase; particularly in the context of view concerns. At best this could become hard to maintain and, at worst, it will result in unexpected bugs down the road. In most cases (as in the above example) the actual code itself is not necessarily bad, however it is the context in which it is placed which is what I would like to highlight as it will almost certainly cause <a href="http://www.martinfowler.com/bliki/TechnicalDebt.html" target="_blank">technical debt</a> to some extent.</p>
<p>Considering the above example, should code such as this become redundantly implemented throughout a codebase it is quite easy to see how it can become a maintenance issue as, something as simple as a change to a hostname would require multiple refactorings. A much more appropriate solution would be to encapsulate this logic within a specific class whose purpose is to provide a facility from which this information can be determined. In this manner unnecessary redundancy would be eliminated (as well as risk) and valuable development time would be regained as the code would need only be tested and written once &#8211; in one place. </p>
<p>So again, using the above example, this could be refactored to a specific API and client code would leverage the API as in the following:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">switch</span><span class="br0">&#40;</span> DeploymentContext.<span class="me1">host</span> <span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">case</span> DeploymentContext.<span class="me1">LOCAL_HOST</span> :<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#8230;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> DeploymentContext.<span class="me1">DEV</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#8230;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">break</span>;<br />
&nbsp; &nbsp; <span class="kw1">case</span> DeploymentContext.<span class="me1">STAGING</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#8230;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">break</span>;<br />
<span class="br0">&#125;</span></div>
<p>This may appear quite straightforward, however, I have seen examples (this one in particular) in numerous projects over the years and it is worth pointing out. Always take the context to which code is placed into consideration and you will reap the maintenance benefits in the long run.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ericfeminella.com/blog/2010/05/31/misplaced-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Some useful Tips to keep in mind</title>
		<link>http://www.ericfeminella.com/blog/2010/02/28/keep-this-in-mind/</link>
		<comments>http://www.ericfeminella.com/blog/2010/02/28/keep-this-in-mind/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 05:32:49 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Object Oriented Design]]></category>

		<guid isPermaLink="false">http://www.ericfeminella.com/blog/?p=315</guid>
		<description><![CDATA[Throughout my career I have always been drawn to books which provide a practical way of thinking about software. Books of this nature have an emphasis on the fundamental principles which apply to all software engineering disciplines; and form much of the basis of the Agile methodologies many of us have come to appreciate. From [...]]]></description>
			<content:encoded><![CDATA[<p>Throughout my career I have always been drawn to books which provide a practical way of thinking about software. Books of this nature have an emphasis on the fundamental principles which apply to all software engineering disciplines; and form much of the basis of the Agile methodologies many of us have come to appreciate. </p>
<p>From time to time I find myself going back to the seminal text <a href="http://www.pragprog.com/the-pragmatic-programmer" target="_blank">The Pragmatic Programmer</a> as it provides a great source of what should be kept in mind from day to day. I thought I would share some tips from the book with my readers that I find of particular value, and would be happy to explain them at length; in the context of real world software challenges.</p>
<blockquote><p><strong>Care About Your Craft</strong><br />
Why spend your life developing software unless you care about doing it well?</p>
<p><strong>Provide Options, Don&#8217;t Make Lame Excuses</strong><br />
Instead of excuses, provide options. Don&#8217;t say it can&#8217;t be done; explain what can be done.</p>
<p><strong>Critically Analyze What You Read and Hear</strong><br />
Don&#8217;t be swayed by vendors, media hype, or dogma. Analyze information in terms of you and your project.</p>
<p><strong>Design with Contracts</strong><br />
Use contracts to document and verify that code does no more and no less than it claims to do.</p>
<p><strong>Refactor Early, Refactor Often</strong><br />
Just as you might weed and rearrange a garden, rewrite, rework, and re-architect code when it needs it. Fix the root of the problem.</p>
<p><strong>Costly Tools Don&#8217;t Produce Better Designs</strong><br />
Beware of vendor hype, industry dogma, and the aura of the price tag. Judge tools on their merits.</p>
<p><strong>Start When You&#8217;re Ready</strong><br />
You&#8217;ve been building experience all your life. Don&#8217;t ignore niggling doubts.</p>
<p><strong>Don&#8217;t Be a Slave to Formal Methods</strong><br />
Don&#8217;t blindly adopt any technique without putting it into the context of your development practices and capabilities.</p>
<p><strong>It&#8217;s Both What You Say and the Way You Say It</strong><br />
There&#8217;s no point in having great ideas if you don&#8217;t communicate them effectively.</p>
<p><strong>You Can&#8217;t Write Perfect Software</strong><br />
Software can&#8217;t be perfect. Protect your code and users from the inevitable errors.</p>
<p><strong>Build Documentation In, Don&#8217;t Bolt It On</strong><br />
Documentation created separately from code is less likely to be correct and up to date.</p>
<p><strong>Put Abstractions in Code, Details in Metadata</strong><br />
Program for the general case, and put the specifics outside the compiled code base.</p>
<p><strong>Work with a User to Think Like a User</strong><br />
It&#8217;s the best way to gain insight into how the system will really be used.</p>
<p><strong>Program Close to the Problem Domain</strong><br />
Design and code in your user&#8217;s language.</p>
<p><strong>Use a Project Glossary</strong><br />
Create and maintain a single source of all the specific terms and vocabulary for a project.</p>
<p><strong>Be a Catalyst for Change</strong><br />
You can&#8217;t force change on people. Instead, show them how the future might be and help them participate in creating it.</p>
<p><strong>DRY &#8211; Don&#8217;t Repeat Yourself</strong><br />
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.</p>
<p><strong>Eliminate Effects Between Unrelated Things</strong><br />
Design components that are self-contained, independent, and have a single, well-defined purpose.</p>
<p><strong>Iterate the Schedule with the Code</strong><br />
Use experience you gain as you implement to refine the project time scales.</p>
<p><strong>Use the Power of Command Shells</strong><br />
Use the shell when graphical user interfaces don&#8217;t cut it.</p>
<p><strong>Don&#8217;t Panic When Debugging</strong><br />
Take a deep breath and THINK! about what could be causing the bug.</p>
<p><strong>Don&#8217;t Assume It &#8211; Prove It</strong><br />
Prove your assumptions in the actual environment&mdash;with real data and boundary conditions.</p>
<p><strong>Write Code That Writes Code</strong><br />
Code generators increase your productivity and help avoid duplication.</p>
<p><strong>Test Your Software, or Your Users Will</strong><br />
Test ruthlessly. Don&#8217;t make your users find bugs for you.</p>
<p><strong>Don&#8217;t Gather Requirements&mdash;Dig for Them</strong><br />
Requirements rarely lie on the surface. They&#8217;re buried deep beneath layers of assumptions, misconceptions, and politics.</p>
<p><strong>Abstractions Live Longer than Details</strong><br />
Invest in the abstraction, not the implementation. Abstractions can survive the barrage of changes from different implementations and new technologies.</p>
<p><strong>Don&#8217;t Think Outside the Box&mdash;Find the Box</strong><br />
When faced with an impossible problem, identify the real constraints. Ask yourself: &#8220;Does it have to be done this way? Does it have to be done at all?&#8221;;</p>
<p><strong>Some Things Are Better Done than Described</strong><br />
Don&#8217;t fall into the specification spiral&mdash;at some point you need to start coding.</p>
<p><strong>Don&#8217;t Use Manual Procedures</strong><br />
A shell script or batch file will execute the same instructions, in the same order, time after time.</p>
<p><strong>Test State Coverage, Not Code Coverage</strong><br />
Identify and test significant program states. Just testing lines of code isn&#8217;t enough.</p>
<p><strong>Gently Exceed Your Users&#8217; Expectations</strong><br />
Come to understand your users&#8217; expectations, then deliver just that little bit more.</p>
<p><strong>Don&#8217;t Live with Broken Windows</strong><br />
Fix bad designs, wrong decisions, and poor code when you see them.</p>
<p><strong>Remember the Big Picture</strong><br />
Don&#8217;t get so engrossed in the details that you forget to check what&#8217;s happening around you.</p>
<p><strong>Make It Easy to Reuse</strong><br />
If it&#8217;s easy to reuse, people will. Create an environment that supports reuse.</p>
<p><strong>There Are No Final Decisions</strong><br />
No decision is cast in stone. Instead, consider each as being written in the sand at the beach, and plan for change.</p>
<p><strong>Estimate to Avoid Surprises</strong><br />
Estimate before you start. You&#8217;ll spot potential problems up front.</p>
<p><strong>Use a Single Editor Well</strong><br />
The editor should be an extension of your hand; make sure your editor is configurable, extensible, and programmable.</p>
<p><strong>Fix the Problem, Not the Blame</strong><br />
It doesn&#8217;t really matter whether the bug is your fault or someone else&#8217;s&mdash;it is still your problem, and it still needs to be fixed.</p>
<p><strong>&#8220;select&#8221; Isn&#8217;t Broken</strong><br />
It is rare to find a bug in the OS or the compiler, or even a third-party product or library. The bug is most likely in the application.</p>
<p><strong>Learn a Text Manipulation Language</strong><br />
You spend a large part of each day working with text. Why not have the computer do some of it for you?</p>
<p><strong>Use Exceptions for Exceptional Problems</strong><br />
Exceptions can suffer from all the readability and maintainability problems of classic spaghetti code. Reserve exceptions for exceptional things.</p>
<p><strong>Minimize Coupling Between Modules</strong><br />
Avoid coupling by writing &#8220;shy&#8221; code and applying the Law of Demeter.</p>
<p><strong>Design Using Services</strong><br />
Design in terms of services: independent, concurrent objects behind well-defined, consistent interfaces.</p>
<p><strong>Don&#8217;t Program by Coincidence</strong><br />
Rely only on reliable things. Beware of accidental complexity, and don&#8217;t confuse a happy coincidence with a purposeful plan.</p>
<p><strong>Organize Teams Around Functionality</strong><br />
Don&#8217;t separate designers from coders, testers from data modelers. Build teams the way you build code.</p>
<p><strong>Test Early. Test Often. Test Automatically.</strong><br />
Tests that run with every build are much more effective than test plans that sit on a shelf.</p>
<p><strong>Find Bugs Once</strong><br />
Once a human tester finds a bug, it should be the last time a human tester finds that bug. Automatic tests should check for it from then on.</p>
<p><strong>Sign Your Work</strong><br />
Craftsmen of an earlier age were proud to sign their work. You should be, too.</p></blockquote>
<p>I suggest keeping these tips, or a subset of them, as well as some of your own, somewhere visible; even if some appear rather obvious, as it will serve as a good general reminder of the things you should always keep in mind.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ericfeminella.com/blog/2010/02/28/keep-this-in-mind/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Design Considerations: Naming Conventions</title>
		<link>http://www.ericfeminella.com/blog/2009/07/31/design-considerations-naming-conventions/</link>
		<comments>http://www.ericfeminella.com/blog/2009/07/31/design-considerations-naming-conventions/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 11:12:42 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Object Oriented Design]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://www.ericfeminella.com/blog/?p=219</guid>
		<description><![CDATA[Intuitive naming conventions are perhaps one of the most important factors in providing a scalable software system. They are essential to ensuring an Object Oriented System can easily be understood, and thus modified by all members of a team regardless of their tenure within the organization or individual experience level. When classes, interfaces, methods, properties, [...]]]></description>
			<content:encoded><![CDATA[<p>Intuitive naming conventions are perhaps one of the most important factors in providing a scalable software system. They are essential to ensuring an Object Oriented System can easily be understood, and thus modified by all members of a team regardless of their tenure within the organization or individual experience level.</p>
<p>When classes, interfaces, methods, properties, identifiers, events and the like fail to follow logical, consistent and intuitive naming conventions the resulting software becomes significantly more complex to understand, follow and maintain. As such this makes changes much more challenging than they would have been had better naming been considered originally. Of equal concern is the inevitability that poor naming will lead to redundant code being scattered throughout a project as when the intent of code is not clearly conveyed with as little thought as possible developers tend to re-implement existing functionality when the needed API cannot easily be located or identified. </p>
<p>Code is typically read many, many more times than it is written. With this in mind it is important to understand that the goal of good naming is to be as clear and concise as possible so that a reader of the code can easily determine the codes intent and purpose; just by reading it.</p>
<p>Teams should collectively define a set of standard naming conventions which align well with the typical conventions found in their language of choice. In doing so this will help to avoid arbitrary naming conventions which often result in code that is significantly harder to determine intent, and thus maintain. Of equal importance is the need for various teams from within the same engineering department to standardize on domain specific terms which align with the non-technical terms used by business stakeholders. Together this will help to develop a shared lexicon between business owners and engineers, and allow for simplified analysis of requirements etc. </p>
<p>Ideally, code should follow the PIE Principle (Program, Intently and expressively) &#8211; that is, code should clearly convey purpose and intent. In doing so the ability to maintain a software application over time becomes significantly easier and limits the possibility of introducing potential risk to project deliverables.</p>
<p>In short, conventions are very important regardless of a teams size; beit a large collaborative team environment, or a single developer who only deals with his own code. Consistency and conventions are a key aspect to ensuring code quality.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ericfeminella.com/blog/2009/07/31/design-considerations-naming-conventions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cairngorm Abstractions: Business Delegates</title>
		<link>http://www.ericfeminella.com/blog/2009/05/07/cairngorm-abstractions-part-2-business-delegates/</link>
		<comments>http://www.ericfeminella.com/blog/2009/05/07/cairngorm-abstractions-part-2-business-delegates/#comments</comments>
		<pubDate>Thu, 07 May 2009 22:07:02 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Object Oriented Design]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Test Driven Development]]></category>

		<guid isPermaLink="false">http://www.ericfeminella.com/blog/2009/05/15/cairngorm-abstractions-part-2-business-delegates/</guid>
		<description><![CDATA[In Part 1 of Cairngorm Abstractions I discussed the common patterns which can be utilized in a design to simplify the implementation of concrete Cairngorm Commands and Responders. Applying such patterns can be leveraged to help facilitate code reuse and provide a maintainable, scalable architecture, as, in doing so the design will ultimately ensure reuse [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.ericfeminella.com/blog/2009/04/21/cairngorm-abstractions-commands-and-responders/" target="_blank">Part 1</a> of Cairngorm Abstractions I discussed the common patterns which can be utilized in a design to simplify the implementation of concrete <a href="http://opensource.adobe.com/wiki/display/cairngorm/" target="_blank">Cairngorm </a>Commands and Responders. Applying such patterns can be leveraged to help facilitate code reuse and provide a maintainable, scalable architecture, as, in doing so the design will ultimately ensure reuse as well as remove redundancy.</p>
<p>In this post I will describe the same benefits which can be gained by defining common abstractions of Business Delegates.</p>
<p><strong>Business Delegate Abstractions</strong><br />
A Business Delegate should provide an interface against the service to which it references. This can be viewed as a one-to-one relationship whereas the operations and signatures defined by a Service, beit an <a href="http://livedocs.adobe.com/flex/gumbo/langref/mx/rpc/http/HTTPService.html" target="_blank">HTTPService</a>, <a href="http://livedocs.adobe.com/flex/gumbo/langref/mx/rpc/soap/WebService.html" target="_blank">WebService</a>, <a href="http://livedocs.adobe.com/flex/gumbo/langref/mx/rpc/remoting/RemoteObject.html" target="_blank">RemoteObject</a>, DataService etc. would dictate the Business Delegate&#8217;s API.</p>
<p>However, a rather common mistake I often find is that many times Business Delegates are defined in the context of the use case which invokes them, rather than the service from which they provide an interface against. </p>
<p>Correcting this is quite simple: refactor the current implementation to follow the one-to-one relationship model between a Service and Business Delegate. </p>
<p>So for instance, if your applications service layer specifies a &#8220;UserService&#8221;, your design should essentially have only one Business Delegate API for that Service. All of the operations provided by the &#8220;UserService&#8221; would be defined by an &#8220;IUserServiceDelegate&#8221; interface which would enforce the contract between the &#8220;UserService&#8221; and concrete Delegate implementations, regardless of their underlying service mechanism. </p>
<p>In this manner clients (delegate instances) can be defined as the abstraction (IUserServiceDelegate) and obtain references to concrete Business Delegate instances via a Delegate Factory, and as such remain completely transparent of their underlying service implementation. </p>
<p>This could be implemented as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<p><span class="kw2">var</span> delegate:IUserServiceDelegate;<br />
delegate = DelegateFactory.<span class="me1">createUserServiceDelegate</span><span class="br0">&#40;</span> responder <span class="br0">&#41;</span>;<br />
<span class="co1">// invoke delegate &#8230;</span></div>
<p><strong>Abstract Delegates</strong><br />
Perhaps the most common design improvement which can be made to improve the implementation and maintainability of Business Delegates is to define proper abstractions which provide an implementation which is common amongst all Business Delegates. Additionally, in doing so you will remove a significant amount of redundancy from your design.</p>
<p>For example, if you compare any two Business Delegates and find they have practically the exact same implementation, that is an obvious sign that a common abstraction should be defined.</p>
<p>Consider the following Business Delegate implementation:</p>
<div class="dean_ch" style="white-space: wrap;">
<p><span class="kw3">public</span> <span class="kw2">class</span> SomeDelegate<br />
<span class="br0">&#123;</span>&nbsp; &nbsp;<br />
&nbsp; <span class="kw3">private</span> <span class="kw2">var</span> _service:RemoteObject;<br />
&nbsp; <span class="kw3">private</span> <span class="kw2">var</span> _responder:IResponder;<br />
&nbsp; &nbsp; <br />
&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> SomeDelegate<span class="br0">&#40;</span>responder:IResponder<span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; _service = ServiceLocator.<span class="me1">getInstance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="me1">getRemoteObject</span><span class="br0">&#40;</span> Services.<span class="me1">LOGIN_SERIVCE</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; _responder = responder;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> methodA<span class="br0">&#40;</span>arg1:<span class="kw3">String</span>, arg2:<span class="kw3">int</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw2">var</span> <span class="kw3">call</span>:AsyncToken = _service.<span class="me1">methodA</span><span class="br0">&#40;</span> arg1, arg2<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span class="kw3">call</span>.<span class="me1">addResponder</span><span class="br0">&#40;</span> _responder <span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> methodB<span class="br0">&#40;</span>arg:<span class="kw3">Boolean</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw2">var</span> <span class="kw3">call</span>:AsyncToken = _service.<span class="me1">methodB</span><span class="br0">&#40;</span> arg <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span class="kw3">call</span>.<span class="me1">addResponder</span><span class="br0">&#40;</span> _responder <span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> methodC<span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw2">var</span> <span class="kw3">call</span>:AsyncToken = _service.<span class="me1">methodC</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span class="kw3">call</span>.<span class="me1">addResponder</span><span class="br0">&#40;</span> _responder <span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; &#8230;<br />
<span class="br0">&#125;</span></div>
<p>The above example may look familiar, and when given just a bit of thought as to it&#8217;s design it becomes apparent that there is quite a bit of redundancy as every method essentially contains the same implementation code. That is, an AsyncToken is created, referencing the operation to invoke against the service, and a reference to the responder is added to the token.</p>
<p>The overall design would benefit much more by refactoring the commonality implemented across all Business Delegate methods to an abstraction, which in it&#8217;s simplest form could be defined as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<p><span class="kw3">public</span> <span class="kw2">class</span> AbstractRemoteObjectDelegate<br />
<span class="br0">&#123;</span>&nbsp; &nbsp;<br />
&nbsp; protected <span class="kw2">var</span> service:RemoteObject;<br />
&nbsp; protected <span class="kw2">var</span> responder:IResponder;</p>
<p>&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> AbstractRemoteObjectDelegate<span class="br0">&#40;</span>serviceId:<span class="kw3">String</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; responder:IResponder<span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">service</span> = ServiceLocator.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="me1">getInstance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getRemoteObject</span><span class="br0">&#40;</span> serviceId <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">responder</span> = responder;<br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; protected <span class="kw2">function</span> invoke<span class="br0">&#40;</span>methodName:<span class="kw3">String</span>, &#8230;<span class="me1">args</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw2">var</span> operation:Operation = service<span class="br0">&#91;</span> methodName <span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; operation.<span class="kw3">arguments</span> = args;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span class="kw2">var</span> <span class="kw3">call</span>:AsyncToken = operation.<span class="kw3">send</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span class="kw3">call</span>.<span class="me1">addResponder</span><span class="br0">&#40;</span> responder <span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>By defining a basic abstraction, the original implementation could then be refactored to the following:</p>
<div class="dean_ch" style="white-space: wrap;">
<p><span class="kw3">public</span> <span class="kw2">class</span> SomeDelegate <span class="kw3">extends</span> AbstractRemoteObjectDelegate<br />
<span class="br0">&#123;</span>&nbsp; &nbsp;<br />
&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> SomeDelegate<span class="br0">&#40;</span>responder:IResponder<span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span class="kw3">super</span><span class="br0">&#40;</span> Services.<span class="me1">LOGIN_SERIVCE</span>, responder <span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> methodA<span class="br0">&#40;</span>arg1:<span class="kw3">String</span>, arg2:<span class="kw3">int</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; invoke<span class="br0">&#40;</span> <span class="st0">&quot;methodA&quot;</span>, arg1, arg2 <span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> methodB<span class="br0">&#40;</span>arg:<span class="kw3">Boolean</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; invoke<span class="br0">&#40;</span> <span class="st0">&quot;methodB&quot;</span>, arg <span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> methodC<span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; invoke<span class="br0">&#40;</span> <span class="st0">&quot;methodC&quot;</span> <span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>The same basic abstractions could easily be defined for HTTPService, WebService and DataService specific Business Delegates (in fact I have a library of Cairngorm extensions which provides them; planning on releasing these soon). Pulling up common implementation code to higher level abstract types also simplifies writing tests against concrete Business Delegates as the abstraction itself would need only to be tested once.</p>
<p>There are many more Business Delegate abstractions I would recommend in addition to what I have outlined here, in particular configuring Delegate Factories via an IoC Container such as <a href="http://www.pranaframework.org/" target="_blank">SAS</a>, however I would first suggest taking a good look at your current design before adding additional layers of abstraction, and the most appropriate place to start would be to define abstractions which encapsulate commonality, promote reuse and remove redundancy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ericfeminella.com/blog/2009/05/07/cairngorm-abstractions-part-2-business-delegates/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cairngorm Abstractions: Commands and Responders</title>
		<link>http://www.ericfeminella.com/blog/2009/04/21/cairngorm-abstractions-commands-and-responders/</link>
		<comments>http://www.ericfeminella.com/blog/2009/04/21/cairngorm-abstractions-commands-and-responders/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 21:43:09 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Object Oriented Design]]></category>
		<category><![CDATA[Refactoring]]></category>

		<guid isPermaLink="false">http://www.ericfeminella.com/blog/2009/04/21/cairngorm-abstractions-commands-and-responders/</guid>
		<description><![CDATA[It is quite common to find a significant amount of code redundancy in Flex applications built on Cairngorm. This is by no means a fault of the framework itself, actually quite the contrary as Cairngorm is designed with simplicity in mind; opting to appropriately take a less-is-more approach in favor of providing a more prescriptive [...]]]></description>
			<content:encoded><![CDATA[<p>It is quite common to find a significant amount of code redundancy in Flex applications built on Cairngorm. This is by no means a fault of the framework itself, actually quite the contrary as Cairngorm is designed with simplicity in mind; opting to appropriately take a less-is-more approach in favor of providing a more prescriptive framework which only defines the implementation classes necessary to facilitate the &#8220;plumbing&#8221; behind the framework. Everything else is really just an interface. </p>
<p>With this amount of flexibility comes additional responsibility in that developers must decide what the most appropriate design is based on their applications specific context. Moreover, as with any design there is never a truly one size fits all approach which can be applied to any problem domain; there are really only common patterns and conventions which can be applied across domains and applications. This IMHO is what had allowed the framework to be a success and it is important to understand that this simplicity also requires developers to give their designs the same attention one would to any Object Oriented design.</p>
<p>However over the years I have found a significant amount of redundancy found in Flex applications built on Cairngorm. This appears to be (more often than not) the result of developers implementing Cairngorm examples verbatim in real world applications, and in doing so failing to define proper abstractions for commonly associated concerns and related responsibilities. The most common example of this is the typical implementation of Commands, Responders BusinessDelegates and <a href="http://martinfowler.com/eaaDev/PresentationModel.html">PresentationModel</a> implementations.</p>
<p>For some of you this may all seem quite obvious, and for others hopefully this series will provide some insight as to how one can reduce code redundancy across your Cairngorm applications by implementing abstractions for common implementations.</p>
<p>This topic will be a multi-part series in which I will provide some suggestions surrounding the common patterns of abstractions which can be implemented in an application built on <a href="http://opensource.adobe.com/wiki/display/cairngorm/Cairngorm" target="_blank">Cairngorm</a>, with this first installment based on common abstractions of Cairngorm Commands and Responders. Other areas in future posts will cover Business Delegate and Presentation Model abstractions. So let&#8217;s get started&#8230;</p>
<p><strong>Command Abstractions</strong><br />
First let&#8217;s begin by looking at what is arguably the simplest abstraction one could define in a Cairngorm application to simplify code and eliminate areas of redundancy &#8211; <a href="http://opensource.adobe.com/svn/opensource/cairngorm/tags/2.2.1/frameworks/cairngorm/com/adobe/cairngorm/commands/ICommand.as">Command</a> abstractions. This example assumes the concern of mx.rpc.IResponder implementations is abstracted to a separate object. For more on this subject see my post regarding <a href="http://www.ericfeminella.com/blog/2007/11/23/iresponder-and-cairngorm/" target="_blank">IResponder and Cairngorm</a>.</p>
<p>A traditional Cairngorm Command is typically implemented as something to the extent of the following:</p>
<div class="dean_ch" style="white-space: wrap;">
<p><span class="kw3">import</span> com.<span class="me1">adobe</span>.<span class="me1">cairngorm</span>.<span class="me1">commands</span>.<span class="me1">ICommand</span>;<br />
<span class="kw3">import</span> com.<span class="me1">adobe</span>.<span class="me1">cairngorm</span>.<span class="kw3">control</span>.<span class="me1">CairngormEvent</span>;</p>
<p><span class="kw3">public</span> <span class="kw2">class</span> Command <span class="kw3">implements</span> ICommand<br />
<span class="br0">&#123;</span><br />
&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> execute<span class="br0">&#40;</span>event:CairngormEvent<span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">var</span> evt:SomeEvent = event as SomeEvent;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">// ModelLocator look-up and common references</span><br />
&nbsp; &nbsp; ModelLocator.<span class="me1">getInstance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>&#8230;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>The problem with the above Command implementation is that it results in numerous look-ups on the ModelLocator Singleton instance in every execute implementation which needs to reference the ModelLocator.</p>
<p>A simpler design would be to define an abstraction for all commands which contains this reference. as in the following:</p>
<div class="dean_ch" style="white-space: wrap;">
<p><span class="kw3">import</span> com.<span class="me1">adobe</span>.<span class="me1">cairngorm</span>.<span class="me1">commands</span>.<span class="me1">ICommand</span>;<br />
<span class="kw3">import</span> com.<span class="me1">adobe</span>.<span class="me1">cairngorm</span>.<span class="kw3">control</span>.<span class="me1">CairngormEvent</span>;<br />
&nbsp; &nbsp; <br />
<span class="coMULTI">/**<br />
&nbsp;*<br />
&nbsp;* Defines an abstraction of common references from<br />
&nbsp;* which concrete ICommand implementations can<br />
&nbsp;* inherit<br />
&nbsp;*<br />
&nbsp;*/</span><br />
internal <span class="kw2">class</span> AbstractCommand <span class="kw3">implements</span> ICommand<br />
<span class="br0">&#123;</span><br />
&nbsp; <span class="co1">// define common reference to ModelLocator</span><br />
&nbsp; <span class="co1">// implementation</span><br />
&nbsp; protected <span class="kw3">static</span> <span class="kw2">var</span> modelLocator:ModelLocator <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= ModelLocator.<span class="me1">getInstance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; <span class="co1">// Force concrete command implementations to </span><br />
&nbsp; <span class="co1">// override execute</span><br />
&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> execute<span class="br0">&#40;</span>event:CairngormEvent<span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw3">throw</span> <span class="kw2">new</span> <span class="kw3">Error</span><span class="br0">&#40;</span> <span class="st0">&quot;Abstract operation&#8230;&quot;</span> <span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>As in any OO system there are many benefits to defining abstractions and a good design certainly reflects this. For example, just by defining a very basic abstraction for all Commands we have now eliminated the number of look-ups on the ModelLocator for every Command in the application as well as redundant imports. By defining an abstraction for common references your code will become easier to read and maintain as the number of lines of code will certainly become reduced. </p>
<p>Commands are by far the easiest to create an abstraction for as most commands will typically reference the ModelLocator, and if so they could do so simply by extending an AbstractCommand, if not they would implement ICommand as they traditionally would. </p>
<p>So the first example could now be refactored to the following:</p>
<div class="dean_ch" style="white-space: wrap;">
<p><span class="kw3">import</span> com.<span class="me1">adobe</span>.<span class="me1">cairngorm</span>.<span class="kw3">control</span>.<span class="me1">CairngormEvent</span>;</p>
<p><span class="kw3">public</span> final <span class="kw2">class</span> Command <span class="kw3">extends</span> AbstractCommand<br />
<span class="br0">&#123;</span><br />
&nbsp; override <span class="kw3">public</span> <span class="kw2">function</span> execute<span class="br0">&#40;</span>event:CairngormEvent<span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">var</span> evt:SomeEvent = event as SomeEvent;<br />
&nbsp; &nbsp; <span class="co1">// modelLocator&#8230;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>You could take these abstractions a step further and define additional abstractions for related behavior and contexts, all of which would also extend the AbstractCommand if a reference to the applications ModelLocator is needed. </p>
<p><strong>Responder Abstractions</strong><br />
Now let&#8217;s take a look at an abstraction which is much more interesting &#8211; Responder abstractions. In this example we will focus on the most common Responder implementation; mx.rpc.IResponder, however the same could easily apply for an LCDS Responder implementation of a DataService. </p>
<p>A separate RPC responder could be defined as an abstraction for HTTPServices, WebServices and RemoteObjects as each request against any of these services results in a response of either result or fault, hence the IResponder interface&#8217;s contract.</p>
<p>For example, consider a typical Responder implementation which could be defined as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<p><span class="kw3">import</span> mx.<span class="me1">rpc</span>.<span class="me1">IResponder</span>;<br />
<span class="kw3">import</span> mx.<span class="me1">rpc</span>.<span class="me1">events</span>.<span class="me1">FaultEvent</span>;<br />
<span class="kw3">import</span> mx.<span class="me1">rpc</span>.<span class="me1">events</span>.<span class="me1">ResultEvent</span>;<br />
&nbsp; &nbsp; <br />
<span class="kw3">public</span> <span class="kw2">class</span> SomeResponder <span class="kw3">implements</span> IResponder<br />
<span class="br0">&#123;</span>&nbsp; &nbsp;&nbsp; &nbsp; <br />
&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> result<span class="br0">&#40;</span><span class="kw3">data</span>:<span class="kw3">Object</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// redundant cast operation</span><br />
&nbsp; &nbsp; <span class="kw2">var</span> result:ResultEvent = <span class="kw3">data</span> as ResultEvent;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">// Redundant ModelLocator lookup and references&#8230;</span><br />
&nbsp; &nbsp; <span class="co1">// ModelLocator.getInstance()&#8230;</span><br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> fault<span class="br0">&#40;</span>info:<span class="kw3">Object</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// Redundant cast operation</span><br />
&nbsp; &nbsp; <span class="co1">// Doesn&#8217;t provide a centralized place for</span><br />
&nbsp; &nbsp; <span class="co1">// global service exception handling</span><br />
&nbsp; &nbsp; <span class="kw2">var</span> fault:FaultEvent = info as FaultEvent;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">// Redundant ModelLocator lookup and references&#8230;</span><br />
&nbsp; &nbsp; <span class="co1">// ModelLocator.getInstance()&#8230;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>By defining a Responder abstraction each concrete Responder implementation would result in significantly less code as the redundant cast operations could be abstracted, and, as with Command Abstractions, a convenience reference to the application specific ModelLocator could also be defined. Moreover, a default service fault implementation could be defined from which each service fault could be handled uniformly across the application.</p>
<p>Thus we could define an abstracttion for RPC Responders as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<p><span class="kw3">import</span> mx.<span class="me1">rpc</span>.<span class="me1">IResponder</span>;<br />
<span class="kw3">import</span> mx.<span class="me1">rpc</span>.<span class="me1">events</span>.<span class="me1">FaultEvent</span>;<br />
<span class="kw3">import</span> mx.<span class="me1">rpc</span>.<span class="me1">events</span>.<span class="me1">ResultEvent</span>;<br />
&nbsp; &nbsp; <br />
<span class="coMULTI">/**<br />
&nbsp;*<br />
&nbsp;* Defines an abstraction of common references and<br />
&nbsp;* functionality from which concrete IResponder<br />
&nbsp;* implementations can inherit<br />
&nbsp;*<br />
&nbsp;*/</span><br />
internal <span class="kw2">class</span> AbstractRPCResponder <span class="kw3">implements</span> IResponder<br />
<span class="br0">&#123;</span>&nbsp; &nbsp;<br />
&nbsp; protected <span class="kw3">static</span> <span class="kw2">var</span> modelLocator:ModelLocator <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= ModelLocator.<span class="me1">getInstance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; <span class="co1">// Provides a default implementation of </span><br />
&nbsp; <span class="co1">// mx.rpc.IResponder.result(); which </span><br />
&nbsp; <span class="co1">// handles casting to a ResultEvent</span><br />
&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> result<span class="br0">&#40;</span><span class="kw3">data</span>:<span class="kw3">Object</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">var</span> result:ResultEvent = ResultEvent<span class="br0">&#40;</span> <span class="kw3">data</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; resultResponse<span class="br0">&#40;</span> result <span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; <span class="co1">// provide default implementation of </span><br />
&nbsp; <span class="co1">// mx.rpc.IResponder.fault(); which </span><br />
&nbsp; <span class="co1">// handles casting to a FaultEvent</span><br />
&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> fault<span class="br0">&#40;</span>info:<span class="kw3">Object</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">var</span> fault:FaultEvent = FaultEvent<span class="br0">&#40;</span> info <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; faultResponse<span class="br0">&#40;</span> fault <span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; <span class="co1">// Force concrete implementation to override</span><br />
&nbsp; <span class="co1">// resultResponse</span><br />
&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> resultResponse<span class="br0">&#40;</span>result:ResultEvent<span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw3">throw</span> <span class="kw2">new</span> <span class="kw3">Error</span><span class="br0">&#40;</span> <span class="st0">&quot;Abstract operation&quot;</span> <span class="br0">&#41;</span>;<br />
&nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; <span class="co1">// Provides default service exception handling </span><br />
&nbsp; <span class="co1">// universally across all Responder implementations.</span><br />
&nbsp; <span class="co1">// Concrete implementations can also override this</span><br />
&nbsp; <span class="co1">// method if specific fault handling needs to be</span><br />
&nbsp; <span class="co1">// implemented</span><br />
&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> faultResponse<span class="br0">&#40;</span>fault:FaultEvent<span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// implement default service exception handling</span><br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>We could now refactor the original Responder implementation to the following simplified implementation:</p>
<div class="dean_ch" style="white-space: wrap;">
<p><span class="kw3">import</span> mx.<span class="me1">rpc</span>.<span class="me1">events</span>.<span class="me1">ResultEvent</span>;<br />
&nbsp; &nbsp; <br />
<span class="kw3">public</span> final <span class="kw2">class</span> SomeResponder <span class="kw3">extends</span> AbstractRPCResponder <br />
<span class="br0">&#123;</span>&nbsp; &nbsp;&nbsp; &nbsp; <br />
&nbsp; override <span class="kw3">public</span> <span class="kw2">function</span> resultResponse<span class="br0">&#40;</span>result:ResultEvent<span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// modelLocator&#8230;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>As you can see just be pulling up common references and functionality to just two abstractions we can significantly remove redundancy from all Commands and Responders. As such this allows designs to improve dramatically as it allows for the isolation of tests and limits the amount of concrete implementation code developers need to sift through when working with your code. </p>
<p>It is important to understand that a design which is built in part on Cairngorm must still adhere to the same underlying Object Oriented Design principles as any other API would, and in doing so you will end up with a much simpler design which can easily scale over time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ericfeminella.com/blog/2009/04/21/cairngorm-abstractions-commands-and-responders/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Refactoring Ant Builds with Macrodefs</title>
		<link>http://www.ericfeminella.com/blog/2009/02/07/refactoring-ant-builds-with-macrodefs/</link>
		<comments>http://www.ericfeminella.com/blog/2009/02/07/refactoring-ant-builds-with-macrodefs/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 01:36:31 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Code Review]]></category>
		<category><![CDATA[Continuous Integration]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Test Driven Development]]></category>

		<guid isPermaLink="false">http://www.ericfeminella.com/blog/2009/02/07/refactoring-ant-builds-with-macrodefs/</guid>
		<description><![CDATA[Within the past few years the proliferation of Agile Best Practices has pushed the importance of refactoring front and center in the world of Object Oriented Software Design, yet for some odd reason build scripts seem to have been overlooked in this regard by many. Perhaps this is due to the risk and complexity involved [...]]]></description>
			<content:encoded><![CDATA[<p>Within the past few years the proliferation of Agile Best Practices has pushed the importance of refactoring front and center in the world of Object Oriented Software Design, yet for some odd reason build scripts seem to have been overlooked in this regard by many. Perhaps this is due to the risk and complexity involved in such an effort as well as the lack of a tools by which refactoring build scripts can safely be accomplished. </p>
<p>For instance, whereas refactoring in typical OO languages relies heavily on Unit Tests for ensuring refactorings do not break existing code along the way, build scripts do not have such safety nets as Unit Tests. <a href="http://ant.apache.org/" target="_blank">Ant</a> is statically typed however it doesn&#8217;t provide compile time type checking, additionally build scripts are defined declaratively via XML mark-up however they can not be validated as there are not fixed DTD attributes to validate them against. Perhaps most importantly is that there are not many resources to turn to for guidance when it comes to refactoring Build Scripts. For example, most of what I have learned about the subject comes from <a href="http://www.juliansimpson.org/2007/07/pipeline-of-doom.html" target="_blank">Julian Simpson&#8217;s</a> work in the <a href="http://www.pragprog.com/titles/twa/thoughtworks-anthology" target="_blank">ThoughtWorks Anthology</a>, which I highly suggest reading for a much more exhaustive, yet comprehensive and succinct essay on the subject. In any case, based on the above factors I am quite certain that all of these points plays a role in Ant Scripts somehow being overlooked with regard to refactoring.</p>
<p><strong>So where do you begin?</strong><br />
That&#8217;s a really good question, one which I was forced to ask myself awhile back while being tasked with the daunting challenge of streamlining a very complex Build / CI process. At the time, I was responsible for modifying a Build for a large enterprise class Flex application which required build time transformations of localized content with varying modules being built for n-locales depending on context specific business rules, all of which needed to be built and deployed to multiple environments via a pre-existing CI Process. Further complicating things was that the builds were wrapped by nested DOS batch files. In addition, the existing builds had dependencies on far more complex underlying build Scripts. To make matters worse, up until that point in time no one, including myself, truly knew the build structure and all of it&#8217;s dependencies, it was very much a black box. So considering the fact that I needed to modify the build and would be responsible for maintaining the builds moving forward, as well as streamlining the existing build scripts so as to allow them to scale in order to support additional applications to seamlessly become part of the build, to say the least, I was eager to learn the Build Scripts inside out if I was to refactor and maintain them. </p>
<p>The moral to the story I just bored you with above is that if you have ever had to maintain a build before then this story probably sounds pretty familiar: you have a Build Script which is a black box that no one wants to deal with; it works and that&#8217;s all that matters &#8211; until it needs to change of course. So again, where does one begin when refactoring a Build Script? Well lets think in terms of typical OO refactoring.</p>
<p><strong>Remove duplication</strong><br />
Perhaps one of the most obvious and easiest places to begin consideration for refactoring candidates in an Object Oriented Design is to remove duplication; that is to isolate and thin out common functionality so as to remove redundancy and duplication. Most Ant Scripts are littered with such duplication, and as such should be viewed in the same manner as one would when refactoring Object Oriented applications. In fact, the goal of refactoring is very much the same regardless of the paradigm &#8211; beit a declaratively language such as Ant or an Object Oriented language such as ActionScript &#8211; provide more efficient, maintainable and easier to work with code.</p>
<p>I tend to think of Build Script design &#8211; yes, it is design &#8211; much the same as any other OO design. So just as one would strive to eliminate code duplication in an Object Oriented Design, the same should apply to the design of a Build Script. For example, consider the following build target which packages a series of distributions:</p>
<div class="dean_ch" style="white-space: wrap;">
&nbsp; &nbsp; <br />
<span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;package&quot;</span> <span class="re0">depends</span>=<span class="st0">&quot;package.src, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; package.tests, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; package.bin, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; package.docs, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; package.dist&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; <br />
<span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;package.src&quot;</span> <span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;zip</span> <span class="re0">basedir</span>=<span class="st0">&quot;${src.dir}&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">destfile</span>=<span class="st0">&quot;${dist.dir}/${dist.src.name}.zip&quot;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re2">/&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;/target<span class="re2">&gt;</span></span></span></p>
<p><span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;package.tests&quot;</span> <span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;zip</span> <span class="re0">basedir</span>=<span class="st0">&quot;${src.tests.dir}&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">destfile</span>=<span class="st0">&quot;${dist.dir}/${dist.src.name}.zip&quot;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re2">/&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;/target<span class="re2">&gt;</span></span></span></p>
<p><span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;package.bin&quot;</span> <span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;zip</span> <span class="re0">basedir</span>=<span class="st0">&quot;${bin.dir}&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">destfile</span>=<span class="st0">&quot;${dist.dir}/${dist.bin.name}.zip&quot;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re2">/&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;/target<span class="re2">&gt;</span></span></span></p>
<p><span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;package.docs&quot;</span> <span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;zip</span> <span class="re0">basedir</span>=<span class="st0">&quot;${docs.dir}&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">destfile</span>=<span class="st0">&quot;${dist.dir}/${docs.name}.zip&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re2">/&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;/target<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; <br />
<span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;package.dist&quot;</span> <span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;zip</span> <span class="re0">basedir</span>=<span class="st0">&quot;${dist.dir}&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">destfile</span>=<span class="st0">&quot;${dist.dir}/${package.name}.zip&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re2">/&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;/target<span class="re2">&gt;</span></span></span><br />
&nbsp;</div>
<p>This kind of Build Script is common, however if you were to think of this in terms of OO Design, whereas each target is analogous to a method, you would quickly realize the code is very redundant. Moreover, the functionality provided by these targets: the packaging of distributions, is a very common task, so just as in an OO design this functionality should be extracted into a reusable library. In Ant 1.6+ we can achieve the same kind of code reuse by extracting these common, redundant targets using Macrodefs.</p>
<p><strong>Use Macrodefs</strong><br />
In short, a <a href="http://ant.apache.org/manual/CoreTasks/macrodef.html">Macrodef</a>, which is short for &#8220;macro definition&#8221;, is basically an extracted piece of reusable functionality in an Ant that can be used across Build Scripts for performing common, or specific tasks. Macrodefs can be thought of as a reusable API for Ant. You include macrodefs in your build scripts by importing the macrodef source file. This is analogous to how one would import a class.</p>
<p>So consider the redundant targets outlined above. Using macrodefs we can extract these common tasks, refactoring them into a single macrodef, import the file which contains the macrodef into our build script and then call the macrodef by wrapping it in a task. </p>
<p>To extract the target to a Macrodef we would first begin by creating a new XML document named after the functionality of the target, in this case we could call it &#8220;dist.xml&#8221;. This document would contain a project root node just as any other Ant Script would. We would then define a macrodef node and specify an identifier via the name attribute; this is how we can reference the macrodef once imported to our build script.</p>
<div class="dean_ch" style="white-space: wrap;">
<p><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span><span class="re2">?&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;project</span> <span class="re0">name</span>=<span class="st0">&quot;package&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;macrodef</span> <span class="re0">name</span>=<span class="st0">&quot;package.dist&quot;</span> <span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/macrodef<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;/project<span class="re2">&gt;</span></span></span></div>
<p>Once we have defined the macrodef we can add dynamic properties to its definition. This could be thought of as begin analogous to arguments of a method signiture. By specifying these arguments we can then assign their values whenever we invoke the macrodef. Default values can also be added if needed.</p>
<div class="dean_ch" style="white-space: wrap;">
<p><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span><span class="re2">?&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;project</span> <span class="re0">name</span>=<span class="st0">&quot;package&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;macrodef</span> <span class="re0">name</span>=<span class="st0">&quot;package.dist&quot;</span> <span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;attribute</span> <span class="re0">name</span>=<span class="st0">&quot;base.dir&quot;</span> &nbsp;<span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;attribute</span> <span class="re0">name</span>=<span class="st0">&quot;dist.dir&quot;</span> &nbsp;<span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;attribute</span> <span class="re0">name</span>=<span class="st0">&quot;dist.file&quot;</span> <span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/macrodef<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;/project<span class="re2">&gt;</span></span></span></div>
<p>Finally, we specify the behavior of the macrodef via the <code>sequential</code> node, This is where the functional markup is defined. Note that we reference the properties internally using the <code>@{property}</code> notation, just as you would normally however the token is prefixed with an <code>@</code> sign rather than a <code>$</code> sign. </p>
<div class="dean_ch" style="white-space: wrap;">
<p><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span><span class="re2">?&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;project</span> <span class="re0">name</span>=<span class="st0">&quot;package&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;macrodef</span> <span class="re0">name</span>=<span class="st0">&quot;package.dist&quot;</span> <span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;attribute</span> <span class="re0">name</span>=<span class="st0">&quot;base.dir&quot;</span> &nbsp;<span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;attribute</span> <span class="re0">name</span>=<span class="st0">&quot;dist.dir&quot;</span> &nbsp;<span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;attribute</span> <span class="re0">name</span>=<span class="st0">&quot;dist.file&quot;</span> <span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;sequential<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;zip</span> <span class="re0">basedir</span>=<span class="st0">&quot;@{base.dir}&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">destfile</span>=<span class="st0">&quot;@{dist.dir}/@{dist.file}.zip&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/sequential<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/macrodef<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;/project<span class="re2">&gt;</span></span></span></div>
<p>We now have a parametrized, reusable piece of functionality which we can use across Ant Builds, and as such, simplifying the build while promoting code reuse. </p>
<p>To use the macrodef in another Ant Build we need only import it and create a target which wraps the macrodef. So we could refactor the distribution targets from the original Build file example to the following:</p>
<div class="dean_ch" style="white-space: wrap;">
<p><span class="sc3"><span class="re1">&lt;import</span> file = <span class="st0">&quot;package.xml&quot;</span> &nbsp;<span class="re2">/&gt;</span></span></p>
<p><span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;package&quot;</span> <span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;package</span>.dist base.<span class="re0">dir</span>=<span class="st0">&quot;${src.dir}&quot;</span> &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dist.<span class="re0">dir</span>=<span class="st0">&quot;${dist.dir}&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dist.<span class="re0">file</span>=<span class="st0">&quot;${dist.src.name}&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;package</span>.dist base.<span class="re0">dir</span>=<span class="st0">&quot;${tests.dir}&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dist.<span class="re0">dir</span>=<span class="st0">&quot;${dist.dir}&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dist.<span class="re0">file</span>=<span class="st0">&quot;${dist.tests.name}&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;package</span>.dist base.<span class="re0">dir</span>=<span class="st0">&quot;${bin.dir}&quot;</span> &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dist.<span class="re0">dir</span>=<span class="st0">&quot;${dist.dir}&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dist.<span class="re0">file</span>=<span class="st0">&quot;${dist.bin.name}&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;package</span>.dist base.<span class="re0">dir</span>=<span class="st0">&quot;${docs.dir}&quot;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dist.<span class="re0">dir</span>=<span class="st0">&quot;${dist.dir}&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dist.<span class="re0">file</span>=<span class="st0">&quot;${dist.doc.name}&quot;</span> &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;package</span>.dist base.<span class="re0">dir</span>=<span class="st0">&quot;${dist.dir}&quot;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dist.<span class="re0">dir</span>=<span class="st0">&quot;${dist.dir}&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dist.<span class="re0">file</span>=<span class="st0">&quot;${dist.package.name}&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re2">/&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;/target<span class="re2">&gt;</span></span></span></div>
<p>And that&#8217;s the basics of using macrodefs to refactor an Ant Build. There is a lot more which can be accomplished with macrodefs in regards to designing and refactoring Ant Builds, specifically <a href="http://ant.apache.org/manual/CoreTypes/antlib.html" target="_blank">antlib</a>, and I encourage you to give it a try as I am sure you will be happy with the results.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ericfeminella.com/blog/2009/02/07/refactoring-ant-builds-with-macrodefs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

