<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><title>Programming @ clintshank.javadevelopersjournal.com</title><link>http://clintshank.javadevelopersjournal.com/</link><description>(Programming) My thoughts on software development.</description><copyright>Copyright 2010 clintshank.javadevelopersjournal.com</copyright><generator></generator><lastBuildDate>Mon, 29 Mar 2010 11:23:00 GMT</lastBuildDate><image><title>Programming @ 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>97 Things Every Programmer Should Know</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/97_things_programmer.htm</guid><link>http://clintshank.javadevelopersjournal.com/97_things_programmer.htm</link><pubDate>Tue, 08 Sep 2009 16:19:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=97%5Fthings%5Fprogrammer</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[The web site for the 3rd book in the <a href="http://search.oreilly.com/?q=97+things">97 Things series</a> recently went public.&nbsp; <a href="http://programmer.97things.oreilly.com/">This one</a> is targeted at programmers.&nbsp; There are 88 contributions that have been edited (mine is <a href="http://programmer.97things.oreilly.com/wiki/index.php/Continuous_Learning">#57</a>) and the exact 97 entries that will go into the final book have yet to be identified. <p>Some of our industry&#39;s thought leaders have already made contributions.&nbsp; If you&#39;d like to contribute and possibly see your name listed alongside them,&nbsp;see <a href="http://programmer.97things.oreilly.com/wiki/index.php/How_to_Become_a_Contributor">How to Become a Contributor</a>.</p>]]></description><category>programming</category></item><item><title>The Difference Between a Property, Field, Attribute</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/difference_property_field.htm</guid><link>http://clintshank.javadevelopersjournal.com/difference_property_field.htm</link><pubDate>Fri, 03 Aug 2007 14:05:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=difference%5Fproperty%5Ffield</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p>I&rsquo;ve been asked a similar question at least three times this year.&nbsp; It goes something like, &ldquo;What&rsquo;s the difference between a field and a property?&rdquo;&nbsp; Or, &ldquo;Is there a difference between an attribute and a property?&rdquo;&nbsp; Given that many inexperienced developers automatically generate getters and setters for all their fields, I can understand the confusion.</p><p>I always give them the example of a circle.&nbsp; Here&rsquo;s some Groovy code:</p><pre>class Circle {<br />&nbsp;&nbsp;&nbsp; def radius<br />&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; def getDiameter() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2 * radius<br />&nbsp;&nbsp;&nbsp; }</pre><pre>&nbsp;&nbsp;&nbsp; def getArea() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Math.PI * Math.pow(radius, 2)<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; def getCircumference() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2 * radius * Math.PI<br />&nbsp;&nbsp;&nbsp; }<br />}</pre><p><em>radius</em> is a field, also known as an instance variable, also known as a member variable in C++.&nbsp; It&rsquo;s an implementation detail of the <em>Circle</em>, i.e., it&rsquo;s a private variable that gets stored as part of the <em>Circle</em> object.</p><p>Properties are more public aspects of an object.&nbsp; In Java, following JavaBeans conventions for getters and setters allows you to expose properties.&nbsp; Those properties don&rsquo;t have to be backed up by fields, but in many cases are.</p><p>Coming back to the Groovy <em>Circle</em>, what are its properties?&nbsp; We&rsquo;ll you get <em>radius</em> for free as a read-write property.&nbsp; But because we&rsquo;ve got some getters that compute other values based on <em>radius</em>, we also have <em>diameter</em>, <em>area</em>, and <em>circumference</em> properties.&nbsp; These happen to be read-only properties since we didn&rsquo;t provide setters, but we could have (which would&nbsp;update the&nbsp;<em>radius</em> field accordingly).&nbsp; You can dump all of a <em>Circle</em> object&rsquo;s properties in Groovy like this: </p><pre>println myCircle.properties</pre><p>The point is <em>Circle</em> could be implemented in different ways: by using a <em>diameter</em> field instead of <em>radius</em>, for instance, but it would have the same properties.</p><p>Now, let&rsquo;s talk about attributes.&nbsp; I typically think of <a href="http://www.uml.org/">UML</a> when I hear attributes.&nbsp; Here&rsquo;s UML for <em>Circle</em>:</p><p><img src="http://files.blog-city.com/files/J06/154001/p/f/circle.png" alt="Circle UML" width="99" height="117" /></p><p>Notice that I&rsquo;ve distinguished the derived <em>diameter</em>, <em>area</em>, and <em>circumference</em> attributes with a &ldquo;/&rdquo;.&nbsp; This is standard UML.&nbsp; <em>radius</em> is a regular, non-derived attribute.&nbsp; We can distinguish fields&nbsp;from other computed properties like this.&nbsp; However, because we&rsquo;re modeling, and thinking in a higher level of abstraction, I like to think of attributes as more synonymous with properties.&nbsp; I&rsquo;d rather not make the assumption that every attribute&nbsp;is implemented with a field.</p><p>A similar discussion of these terms can be found <a href="http://forum.java.sun.com/thread.jspa?threadID=701357&amp;messageID=4068418">here</a>.</p>]]></description><category>programming</category><category>design</category></item><item><title>How to Refactor Many Arguments</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/refactor_many_arguments.htm</guid><link>http://clintshank.javadevelopersjournal.com/refactor_many_arguments.htm</link><pubDate>Fri, 25 May 2007 11:01:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=refactor%5Fmany%5Farguments</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p>The following conversation is based on a true story.&nbsp; The roles are taken from <a href="http://www.objectmentor.com/resources/publishedArticles.html">Robert Martin&rsquo;s Craftsman series</a>.</p><p><span style="font-weight: bold">Apprentice:</span>&nbsp; Hey, can I ask you something?<br /><span style="font-weight: bold">Journeyman:</span>&nbsp; Sure.<br /><span style="font-weight: bold">Apprentice:</span>&nbsp; I was looking at a big constructor on one of our domain objects and I saw some groupings and was wondering whether it would be good to create objects for those groupings.&nbsp; Is there some kind of pattern for that?<br /><span style="font-weight: bold">Journeyman:</span>&nbsp; Well first, how many parameters are there?<br /><span style="font-weight: bold">Apprentice:</span>&nbsp; 72.<br /><span style="font-weight: bold">Journeyman:&nbsp;</span> 72!?&nbsp; Wow.&nbsp; Talk about a <a href="http://c2.com/cgi/wiki?TooManyParameters">code smell</a> .&nbsp; <a href="http://pmd.sourceforge.net/rules/index.html">PMD&#39;s ExcessiveParameterList</a>  rule will flip out on this one.&nbsp; I guess the author mapped those parameters straight from the out-of-our-control XML schema, whose document instances get unmarshalled in order to create the object.&nbsp; We don&rsquo;t have to follow suit and create such a flat domain model that matches the schema.<br /><span style="font-weight: bold">Apprentice:</span>&nbsp; What do you mean?<br /><span style="font-weight: bold">Journeyman:</span>&nbsp; Well, what I really mean is that I&#39;d much prefer distributing the logic among some richer objects.&nbsp; That is, create more little objects with little methods that do a little of the work.&nbsp; Not one big object that does it all.&nbsp; There would be an impedance mismatch between XML and the domain model, but that&rsquo;s typical because of the technology differences.&nbsp; I suppose the only benefit of the big constructor approach is the simpler mapping... maybe, but the negatives are much too great.<br /><span style="font-weight: bold">Apprentice:</span>&nbsp; What are the negatives?<br /><span style="font-weight: bold">Journeyman:</span>&nbsp; Well, for one, trying to list all 72 parameters in the correct order would be a pain.&nbsp; And trying to understand the object as a whole with all those fields would be difficult.&nbsp; Here [grabbing his <a href="http://martinfowler.com/books.html#refactoring">Refactoring</a>  book].&nbsp; You were initially asking about the &ldquo;Introduce Parameter Object&rdquo; refactoring.&nbsp; Read that refactoring.&nbsp; It can probably give you better details.<br /><span style="font-weight: bold">Apprentice:</span>&nbsp; Martin Fowler.&nbsp; Does he know what he&rsquo;s talking about?<br /><span style="font-weight: bold">Journeyman:</span>&nbsp; Oh yeah.&nbsp; He&rsquo;s one of the Masters.&nbsp; Let&rsquo;s take a look at the class.&nbsp; [Brings up the class in the IDE]</p> <pre>  public ImportantDomainObject(</pre><pre>    ...</pre><pre>    int x, int y,</pre><pre>    ...</pre><pre>    double min, double max,</pre><pre>    ...)</pre> <p>Well, I can see a few <a href="http://www.martinfowler.com/bliki/DataClump.html">data clumps</a>  already.&nbsp; You see the &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;?<br /><span style="font-weight: bold">Apprentice:</span>&nbsp; Yeah.<br /><span style="font-weight: bold">Journeyman:</span>&nbsp; That&rsquo;s probably a &lt;code&gt;Point&lt;/code&gt;.&nbsp; Also, that &lt;code&gt;min&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt;, that looks like a &lt;code&gt;Range&lt;/code&gt;.&nbsp; What I would do is search for data clumps like these and create objects for them.&nbsp; Then, see where they&rsquo;re used in &lt;code&gt;ImportantDomainObject&lt;/code&gt; and start moving behavior into the new objects.&nbsp; Distribute the logic.&nbsp; We can create a richer domain model this way.&nbsp; You&rsquo;ll probably find clumps of these clumps and can follow the same process.<br /><span style="font-weight: bold">Apprentice:</span>&nbsp; Okay.&nbsp; This sounds great.&nbsp; Thanks.&nbsp; I&rsquo;m on it.<br /><span style="font-weight: bold">Journeyman:</span>&nbsp; Thank you for recognizing this and taking the initiative to improve the code base.</p><p>&nbsp;</p>]]></description><category>programming</category><category>design</category></item><item><title>Collection Utility Methods on the Object</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/collectionmethodsonobject.htm</guid><link>http://clintshank.javadevelopersjournal.com/collectionmethodsonobject.htm</link><pubDate>Sun, 16 Apr 2006 23:16:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=collectionmethodsonobject</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p class="MsoNormal">&nbsp;</p>
<p>I&rsquo;ve been experimenting lately with a technique that I&rsquo;m still unsure about. It has a little smell to it, but it has some benefits. The technique is to add static collection-related utility methods on an object. That is, I&rsquo;ll add a static method that takes a collection of that object on the object itself.</p>
<p>Take the classic Shape example:</p>
<pre>public class Shape {<br />&nbsp;  // here&rsquo;s a typical instance method   <br />&nbsp;&nbsp; public void draw();<br /><br />   // here's what I&rsquo;m talking about.   <br />&nbsp;&nbsp; public static void draw(Collection shapes) {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (Iterator it = shapes.iterator(); it.hasNext; ) {         <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Shape shape = (Shape) it.next();         <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shape.draw();      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }   <br />&nbsp;&nbsp; }<br />}<br /></pre>
<p>Now, I haven&rsquo;t done this kind of thing much; maybe once or twice. The first time I did it was because I noticed some duplication. The exact looping code was shared by multiple clients. I wanted to remove the duplication, but I didn&rsquo;t know where to put it, so I stuck it on the object itself. The clients became (in the context of this example):</p>
<pre>someShapeClientMethod() {   <br />   ...   <br />   Shape.draw(myShapes);      <br />   ...<br />}</pre>
<p>I think this actually improves clarity (although I could have accomplished the same thing by creating a helper <font face="Courier New">draw()</font> method in the client). This also has the benefit of grouping the iteration logic close to its source, e.g.,<font face="Courier New">Shape</font> here.</p>
<p>The real world example where I first used this was a query.&nbsp; I had a collection of objects and I wanted to find a specific one. Say, I had a collection of Shapes and I wanted to know who was the biggest:</p>
<pre>public static Shape getBiggest(Collection shapes);</pre>
<p>I think this technique is most applicable under the following circumstances:</p>
<ol>
    <li>For good reason, multiple clients share the code and you want to remove the duplication. </li>
    <li>The number of such utility methods is very small. Otherwise, it would be better to group the methods in another, separate utility class (e.g., <font face="Courier New">Shapes</font> or <font face="Courier New">ShapeUtils</font>). </li>
</ol>
<p>What bothers me most is that I can&rsquo;t recall seeing this technique used anywhere. That could mean that there&rsquo;s something really bad about it. One problem is that it clutters the object with additional methods. That&rsquo;s why I think it should be used judiciously. Also, from a purist view, do these methods really belong on the object?</p>
<p>What do you think?</p>]]></description></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>Small Methods: A Best Practice</title><guid isPermaLink="true">http://clintshank.javadevelopersjournal.com/smallmethods.htm</guid><link>http://clintshank.javadevelopersjournal.com/smallmethods.htm</link><pubDate>Sun, 22 Jan 2006 18:09:00 GMT</pubDate><comments>http://clintshank.javadevelopersjournal.com/console/comments/popup/?f=smallmethods</comments><dc:creator>Clint Shank</dc:creator><description><![CDATA[<p class="MsoNormal">If you've read my previous entry <a href="http://clintshank.javadevelopersjournal.com/coding_by_intention.htm">Coding By Intention</a>,&nbsp;you know I like small methods.<span style="">&nbsp; </span>Small methods are easy to understand and easy to maintain - they&rsquo;re small after all.<span style="">&nbsp; </span>The name of a small method can reveal your intent to those higher level methods using it.</p>
<p class="MsoNormal"><span style=""></span>Besides these obvious things, small methods remove duplication and thus provide chunks of reusable code.<span style="">&nbsp; </span>When I&rsquo;m doing test-driven development, after I&rsquo;ve coded by intention and refactored by extracting methods, I wind up with several small methods that are typically private scope in the class I&rsquo;m developing.<span style="">&nbsp; </span>These private methods are potential candidates for reusability - reusability beyond the class I&rsquo;m working with.<span style="">&nbsp; </span>In another blog entry, I want to go into detail on this part, but for now, I&rsquo;ll give you a little teaser: these small, private methods typically belong on another object (one of the objects passed in as an argument) or maybe on a utility class.</p>
<p><o:p></o:p>There are a couple of negatives to small methods.<span style="">&nbsp; </span>One is performance.<span style="">&nbsp; </span>There is extra overhead in making method calls.<span style="">&nbsp; </span>But really, I find this impact negligible and not worth discussing. </p>
<p class="MsoNormal">The biggest negative is when you&rsquo;re tracing through code.<span style="">&nbsp; </span>Maybe you&rsquo;re doing a code review, debugging, or maybe just want to understand all the details.<span style="">&nbsp; </span>It is pretty annoying sometimes to keep drilling down in methods and then trying to remember how you got to where you are and then trying to find your way back to where you started.<span style="">&nbsp; </span>However, I&rsquo;ll make this sacrifice to get all the benefits of reusability, algorithmic clarity, etc. that small methods provide.</p>
<p><o:p></o:p>So how did I get to where I am today?<span style="">&nbsp; </span>Early in my career, I came up with a standard of making methods fit on a printed, landscape page.<span style="">&nbsp; </span>I even printed out a page to see how many lines that was.<span style="">&nbsp; </span>It was about 65.<span style="">&nbsp; </span>I told this to a colleague of mine, Brian Mills.<span style="">&nbsp; </span>I said, &ldquo;I like being able to see an entire method on one page.&rdquo;<span style="">&nbsp; </span>He said, &ldquo;One page?<span style="">&nbsp; </span>I like seeing an entire method on my screen!&rdquo;<span style="">&nbsp; </span>After that, I looked at Brian&rsquo;s code.<span style="">&nbsp; </span>It was the best code I&rsquo;d ever seen.<span style="">&nbsp; </span>I adopted his practice.</p>
<p class="MsoNormal">Shortly afterward, I remember taking an object-oriented analysis and design course.<span style="">&nbsp; </span>During a break, I was discussing the method lines practice with the instructor, who said something like all methods should be 10 lines or less.<span style="">&nbsp; </span>At that time, I couldn&rsquo;t even understand how that was possible, with exception handling, logging and all.</p>
<p><o:p></o:p>Then, the agile movement started and I read books like <a href="http://www.martinfowler.com/books.html#refactoring">Martin Fowler&rsquo;s Refactoring</a> and I learned about coding by intention.<span style="">&nbsp; </span>I started to understand that OOAD instructor.</p>
<p class="MsoNormal">Today, my methods range from one line to a screenful.<span style="">&nbsp; </span>A screenful in my Eclipse IDE is 36 lines.<span style="">&nbsp; </span>Those 36 lines include the start of Javadoc to the ending bracket of the method.<span style="">&nbsp; </span>The one line methods are either trivial or intention revealing methods (like the <font face="Courier New">notTooCold()</font> method from my earlier blog).<span style="">&nbsp; </span>Admittedly, I do go over a screen from time to time.<span style="">&nbsp; </span>It&rsquo;s most likely because I&rsquo;ve been lazy.<span style="">&nbsp; </span>Whenever I feel a method is getting too long, I look for reusability and intention, and extract methods.</p>
<p class="MsoNormal"><br /></p>]]></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>