<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><title>Java @ clintshank.javadevelopersjournal.com</title><link>http://clintshank.javadevelopersjournal.com/</link><description>(Java) My thoughts on software development.</description><copyright>Copyright 2008 clintshank.javadevelopersjournal.com</copyright><generator></generator><lastBuildDate>Fri, 05 Sep 2008 17:00:00 GMT</lastBuildDate><image><title>Java @ clintshank.javadevelopersjournal.com</title><url>http://res.sys-con.com/portlet/163/featured-blog-graphic-145.gif</url><link>http://clintshank.javadevelopersjournal.com/</link></image><ttl>360</ttl><docs>http://backend.userland.com/rss</docs><item><title>Essence Over Ceremony in Unit Testing</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/essence_over_ceremony_in_unit_testing.htm</guid><link>http://clintshank.javadevelopersjournal.com/essence_over_ceremony_in_unit_testing.htm</link><pubDate>Sat, 09 Aug 2008 14:45:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=essence%5Fover%5Fceremony%5Fin%5Funit%5Ftesting</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p>There&#39;s been some talk recently about essence and ceremony, particularly regarding JVM programming languages. My first remembrance of this discussion was reading <a href="http://blog.thinkrelevance.com/2008/4/1/ending-legacy-code-in-our-lifetime">this blog entry</a> from Stu Halloway.&nbsp; Java is a ceremonious language because there&#39;s a lot of extra, requried typing that blurs the essence of what you&#39;re trying to communicate in the code.</p><p>I want to talk about essence vs. ceremony in unit testing.</p><p>If I give you this:</p><pre>OrderTest<br />    testCalculatePrice<br />        1 20.00<br />        2  5.00<br />          30.00<br /></pre><p>Can you tell what this is about? This is a specification of the calculatePrice() method on an Order object, where there are two LineItem objects and the expected price is $30.00. Did you figure that out without the explanation? This is essence.</p><p>Instead, what you often see is something like this:</p><pre>public class OrderTest { </pre><pre>    @Test<br />    public void testCalculatePrice() {<br />        Order order = new Order();<br />        Product product1 = new Product(20.00);<br />        LineItem lineItem1 = new LineItem(1, product1);<br />        order.add(lineItem1);<br />        Product product2 = new Product(5.00);<br />        LineItem lineItem2 = new LineItem(2, product2);<br />        order.add(lineItem2);<br />        assertEquals(30.00, order.calculatePrice());<br />    }<br />}</pre><p>Here, you&#39;re blinded with irrelevant details that <a href="http://xunitpatterns.com/Obscure%20Test.html">obscure</a> what this test is all about. The essence of the specification is buried in details. What you want is clarity at this level.</p><p>You can factor out the obscurity with something like this:</p><pre>public class OrderTest {<br />    private Order order = new Order();</pre><pre>    @Test<br />    public void testCalculatePrice() {<br />        lineItem(1, 20.00);<br />        lineItem(2, 5.00);<br />        expectPrice(30.00);<br />    }<br />}</pre><p>That&#39;s gets us about as close as we can in Java.</p><p>A consequence of this approach is that you have more methods overall, but I&#39;ve always felt that clarity trumps in specifications. <a href="http://thediscoblog.com/2008/07/02/its-ok-to-wet-yourself-every-once-in-awhile/">Others have agreed</a>.</p><p>What&#39;s interesting with these helper methods is that I can change how I implement them and not change the higher level, essence methods at all. For example, in lineItem() I can inject real production objects (as I did in the original example with LineItem and Product), or I can inject a mock or stub LineItem.</p><p>So my suggestion is when you are specifying these high level methods, attempt to only show the essence of the test and factor out the ceremony.</p><p>&nbsp;</p>]]></description><category>testing</category><category>java</category></item><item><title>A Logical Ordering of Import Statements</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/importordering.htm</guid><link>http://clintshank.javadevelopersjournal.com/importordering.htm</link><pubDate>Fri, 03 Mar 2006 22:54:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=importordering</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p>Do you care about the order of your Java import statements?&nbsp; Well, maybe not, but I do.&nbsp; The reason is because I like to do a visual check of my dependencies.&nbsp; According to the <a href="http://www.objectmentor.com/resources/articles/granularity.pdf">Acyclic Dependencies Principle</a>, there should be no cycles in packages.&nbsp; Robert Martin wrote <a href="http://www.butunclebob.com/ArticleS.UncleBob.JdependFixture">here</a>, &quot;Remember that the prime motivation for using Object Oriented Design is to manage module dependencies.&quot;&nbsp; One way I check my dependencies is by scanning the import statements of a class or interface to make sure it depends on other, appropriate classes and interfaces.</p>
<p>The rule I use is that you can go up a package hierarchy and over to the right, but you can&rsquo;t go down or to the left.&nbsp; (Props go to <a href="http://gaffney.wsj2.com/">Michael Gaffney</a> for working with me to come up with this rule years ago).&nbsp; Over to the right?&nbsp; I&rsquo;m working in the context of a layered architecture and when I say &ldquo;over to the right,&rdquo; I&rsquo;m talking about something like this:</p>
<pre>   presentation -&gt; business -&gt; persistence -&gt; general utilities</pre>
<p>So starting on the left, dependencies flow to the right.&nbsp; You wouldn't want to go back to the left.&nbsp; That would introduce a cycle.&nbsp; Also, within a layer, you could go up the package hierarchy, but you wouldn't want to go down.</p>
<p>Here&rsquo;s an example.&nbsp; Suppose I&rsquo;m developing a web application and I&rsquo;m working on a class in the presentation layer.&nbsp; I might have something like (contrived):</p>
<pre>   package com.mycompany.myapp.web.struts;</pre>
<pre>   import java.util.List;<br />   import javax.servlet.http.HttpServletRequest;<br />   import javax.servlet.http.HttpServletResponse;<br />   import org.apache.commons.beanutils.PropertyUtils;<br />   import org.apache.struts.action.ActionForm;<br />   import org.apache.struts.action.ActionForward;<br />   import org.apache.struts.action.ActionMapping;</pre>
<pre>   import com.mycompany.myapp.utils.SomeUtil;<br />   import com.mycompany.myapp.domain.SomeDomainObject;<br />   import com.mycompany.myapp.domain.SomeOtherDomainObject;<br />   import com.mycompany.myapp.web.SomethingHigher;<br />   import com.mycompany.myapp.web.utils.SomethingToTheRightWithinModule;</pre>
<p>Now, ideally, I&rsquo;d like to list those imports in the exact opposite order.&nbsp; That way, I could start at the package statement and look down and make sure the imports flow in an acceptable, directed acyclic graph kind of order, following the rule above.&nbsp; In fact, I used to list them in this order, but everybody else lists them in an order more like the above, and so, I accepted defeat and now follow suit.&nbsp; I just start at the bottom and look up.</p>
<p>I only care about ordering among packages here.&nbsp; I don&rsquo;t try to list the imports in dependency order among classes at the same package level.&nbsp; That would be crazy.&nbsp; However, I am strict within the layer of the class I&rsquo;m working with.&nbsp; In the example, that would be the web layer.&nbsp; I&rsquo;m looking at a class in <font face="Courier New">...web.struts</font> and I want to make sure that I&rsquo;m going to the right first, then up, all within <font face="Courier New">com.mycompany.myapp.web</font> in an appropriate package order.</p>
<p>Once I&rsquo;m past the web layer, I loosen up.&nbsp; I just want to make sure all the imports for the next layer are grouped together, and so on.&nbsp; So in the example, all the <font face="Courier New">domain</font> imports would be together, then all the <font face="Courier New">utils</font>, and so on, all the way up to <font face="Courier New">java.util</font>.</p>
<p>You can set up this ordering standard in Eclipse under the Organize Imports preference.</p>
<p>How else could you check your dependencies?&nbsp; There are useful tools out there like <a href="http://www.clarkware.com/software/JDepend.html">JDepend</a>, and I use them.&nbsp; But I still like this import ordering practice.&nbsp; I typically do the manual scan right before check in time.<br /></p>]]></description></item><item><title>A Common Set of Test Refactorings</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/commontestrefactorings.htm</guid><link>http://clintshank.javadevelopersjournal.com/commontestrefactorings.htm</link><pubDate>Sun, 19 Feb 2006 13:02:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=commontestrefactorings</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[Today, I want to blog about what I think might be my most common set of low level Eclipse refactorings used together when I&rsquo;m developing unit tests.&nbsp;I use this set to parameterize tests as discussed in <a href="http://clintshank.javadevelopersjournal.com/tabulartests.htm">Tabular Tests</a>.&nbsp;Lately, I&rsquo;ve been trying to use the Eclipse refactorings as much as possible, thinking that the automation would be less likely to break things than if I made the changes manually.&nbsp;It&rsquo;s also interesting to make code changes without doing any typing.&nbsp;The set consists of:
<ol type="1" style="margin-top: 0in;" start="1">
    <li><font face="Courier New">Extract      Local Variable</font> (possibly multiple times).</li>
    <li><font face="Courier New">Extract      Method</font></li>
    <li><font face="Courier New">Inline</font>      the local variables created in step 1.</li>
</ol>
So let&rsquo;s suppose I&rsquo;m about to start work on a new method.&nbsp;I like to start with a brain dump of most, if not all of the test cases.&nbsp;This is to get me thinking about the desired behavior and also because it clears my head, so that when I start writing the code, I can focus.&nbsp;Once I&rsquo;m ready, I start with a simple case.
<p>Let&rsquo;s take the <font face="Courier New">Range.equals()</font> example from the previous blog.&nbsp;My brain dump would have resulted in a table like this:</p>
<p>
<table width="200" cellspacing="1" cellpadding="1" border="1" align="">
    <tbody>
        <tr>
            <td><strong>Min</strong></td>
            <td><strong>Max</strong></td>
            <td><strong>Equals?</strong></td>
        </tr>
        <tr>
            <td>same</td>
            <td>same</td>
            <td>true</td>
        </tr>
        <tr>
            <td>different</td>
            <td>same</td>
            <td>false</td>
        </tr>
        <tr>
            <td>same</td>
            <td>different</td>
            <td>false</td>
        </tr>
    </tbody>
</table>
</p>
<p>Yes, I would have jotted down other, exceptional cases for things like <font face="Courier New">null</font> and a non-<font face="Courier New">Range</font> object, but that&rsquo;s out of scope here.&nbsp;For this blog, I&rsquo;m just concentrated on comparing <font face="Courier New">Range</font> objects.&nbsp;I start with the positive case and come up with something like:</p>
<p><code><span style="font-size: 10pt;">&nbsp;&nbsp; private static final int MIN = 1;<br /> &nbsp;&nbsp; private static final int MAX = 7;<br /> <br /> &nbsp;&nbsp; private Range range = new Range(MIN, MAX);</span></code> </p>
<div><code>&nbsp;</code></div>
<code><span style="font-size: 10pt;">&nbsp; &nbsp;public void testEqualsAnotherRange() {<br /> &nbsp; &nbsp; &nbsp; Range anotherRange = new Range(MIN, MAX);<br /> &nbsp; &nbsp; &nbsp; assertEquals(range, anotherRange);<br /> &nbsp; &nbsp;}</span></code>
<p>&nbsp;</p>
<p><code><span style="font-size: 10pt;"> </span></code>To be honest, I probably would have had the <font face="Courier New">MIN</font> and <font face="Courier New">MAX</font> constants hardcoded in there, but at some point, I would have performed <font face="Courier New">Extract Constant</font> to come up with the above.</p>
<p>After I get that test to pass (by returning <font face="Courier New">true</font>), I&rsquo;m ready to write the next test.&nbsp;I want to pick something simple again (of course in this example they&rsquo;re all simple).&nbsp;But before I write the next test, I do something that is probably considered cheating by the hardcore test-driven development crowd.&nbsp;I&rsquo;m not too concerned because I know from experience that I&rsquo;m going to end up with tabular tests.&nbsp;In fact, the table is specified above! &nbsp;So here goes the set of refactorings:</p>
<p>I first <font face="Courier New">Extract Local Variable</font> on <font face="Courier New">MIN</font> and <font face="Courier New">MAX</font>:</p>
<p><code><span style="font-size: 10pt;">&nbsp; &nbsp;public void testEqualsAnotherRange() {<br /> &nbsp; &nbsp; &nbsp; <strong>int testMin = MIN;<br /> &nbsp; &nbsp; &nbsp; int testMax = MAX;</strong><br /> &nbsp; &nbsp; &nbsp; Range anotherRange = new Range(<strong>testMin, testMax</strong>);<br /> &nbsp; &nbsp; &nbsp; assertEquals(range, anotherRange);<br /> &nbsp; &nbsp;}</span></code></p>
<p><code><span style="font-size: 10pt;"></span></code><span style=""><br /> </span>Now I know from my table that I need a <font face="Courier New">boolean</font> for my expected equals. There's no simple Eclipse refactoring so I manually make the change: </p>
<p><code><span style="font-size: 10pt;">&nbsp;&nbsp; public void testEqualsAnotherRange() {<br /> &nbsp; &nbsp; &nbsp; int testMin = MIN;<br /> &nbsp; &nbsp; &nbsp; int testMax = MAX;<br /> &nbsp; &nbsp; &nbsp; <strong>boolean expectedEquals = true;</strong><br /> &nbsp; &nbsp; &nbsp; Range anotherRange = new Range(testMin, testMax);<br /> &nbsp; &nbsp; &nbsp; <strong>assertEquals(expectedEquals, range.equals(anotherRange));</strong><br /> &nbsp; &nbsp;}</span></code></p>
<p><code><span style="font-size: 10pt;"></span></code><span style=""><br /> </span>I highlight the last two lines and perform <font face="Courier New">Extract Method</font>:<br /><br /> <code><span style="font-size: 10pt;">&nbsp; &nbsp;public void testEqualsAnotherRange() {<br /> &nbsp; &nbsp; &nbsp; &nbsp;int testMin = MIN;<br /> &nbsp; &nbsp; &nbsp; &nbsp;int testMax = MAX;<br /> &nbsp; &nbsp; &nbsp; &nbsp;boolean expectedEquals = true;<br /> &nbsp; &nbsp; &nbsp; &nbsp;<strong>testEqualsAnotherRange(testMin, testMax, expectedEquals);</strong><br /> &nbsp; &nbsp;}<br /> <br /><strong> &nbsp; &nbsp;private void testEqualsAnotherRange(int testMin, int testMax,<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean expectedEquals) {<br /> &nbsp; &nbsp; &nbsp; &nbsp;Range anotherRange = new Range(testMin, testMax);<br /> &nbsp; &nbsp; &nbsp; &nbsp;assertEquals(expectedEquals, range.equals(anotherRange));<br /> &nbsp; &nbsp;}</strong></span></code></p>
<p><code><span style="font-size: 10pt;"></span></code><span style=""><br /> </span>I want to get back to what it looks like in the table, so I <font face="Courier New">Inline</font> each local variable:</p>
<p><code><span style="font-size: 10pt;">&nbsp;&nbsp; public void testEqualsAnotherRange() {<br />&nbsp; &nbsp; &nbsp; testEqualsAnotherRange(<strong>MIN, MAX, true</strong>);<br /> &nbsp; &nbsp;}<br /> <br /> &nbsp; &nbsp;private void testEqualsAnotherRange(int testMin, int testMax,<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean expectedEquals) {<br /> &nbsp; &nbsp; &nbsp; Range anotherRange = new Range(testMin, testMax);<br /> &nbsp; &nbsp; &nbsp; assertEquals(expectedEquals, range.equals(anotherRange));<br /> &nbsp; &nbsp;}</span></code></p>
<p><code><span style="font-size: 10pt;"></span></code><span style=""><br /> </span>I just created the first row of the table by applying the set of refactorings that is the topic of this blog.&nbsp;At this point, I usually look at the parameterized method and make sure I&rsquo;m happy with the signature.&nbsp;I&rsquo;ve already done most of this when I extracted the method, but Eclipse doesn&rsquo;t let me make the method <font face="Courier New">static</font>, if that&rsquo;s appropriate (in this case it&rsquo;s not).&nbsp;Eclipse also lists all the checked exceptions, which for tests, I&rsquo;d rather generalize to <font face="Courier New">Exception</font>.&nbsp; This doesn't apply in this case either.<br /></p>
<p>During this set of refactorings, I would have ran the tests after a change or two just to make sure I didn&rsquo;t break anything.&nbsp;I also do that because it feels good to get that green bar.</p>
<p>This is a pretty trivial example.&nbsp;Most of the time much more is going on in the parameterized method, but this still shows the removal of duplication.</p>
<p>I could have made these changes manually, but like I mentioned above, I like the automation; I&rsquo;m less likely to break things.</p>
<div>Okay, I can write the next test now:</div>
<p><code><span style="font-size: 10pt;">&nbsp;&nbsp; public void testEqualsAnotherRange() {<br /> &nbsp; &nbsp; &nbsp; testEqualsAnotherRange(MIN, &nbsp; &nbsp; MAX, true);<br /> &nbsp; &nbsp; &nbsp; <strong>testEqualsAnotherRange(MIN - 1, MAX, false);</strong><br /> &nbsp; &nbsp;}</span></code></p>
<p><code><span style="font-size: 10pt;"> </span></code>I&rsquo;ve got the red bar.&nbsp;Time to make it green.</p>
<div>&nbsp;</div>]]></description></item><item><title>TDD Pattern: Tabular Tests</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/tabulartests.htm</guid><link>http://clintshank.javadevelopersjournal.com/tabulartests.htm</link><pubDate>Sun, 05 Feb 2006 19:23:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=tabulartests</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p><font face="Arial">Today, I thought I'd blog about one of my most common testing patterns.&nbsp; I call it table-like tests because I end up with a test method that calls a helper, parameterized test method.&nbsp; The high level test method shows all the test cases in what looks kind of like a table.&nbsp; </font>     To give proper credit to this technique, I believe I read about it on <a href="http://www.junit.org/">www.junit.org</a> or one of the XP sites years ago, but I couldn't find the article with a quick Google search.&nbsp; To demonstrate, <font face="Arial">suppose I were working on a <font face="Courier New">Range</font> class and was currently focused on the </font><font face="Arial"><font face="Courier New">equals() </font>method.&nbsp; I'd end up with the following:</font></p>
<font face="Courier New">&nbsp;&nbsp; private static final int MIN = 1;<br />&nbsp;&nbsp; private static final int MAX = 7;<br /><br /> &nbsp;&nbsp; private Range range = new Range(MIN, MAX);<br /><br />&nbsp;&nbsp; public void testEqualsAnotherRange() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; testEqualsAnotherRange(MIN,&nbsp;&nbsp;&nbsp;&nbsp; MAX, true);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; testEqualsAnotherRange(MIN - 1, MAX, false);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; testEqualsAnotherRange(MIN,&nbsp;&nbsp;&nbsp;&nbsp; MAX + 1, false);<br /></font><font face="Courier New"><font face="Courier New">&nbsp;&nbsp; }<br /><br />&nbsp;&nbsp; private void testEqualsAnotherRange(int testMin,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int testMax, boolean expectedEquals) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br />&nbsp;&nbsp; }<br /></font>  <br /></font>
<p><font face="Arial">Now, I know there are other tests for the <font face="Courier New">equals()</font> method, but I'm focusing on the meat of the tests here.&nbsp; I also want to emphasize that I don't start out specifying all the tests like this.&nbsp; I actually write one of the test cases, get that test to pass by minimally implementing the <font face="Courier New">Range</font> class, and then, as I start to write the next test case, I see the pattern and will extract the method and parameterize it for the next test case.</font></p>
<p><font face="Arial"> </font><font face="Arial">Time to make some points:</font></p>
<ol>
    <li>Test cases for the <font face="Courier New">equals()</font> method are grouped together.&nbsp; If I want to know how the <font face="Courier New">equals()</font> method works for non-exceptional cases, all the test cases are right there. When I write code, I focus on a method for a particular production class.&nbsp; That is, I'm trying to build out all the functionality for that method before moving on to the next method (or class).&nbsp; I find it helpful to have all the test cases grouped together.&nbsp; Parameterizing tests keeps the test cases as close together as possible.</li>
    <li>Whenever I detect duplication, I strive to remove it.&nbsp; This is a way to do so among the test cases.</li>
    <li>The mapping between test methods and production class methods is nearly one to one.&nbsp; There's one main <font face="Courier New">testEquals() </font>method for one <font face="Courier New">equals()</font> method.&nbsp; Of course, I'll have test methods for exceptional cases, but I don't have much more test methods than production methods.&nbsp; If I kept each test case in its own method, I'd have a harder time removing duplication and I would have much more test code than production code.<br /></li>
</ol>
<p><font face="Arial"> </font></p>
<p><font face="Courier New"><font face="Arial">I should also point out that many times, I don't use the same fixture.&nbsp; In the example above, I did, but many times I don't.&nbsp; The parameters passed in to the test helper method could be used to setup the production object.&nbsp; Sometimes, the parameters are used to help mock a nested object.&nbsp;&nbsp; Take, for example, the typical web site <font face="Courier New">Order</font> class.&nbsp; Let's suppose I were focused on the </font><font face="Arial"><font face="Courier New">getTotal()</font> method:</font></font></p>
<font face="Courier New"><font face="Courier New">&nbsp;&nbsp; private Order order = new Order();<br /><br />&nbsp;&nbsp; public void testGetTotal() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; assertEquals(0.00, order.getTotal(), 0.0001);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; testGetTotal(1, 3.00, 3.00);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; testGetTotal(3, 5.00, 18.00);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // etc.<br />&nbsp;&nbsp; }<br /><br />&nbsp;&nbsp; private void testGetTotal(int lineItemQuantity,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; double lineItemPrice, double expectedTotal) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // create a line item<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // add line item to order<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // verify total<br />&nbsp;&nbsp; }<br /></font><br /><font face="Arial"><br /></font></font>
<p><font face="Courier New"><font face="Arial">Now, I should point out some of the concerns of using this technique:</font></font> <br /></p>
<ol>
    <li><font face="Courier New"><font face="Arial">It doesn't follow the JUnit convention of working with the same fixture per <font face="Courier New">TestCase</font>.&nbsp; Many times, my &quot;fixture&quot; is just an empty production object that will be setup in individual test methods.&nbsp; B</font></font><font face="Arial">ehaviour Driven Development (BDD) is getting press these days and its focus is on fixtures called Contexts.&nbsp; Caveat: my knowledge of BDD is from reading this one <a href="http://blog.daveastels.com/files/sdbp2005/BDD%20Intro.pdf">article</a> by Dave Astels, and I haven't experimented with it.&nbsp; It seems to be good for showing behavior in a given context, whereas my technique is method focused.&nbsp; Maybe I'll give it a shot in the future.<br /></font></li>
    <li><font face="Courier New"><font face="Arial">Because there isn't a real fixture, the test methods are more static than object-oriented.&nbsp; That is, I might not be working with instance variables of the <font face="Courier New">TestCase</font>.&nbsp; I don't find this to be an issue though.&nbsp; I thought I'd point it out for OO purists.<br /></font></font></li>
    <li><font face="Courier New"><font face="Arial">Gerard Meszaros, who is writing a good book on <a href="http://tap.testautomationpatterns.com:8080/Book%20Outline.html">test automation patterns</a>, pointed out that if one of my earlier tests fails, then later tests won't be run.&nbsp; He also pointed out that it is harder to determine how many total test cases there are since some are embedded.&nbsp; These are valid points.&nbsp; To counter, I mentioned that I am following test-driven development, so I am building the tests as I go, and I usually don't run into the situation where an earlier test fails, although it does happen.&nbsp; I also never run into the situation where a bunch of tests are all of the sudden failing.&nbsp; As for the number of test cases, I've never had a need to know.&nbsp; But his points are definitely valid.</font></font></li>
    <li><font face="Courier New"><font face="Arial">Tests aren't isolated.&nbsp; You don't start fresh each time.&nbsp; Thus, earlier tests could have unwanted side effects.&nbsp; You have to be aware of this.&nbsp; I either want side effects (as in the <font face="Courier New">Order</font> example where I'm building up an <font face="Courier New">Order</font>) or I simply start with an empty production object and build it up in the helper test method.<br /></font></font></li>
</ol>
<font face="Courier New"><br /></font>]]></description></item><item><title>Coding By Intention</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/coding_by_intention.htm</guid><link>http://clintshank.javadevelopersjournal.com/coding_by_intention.htm</link><pubDate>Tue, 10 Jan 2006 17:56:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=coding%5Fby%5Fintention</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p>I thought I'd start blogging about something I think I'm pretty decent at - programming.&nbsp; Yeah, yeah, I know, everybody thinks they are the best programmer, but I think I have some decent stuff to share.&nbsp; I wanted to start with programming tips.&nbsp; I'll try one tip per blog entry.</p>
<p>The first tip is coding by intention.&nbsp;The first time I learned of this was in college.&nbsp;I was reviewing some code with a professor of mine, Dave Binkley of Loyola College.&nbsp;We were reviewing a long method of mine and Professor Binkley said something equivalent to, &ldquo;I like to extract a method here to make it more clear what I&rsquo;m doing.&rdquo;&nbsp;The light came on.&nbsp;I learned that programming was more than just a science, but an art.&nbsp;This was back in the early 90s, before the Extreme Programming/Agile folks coined terms like &ldquo;extract method&rdquo;, &ldquo;refactor&rdquo;, and &ldquo;code by intention&rdquo;.</p>
<p>Now, I'm a big fan of Test-Driven Development (TDD).&nbsp; Since I've got to write a test first, I start there.&nbsp; The real code doesn't exist yet.&nbsp; The intent part starts here.&nbsp; I think, &quot;I've got this piece of functionality to implement.&nbsp; What would I call if I could write anything?&nbsp; What would the method name be and its arguments?&nbsp; How would I call this functionality as a client so that what I'm doing is crystal clear?&nbsp;The world is my oyster.&quot;</p>
<p>Coding by intention from the point of view of the test is pretty obvious.&nbsp; What I wanted to emphasize in this blog entry is to continue coding by intention all the way through until you've completely implemented everything.&nbsp; So after you've created a new call in the test, you start implementing it.&nbsp; It'll most likely be a public method.&nbsp; When you're implementing this, you continue coding by intention.&nbsp; You could just crank out a possibly long method to get the functionality implemented, but that's not what I'm talking about &ndash; that&rsquo;s what I did in the early days of college.&nbsp; The public method now becomes like another client.&nbsp; You code by intention here, asking yourself what you would call if what you needed already existed.&nbsp; You keep doing this with every method you need to implement, creating lots of small, but communicative and easy to understand methods.&nbsp; Eventually, you'll get to a level where the implementation is small and simple and you're done.&nbsp; What you've done is created some possibly reusable and very easy to maintain methods along the way.</p>
<p>It&rsquo;s time for an example.&nbsp;Suppose I&rsquo;m working with the usual Order/Line Items web site shopping example.&nbsp;One coding by intention technique I use when I need to process a collection like LineItems looks like this:</p>
<div><span style="FONT-SIZE: 10pt"><font face="Courier New">&nbsp;&nbsp;&nbsp; public void processLineItems(List lineItems) {</font></span></div>
<div style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt"><font face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Iterator it = lineItems.iterator();</font></span></div>
<div style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt"><font face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (it.hasNext()) {</font></span></div>
<div style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt"><font face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; process((LineItem) it.next());</font></span></div>
<div style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt"><font face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font></span></div>
<div style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt"><font face="Courier New">&nbsp;&nbsp;&nbsp; }</font></span></div>
<div style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt"></span></div>
<p>Instead of writing all the code in the while loop to process an item, I code by intention, pretending that the <font face="Courier New">process(LineItem)</font> method already exists.</p>
<p>Here&rsquo;s another, albeit obviously contrived example.&nbsp;Suppose I&rsquo;m working on Goldilocks&rsquo; Porridge class and need to implement the <font face="Courier New">isJustRight()</font> method.&nbsp;Right now I just have:</p>
<div><span style="FONT-SIZE: 10pt"><font face="Courier New">&nbsp;&nbsp;&nbsp; public class Porridge {</font></span></div>
<div style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt"><font face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private double currentTemperature = 0.0;</font></span></div>
<div style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt"><font face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font></span></div>
<div style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt"><font face="Courier New">&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public Porridge(double currentTemperature) {</font></span></div>
<div style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt"><font face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.currentTemperature = currentTemperature;</font></span></div>
<div style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt"><font face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font></span></div>
<div style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt"><font face="Courier New">&nbsp;&nbsp;&nbsp; }</font></span></div>
<div style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt"></span></div>
<p>To implement <font face="Courier New">isJustRight()</font>, in coding by intention style, I would do:</p>
<div style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt"><font face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public boolean isJustRight() {</font></span></div>
<div style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt"><font face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (notTooHot() &amp;&amp; notTooCold());</font></span></div>
<div style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt"><font face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font></span></div>
<div style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 10pt"></span></div>
<p>So why code by intention?&nbsp;There are several reasons I can think of off the bat.&nbsp;The first is, as I&rsquo;ve stated above, you end up creating small, possibly reusable methods.&nbsp;My first tip was going to be &ldquo;create small methods&rdquo;, but I realized that coding by intention drives that.&nbsp;In my next entry, I&rsquo;ll go into more detail on why I think creating small methods is really important.&nbsp;There&rsquo;s a process to crafting elegant software and this is the first step.</p>
<p>Another reason is that the top level methods become really easy to understand.&nbsp;<br />They describe the algorithm in almost pseudo-code level.&nbsp;They communicate your intent of what you&rsquo;re trying to accomplish step by step.</p>
<p>Yet another reason is that it is sometimes hard to get started on a task and by coding by intention you are delaying all the details, but still getting things done.&nbsp;That is, not everything you need is available, but you&rsquo;re coding like it is.&nbsp;You&rsquo;re working top down, drilling down into the details level by level until your done, making progress along the way.</p>
<p>Now, if you&rsquo;re doing true TDD, you are writing just enough test to get a failing test, and just enough code to pass that test.&nbsp;Remember, compiler errors count as failures.&nbsp;I&rsquo;m a little lenient on this practice.&nbsp;I like writing an entire test method for one test case scenario before going to the code.&nbsp;So in the <font face="Courier New">isJustRight()</font> method above, I&rsquo;d first write a complete test method for a Porridge object that is too cold with all its calls and assertions.&nbsp;Then, I&rsquo;d write just enough code to get the test to pass, but still coding by intention in the Porridge class.&nbsp;So, in this case, the <font face="Courier New">isJustRight()</font> method would just call <font face="Courier New">notTooCold()</font> for now.</p>
<p>I suppose if you truly wanted to follow TDD, and in particular, get a passing test as fast as you could, you could just write the code in <font face="Courier New">isJustRight()</font>, then extract a <font face="Courier New">notTooCold()</font> method.&nbsp;Just remember to do this.&nbsp;Sometimes, we as programmers get a little lazy.&nbsp;Still, I favor coding by intention top down from the beginning.</p>
<p>Well, that&rsquo;s it for my first blog.&nbsp;Hope you enjoyed.</p>
<div>&nbsp;</div>]]></description></item></channel></rss>