<?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>non-random ramble &#187; design</title>
	<atom:link href="http://jimbarritt.com/non-random/category/design/feed/" rel="self" type="application/rss+xml" />
	<link>http://jimbarritt.com/non-random</link>
	<description>adventures in code</description>
	<lastBuildDate>Fri, 23 Jul 2010 13:42:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Online Mind Mapping Tool</title>
		<link>http://jimbarritt.com/non-random/2010/04/17/online-mind-mapping-tool/</link>
		<comments>http://jimbarritt.com/non-random/2010/04/17/online-mind-mapping-tool/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 11:46:44 +0000</pubDate>
		<dc:creator>Jim Barritt</dc:creator>
				<category><![CDATA[ides]]></category>
		<category><![CDATA[modelling]]></category>
		<category><![CDATA[thoughtblog]]></category>

		<guid isPermaLink="false">http://jimbarritt.com/non-random/?p=367</guid>
		<description><![CDATA[Mind mapping, invented by]]></description>
			<content:encoded><![CDATA[<p><a href="http://jimbarritt.com/non-random/wp-content/uploads/2010/04/MindMeister.gif" rel="lightbox[367]"><img src="http://jimbarritt.com/non-random/wp-content/uploads/2010/04/MindMeister_small.gif" alt="" title="MindMeister_small" width="567" height="289" class="alignnone size-full wp-image-387" /></a></p>
<p>Mind mapping, invented by <a href=http://en.wikipedia.org/wiki/Tony_Buzan">Tony Buzan</a> is a way to get lots of ideas down and relate them together.</p>
<p><a href="http://www.mindmeister.com">MindMeister</a> is a great online Mind Mapping tool. It can be used for free but for $59 for a year you get things like offline access. It is completely written using html and javascript.</p>
<p>Map On!</p>
<p><a href="http://www.mindmeister.com"><br />
<img src="http://webworkerdaily.files.wordpress.com/2007/03/mindmeister-logo.png?w=257&#038;h=56" /><br />
</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jimbarritt.com/non-random/2010/04/17/online-mind-mapping-tool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using tests to help refactor a legacy codebase</title>
		<link>http://jimbarritt.com/non-random/2009/06/10/using-tests-to-help-refactor-a-legacy-codebase/</link>
		<comments>http://jimbarritt.com/non-random/2009/06/10/using-tests-to-help-refactor-a-legacy-codebase/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 11:12:32 +0000</pubDate>
		<dc:creator>Jim Barritt</dc:creator>
				<category><![CDATA[legacy code]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[thoughtblog]]></category>

		<guid isPermaLink="false">http://jimbarritt.com/non-random/2009/06/10/using-tests-to-help-refactor-a-legacy-codebase/</guid>
		<description><![CDATA[I have been tidying up a legacy code base recently. Its actually my own code, and there&#8217;s plenty to be done. Of course it was written under hard time pressure and I was working on my own so the quality has suffered. My discipline was not at the level at which I would apply to [...]]]></description>
			<content:encoded><![CDATA[<p>I have been tidying up a legacy code base recently. Its actually my own code, and there&#8217;s plenty to be done. Of course it was written under hard time pressure and I was working on my own so the quality has suffered. My discipline was not at the level at which I would apply to a commercial project. Which is an interesting learning in itself.</p>
<p>Here are some (quite lengthy) notes about the process I went through to clean up this code. </p>
<p>The first thing I noticed about the codebase was that although I had a build file which did lots of cool things, it didn&#8217;t run the tests automatically.</p>
<p><b>Step One &#8211; re-factor the build script</b></p>
<p>I wanted to tidy up the build script. It was quite long and hard to understand. Also I want to distribute this project on Google code to people and so I wanted to make it possible for people to simply checkout the code, and type &#8220;go&#8221; to get a distributable package built.</p>
<p>One of the issues with this is the dependant libraries. So I introduced Ivy to manage this for me (other posts to come on this topic).</p>
<p>Secondly I wanted to compile and run source code, unit tests functional tests (i.e. tests which may use external resources and involve a set of objects working together) and hopefully acceptance tests. </p>
<p>In ant, doing all this can require quite a bit of duplication. So I broke out some <a h ref="http://ant.apache.org/manual/CoreTasks/macrodef.html">macrodefs</a> to handle compilation and execution (again I&#8217;ll post this as a separate item hopefully).</p>
<p>Ok so the build looks sweet. When I finally run it and get it to open the junit reports in a browser for me, I see heaps of errors and failures! oh dear. I obviously hadn&#8217;t been running the tests every time I checked in. Bad developer!</p>
<p>So, step one with the legacy code base is to get all the tests runnable from the command line ON THE DEV BOX (I have seen a project where this was not simple to do and so the devs relied on the &#8220;CI&#8221; build to do this for them. not fast enough feedback).</p>
<p>I would like to get a CI build set up for it, but not sure where I&#8217;m going to host it. I think that the precedence should be FIRST dev build, THEN CI. If you cant get feedback on your dev box you are asking for integration problems.</p>
<p><b>Step Two &#8211; Make the tests green</b></p>
<p>Most tests were easy to fix. There were several types of failure / error:</p>
<p>1) Error because of missing resources. Simple to fix, just needed to copy them onto the classpath.</p>
<p>2) Changed functionality where test not updated. Often I had changed the underlying code (e.g. formatting a value) and not updated the tests. This is mostly caused by not running ALL the tests every time a change is made (see kent becks&#8217; <a href="http://www.threeriversinstitute.org/blog/?page_id=4">JUnit Max</a></p>
<p>3) Incomprehensible failure &#8211; Ah. One of the tests had such a complex set-up that I just couldn&#8217;t remember what the hell it was supposed to be testing. Well, I could see what it was trying to test, but the scenario was so complex that it was hard to work out why it was failing. </p>
<p>Solution ? Rip it out. This might seem a little drastic. Infact in one case I simplified it greatly but in a couple of other places I simply removed the test.</p>
<p>My rationale for this is that a failing test that I cannot fix is providing NO VALUE. Infact it is probably providing NEGATIVE value and actually creating NOISE. </p>
<p>Besides, its always in version control if I REALLY want to get it back later.</p>
<p><b>Step Three &#8211; Remove compilation warnings</b></p>
<p>My project was written in Java 1.4 I was using &#8220;enum&#8221; all over the shop as a variable name, which is obviously not cool in 1.5. So I removed all the warnings. Again, Warnings are NOISE, adding accidental complexity to my codebase. A health warning here. I once worked on a codebase where development was done on a branch. <b>Going round removing all the warnings everywhere on a branch can seriously affect your merge</b> because there are heaps of small changes which can lead to conflicts. Although relatively safe in terms of refactoring the cost in terms of merge hell is probably not worth it.</p>
<p>I did not remove deprecations yet. Thats another step.</p>
<p><b>Step Four &#8211; Refactor the tests</b></p>
<p>This is the interesting one and the one that prompted this post. </p>
<p>When running the unit tests, I saw that a few of them were taking quite a long time. These tended to be of the kind which were testing random number generators. To do this, they were generating say 100,000 random numbers in a Gaussian distribution and then analysing the standard deviation of the results. </p>
<p>Also I noticed that some of the tests had many test methods all testing different functionality (for example I had a &#8220;Math&#8221; class which does heaps of things).</p>
<p>Firstly I created a new source folder for &#8220;Functional Tests&#8221; to put these longer running, more high level tests in. As I was adding the new Test Cases, I realised that what I was doing was re-organising the functional areas of the tests. For example I extracted a class called &#8220;GaussianRandomGeneratorTest&#8221; which used to be a single method on my uber-test case.</p>
<p>I realised that what I was doing was <i>Refactoring the Test Cases</i> to represent a new, structure, without refactoring any of the source. Interesting. This means that what I am doing is 100% safe, there is NO WAY I can introduce a bug into the code because I am not mutating it. And yet I am building up a new understanding and description of the code which can point the way towards how the code might look one day.</p>
<p>This is potentially a powerful technique. I realise that I have used the same technique on a previous legacy source base, without really conceptualising it. We extracted all the tests that worked for the area we wanted to change, and then re-structured some of them to make more sense. </p>
<p>In summary, refactor the tests! Its 100% safe and can give you the freedom to experiment with levels of abstraction and cohesion without risking any changes to the code.</p>
<p><b>Step Five &#8211; Remove unused code</b></p>
<p>This one is quite hard. But luckily in my case I had used deprecation to mark classes I no longer needed. So this stage will involve removing all deprecated classes. Note that I have the luxury to do this as no-one depends on my project. At a minimum I should make sure that nothing inside the codebase uses these deprecated classes.</p>
]]></content:encoded>
			<wfw:commentRss>http://jimbarritt.com/non-random/2009/06/10/using-tests-to-help-refactor-a-legacy-codebase/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8220;Object Calisthenics&#8221; : Jeff Bay &#8211; II</title>
		<link>http://jimbarritt.com/non-random/2009/03/07/object-calisthenics-jeff-bay-part-2/</link>
		<comments>http://jimbarritt.com/non-random/2009/03/07/object-calisthenics-jeff-bay-part-2/#comments</comments>
		<pubDate>Sat, 07 Mar 2009 09:44:06 +0000</pubDate>
		<dc:creator>Jim Barritt</dc:creator>
				<category><![CDATA[anecdotes]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[thoughtblog]]></category>

		<guid isPermaLink="false">http://jimbarritt.com/non-random/2009/03/07/object-calisthenics-jeff-bay-part-2/</guid>
		<description><![CDATA[Following my Previous post on this topic, I have been in contact with Jeff via email, and he mentioned that the version of the chapter which is on his website is a preview version and so the published one should be taken as the final version. Something I mentioned there is a rule which wasn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>Following my <a href="http://jimbarritt.com/non-random/2009/03/04/object-calisthenics-jeff-bay/">Previous post</a> on this topic, I have been in contact with <a h ref="http://www.xpteam.com/jeff/writings/">Jeff</a> via email, and he mentioned that the version of the chapter which is on his website is a preview version and so the published one should be taken as the final version. </p>
<p>Something I mentioned there is a rule which wasn&#8217;t included in the final version (it is called &#8220;3. No static methods other than factory methods&#8221;). Jeff had some interesting comments and it sparked some good discussion between my pair (Steph) and I and so I think its worth adding the result here. </p>
<p>As I indicated, I had been thinking a bit about statics recently and been trying to pin down what I think about them. I was thinking along the lines of &#8220;Don&#8217;t use statics when you really want an instance&#8221; (see below). Jeff made the observation that actually they use statics quite a bit under certain circumstances.</p>
<p>With Jeffs kind permission, I will try to summarise our discussion. </p>
<p><strong>Methods with no side effects</strong></p>
<blockquote><p>
we use static methods extensively. whenever we can have a method that doesn&#8217;t have any side effects, we mark it as static so that the ide can alert us to the fact that it is so.
</p></blockquote>
<p>I realised that this is something I also like, if we&#8217;ve just written a method and it has no interaction with the state of the object, making it static is a clear indicator at the compiler level that the method has no side effects. I would tend to just do this without even thinking about it, so it was interesting to call it out.</p>
<p><strong>Guard conditions / Encapsulation</strong></p>
<blockquote><p>
<code >bombUnless(isTrue, "the thing was supposed to be false!");</code>
</p></blockquote>
<p>Here, there is a piece of logic which belongs to this class, but doesn&#8217;t involve the state of the instance. Steph and I discussed this also. </p>
<blockquote><p>
<code>String params = url.params();</code><br />
<code>if (params == null || params.trim().equals("")) children.add(sentinal);</code>
</p></blockquote>
<p>Becomes :</p>
<blockquote><p>
<code>if (isEmpty(params)) children.add(sentinal);</code>
</p></blockquote>
<p>I had hinted at this in the previous post, in terms of it being a common pattern. One place it is frequent is in the area of testing and mocking, for example <a href='http://mockito.org'>Mockito</a> or <a href='http://www.junit.org'>JUnit</a> both make extensive use of statics which makes the code easier to read.</p>
<p>Jeff had this to say:</p>
<blockquote><p>
These kinds of minor improvements to code, getting rid of micro-duplication, have given me some of the best gains in my practice. Without static methods and static imports to help reduce the client&#8217;s overhead for the method call, it&#8217;s hard to make certain kind of improvements worthwhile.
</p></blockquote>
<p><strong>Don&#8217;t use statics where you can have an instance</strong></p>
<p>This was my original understanding of the rule &#8220;only use statics for factories&#8221;. I had been thinking that the intent of the rule was this, so for example:</p>
<pre name="code" class="java:nogutter:nocontrols">
public void talkToTheService() {
    Result result = SomeService.doSomethingForMe(myArgs);
}
</pre>
<p>instead of :</p>
<pre name="code" class="java:nogutter:nocontrols">
private SomeService someServiceInstance = new SomeService();

public void talkToTheService() {
    Result result = someServiceInstance.doSomethingForMe(myArgs);
}
</pre>
<p><strong>Summary</strong></p>
<p>I&#8217;m sure there is more to say on the subject, but I think some of these points are useful. As a general point, Jeff concluded our discussion with:</p>
<blockquote><p>
Static methods should be reserved for things that either touch multiple objects and have no identity beyond the functional cohesion (make it a static method to give the related steps a name) or things that represent generic operations on fundamental types (such as the isEmpty example).
</p></blockquote>
<p>Thanks Jeff!</p>
]]></content:encoded>
			<wfw:commentRss>http://jimbarritt.com/non-random/2009/03/07/object-calisthenics-jeff-bay-part-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>&#8220;Object Calisthenics&#8221; : Jeff Bay</title>
		<link>http://jimbarritt.com/non-random/2009/03/04/object-calisthenics-jeff-bay/</link>
		<comments>http://jimbarritt.com/non-random/2009/03/04/object-calisthenics-jeff-bay/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 12:04:56 +0000</pubDate>
		<dc:creator>Jim Barritt</dc:creator>
				<category><![CDATA[anecdotes]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[thoughtblog]]></category>

		<guid isPermaLink="false">http://jimbarritt.com/non-random/2009/03/04/object-calisthenics-jeff-bay/</guid>
		<description><![CDATA[I happened to notice the book &#8220;The ThoughtWorks Anthology&#8221; lying around the office. It contains quite a few interesting articles. One in particular resonated with me. In Chapter 6, Jeff Bay talks about a set of simple rules for writing OO code, which he suggests you try applying rigorously on a small project (say 1000 [...]]]></description>
			<content:encoded><![CDATA[<p>I happened to notice the book <a href-"http://www.pragprog.com/titles/twa/thoughtworks-anthology">&#8220;The ThoughtWorks Anthology&#8221;</a> lying around the office. It contains quite a few interesting articles. One in particular resonated with me. </p>
<p>In <strong>Chapter 6</strong>, <a href="http://www.xpteam.com/">Jeff Bay</a> talks about a set of simple rules for writing OO code, which he suggests you try applying rigorously on a small project (say 1000 lines of code). The article can be downloaded <a href="http://www.xpteam.com/jeff/writings/objectcalisthenics.rtf">here</a>.</p>
<blockquote><p>This is a hard exercise, especially because many of these rules are not universally applicable.   The fact is, sometimes classes are a little more than 50 lines. But there’s great value in thinking about what would have to happen to move those responsibilities into real, first-class-objects of their own.  It’s developing this type of thinking that’s the real value of the exercise.  So stretch the limits of what you imagine is possible, and see whether you start thinking about your code in a new way. </p></blockquote>
<p>What I liked about this article is that it cut to the core of something which I have been working at explaining over the last few months. I sometimes feel like a &#8220;purist&#8221; when discussing OO design. It is a common situation to find myself defending writing a small class, or encapsulating a String to make a small value object which only has a single attribute. Jeff explains in a great way a few principles and challenges the reader to try them out in a rigorous way, just to see how it works out. This is a great way to present it, its not saying &#8220;I know the right way and you must follow the rules&#8221;, its suggesting that you should give it a  chance and you might begin to see some rewards, or &#8220;Try it, you might like it&#8221;.</p>
<p>I also liked the fact that eight out of the ten rules were all about <strong>encapsulation</strong> something I have also been thinking a good deal about recently, prompted by <a href="http://jimbarritt.com/non-random/2008/05/31/encapsulation-what-is-it-good-for/">this experience</a>, even to the extent of getting it printed on a t-shirt (but that&#8217;s another story).</p>
<blockquote>
<p><emp>NOTE:</emp><br />
Since writing this post, I have had a discussion with Jeff via email, the results of which can be found <a href="http://jimbarritt.com/non-random/2009/03/07/object-calisthenics-jeff-bay-part-2/">here</a>, this paragraph is superseded.</p>
<p>Another one which isnt actually in the book, but that I have been getting into recently is &#8220;No static methods except as factories&#8221;, This one is particularly hard to follow when so many libraries use them and its just so convenient:)
</p></blockquote>
<p>He&#8217;s not suggesting that these rules are <em>always applicable</em> but I think its interesting to make the effort of taking it to an extreme and see where it leads you, it may open new possibilities when going back to everyday coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://jimbarritt.com/non-random/2009/03/04/object-calisthenics-jeff-bay/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Distilling the principles of software design</title>
		<link>http://jimbarritt.com/non-random/2009/01/03/distilling-the-principles-of-software-design/</link>
		<comments>http://jimbarritt.com/non-random/2009/01/03/distilling-the-principles-of-software-design/#comments</comments>
		<pubDate>Sat, 03 Jan 2009 17:05:15 +0000</pubDate>
		<dc:creator>Jim Barritt</dc:creator>
				<category><![CDATA[anecdotes]]></category>
		<category><![CDATA[design]]></category>

		<guid isPermaLink="false">http://jimbarritt.com/non-random/2009/01/03/distilling-the-principles-of-software-design/</guid>
		<description><![CDATA[How do I make sure I am reading enough, or have a good set of working principles to be able to apply to my software design / coding ? Most people come to this by experience. Experience and interaction with other software developers. Ideas (or memes) spread by word of mouth, more specifically the internet. [...]]]></description>
			<content:encoded><![CDATA[<p>How do I make sure I am reading enough, or have a good set of working principles to be able to apply to my software design / coding ?</p>
<p>Most people come to this by experience. Experience and interaction with other software developers. Ideas (or <a href='http://en.wiktionary.org/wiki/meme'>memes</a>) spread by word of mouth, more specifically the internet.</p>
<p>Something the scientific community often publishes are review papers. These take a particular field and summarise the findings, providing a massive list of references at the end. </p>
<p>These are extremely useful if you want to then work in that area. Because it has been peer reviewed, there is a certain amount of confidence that crucial works (at least until the date of publication) have been included. </p>
<p>We <i>almost</i> have this in software, but people tend to write books, or blogs about things. These tend to be written by people who have gained a reputation in the field for collating ideas, for example <a href='http://martinfowler.com/bliki'>Martin Fowler</a>. Maybe its the same thing, and you get a word of mouth gain in respect for these people.</p>
<p>Perhaps a more formal peer-reviewed process could be realised for software design principles. As a local alternative, I have created a page called <a href='http://jimbarritt.com/non-random/essential-reading/'>&#8216;Essential Reading&#8217;</a> where I am going to begin to collate such references. Works which I believe contain valuable principles, and which everyone should be aware. I&#8217;ll see if it is possible to get something like a review paper going.</p>
<p>I think the goal is that you should be able to communicate about any of these concepts amongst developers and they should be able to understand what your talking about. Its a bit like patterns, but with a wider context, they are <i>principles</i>.</p>
<p>I was inspired to write this after finding <a href='http://memeagora.blogspot.com/'>Neal Ford</a>&#8216;s book  <a href='http://oreilly.com/catalog/9780596519780/' The Productive Programmer</a> which is a book that I had thinking would be great to write. Neal has been working on it for a couple of years and I am looking forward to reading it. </p>
<p>Non-randomly enough, I just found <a href='http://www.joelonsoftware.com/articles/Unicode.html'>this</a> from <a href='http://www.joelonsoftware.com/'>Joel</a> which is all about Unicode, but is entitled &#8220;The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)&#8221; maybe what I&#8217;m looking for is a similar article about design principles. Infact, Joel has a list of articles by job role which he has written which is close to what I&#8217;m looking for. Also, he co-founded <a href='http://stackoverflow.com/'>Stack Overflow</a> which is a way forward. </p>
<p>I would also like to start &#8216;canigrokit.org&#8217; which would be a similar idea but allows you to post snippets of code and people can vote on wether they are readable or not. I have the domain name already.</p>
]]></content:encoded>
			<wfw:commentRss>http://jimbarritt.com/non-random/2009/01/03/distilling-the-principles-of-software-design/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why simple code is more cost effective</title>
		<link>http://jimbarritt.com/non-random/2008/12/21/why-simple-code-is-more-cost-effective/</link>
		<comments>http://jimbarritt.com/non-random/2008/12/21/why-simple-code-is-more-cost-effective/#comments</comments>
		<pubDate>Sun, 21 Dec 2008 11:27:03 +0000</pubDate>
		<dc:creator>Jim Barritt</dc:creator>
				<category><![CDATA[anecdotes]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://jimbarritt.com/non-random/2008/12/21/why-simple-code-is-more-cost-effective/</guid>
		<description><![CDATA[A while ago, I was performing a refactor in our codebase, pulling some functionality into a core domain object and then factoring it into some smaller, more focused objects. My main drivers for making this change were: 1) Law of demeter 2) Hard to see all the paths of execution through the method 3) Nested [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago, I was performing a refactor in our codebase, pulling some functionality into a core domain object and then factoring it into some smaller, more focused objects.</p>
<p>My main drivers for making this change were:</p>
<p>1) <a href='http://en.wikipedia.org/wiki/Law_of_Demeter'>Law of demeter</a></p>
<p>2) Hard to see all the paths of execution through the method</p>
<p>3) Nested <code>if { ... }</code> statements</p>
<p>Thinking about 2), a method which has an execution path which is difficult to understand immediately and intuitively (for normal beings that is) is likely to hide a bug. Infact this particular piece of code was doing just that which is why I was changing it. </p>
<p>So more complex logic leads to buggy code which leads to a higher cost of maintennance. The latter has a direct impact on the bottom line of your project, not to mention potential success of the software in its usability or even core function.</p>
<p>The refactor followed three steps.</p>
<p>1) <a href='http://www.refactoring.com/catalog/moveMethod.html'>Move the method</a> into the Target class. This involved extracting a parameter which was really the knowledge of the client of Traget. I was a little concerned about Target knowing too much which leads to 2). I did this first by moving the method and then running all tests for client class, then I duplicated the test methods on the Target class and ran them (required a small refactor which made them easier to read as a side effect)</p>
<p>2) As i was doing this move, i realised that a scenario had not been tested, a consequence of 2) above, i.e. the path of execution was complex and so a test had been missed (if TDD had been followed this would be unlikely)</p>
<p>3) Finally, I wanted to get rid of the <code>if</code> statements. In order to do this, I used <b><a href='http://www.refactoring.com/catalog/replaceMethodWithMethodObject.html'>Replace method with method object</a></b> to pull out the logic inside the if blocks.</p>
<p><b>Summary :</b></p>
<p>By decomposing the flow of logic into smaller more focused units, it makes the individual parts easier to understand and so it is more obvious if an error in the flow exists. It also helps to write test cases in a more modular way. Rather than 20 test methods in a test case you break into several test cases covering specific functionality.</p>
<p>One question arises, is refactor 3) a refactor too far ?</p>
<p>How do I decide this ?</p>
<p>The best way is to get someone else to look at the code and see if it reads well to them. If i was writing a book, I would get someone to proof read it so that I could confirm that my language was expressed in a clear way. This is the kind of review which <a href='http://en.wikipedia.org/wiki/Pair_programming'>Pair programming</a> provides as standard, but even then it might be useful to show it to people outside the pair as you can both become entangled.</p>
]]></content:encoded>
			<wfw:commentRss>http://jimbarritt.com/non-random/2008/12/21/why-simple-code-is-more-cost-effective/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>When to stop refactoring (Stop, Look and Listen before you cross the road)</title>
		<link>http://jimbarritt.com/non-random/2008/12/21/when-to-stop-refactoring-stop-look-and-listen-before-you-cross-the-road/</link>
		<comments>http://jimbarritt.com/non-random/2008/12/21/when-to-stop-refactoring-stop-look-and-listen-before-you-cross-the-road/#comments</comments>
		<pubDate>Sun, 21 Dec 2008 11:19:04 +0000</pubDate>
		<dc:creator>Jim Barritt</dc:creator>
				<category><![CDATA[anecdotes]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://jimbarritt.com/non-random/2008/12/21/when-to-stop-refactoring-stop-look-and-listen-before-you-cross-the-road/</guid>
		<description><![CDATA[I have been working with Clive on a fair amount of refactoring the last couple of weeks. The other day we encountered a classic case of refactoring choice and narrowly averted a potentially lengthy (and confusing to future developers) refactor. This was due to my pairs&#8217; breaking the spell of the refactor path we were [...]]]></description>
			<content:encoded><![CDATA[<p>I have been working with Clive on a fair amount of refactoring the last couple of weeks.</p>
<p>The other day we encountered a classic case of refactoring choice and narrowly averted a potentially lengthy (and confusing to future developers) refactor. This was due to my pairs&#8217; breaking the spell of the refactor path we were following. </p>
<p>Our problem was a circular dependancy. We were introducing a new service to the system to replace a heap of code sprinkled throughout the codebase with a central point of access. We had implemented our shiny new service and were moving around the codebase getting things to use it instead of doing all the work for themselves. </p>
<p>We reached a class and attempted to add a reference to our new module (we are working in C#). Doh! visual studio complains at us &#8220;you cant add that reference because you already depend on this module&#8221;. Apparently there are ways to circumvent this but I am quite a stickler for dependency management (think it was hammered in to me learning C++), and anyway who wants to mess with visual studio when its angry?</p>
<p><strong>Solution 1</strong></p>
<p>Ok, so lets <i>move</i> our dependancy into a module which is higher up the dependancy tree, problem solved. Well, yes, except that once we moved one, we discover a couple more. Plus we are working on a branch and in a codebase without too much of a safety net (ok lets not get into why we would be doing that), so we dont want to be changing the world.</p>
<p>Our Solution was move the class up into a new module, but leave the old one there, just delegating to the new implementation. Not ideal, but at least it leaves the code in an unambigous state (i.e. there are not 2 classes doing the same thing, infact with the <strong>same</strong> code.</p>
<p>We had reached a point where we were beginning to feel uncomfotable with this. It was feeling heavyweight and intrusive, and worse, felt like we were creating technical debt. this situation would really need to be tidied up later. </p>
<p>We broke for lunch, in the hope that we may have some inspiration with food. After lunch, I returned to find Clive browsing the codebase:</p>
<p>&#8220;Hey Clive, did you get any genius ideas over lunch ?&#8221;</p>
<p>&#8220;Well,  I was just looking through the code, and i was wondering, what was it we were trying to achieve again ?&#8221;</p>
<p>It was like smelling salts:</p>
<p>&#8220;Of course, that&#8217;s great problem solving skills, we&#8217;ve been going about this all the wrong way!&#8221;</p>
<p>The answer was obvious. Instead of moving half the code all over the place because of this one problem child, we had to change that child to be less greedy for responsibility. It was doing too much. </p>
<p>All we had to do was stop it getting its own resources, and instead, pass them in, injecting through the constructor. This pushed the responsibility for obtaining the resource to the client. </p>
<p>As it happened, the client was also in the common module, but was a couple of static methods. A simple move method refactor moved these methods down into a module which we didnt depend upon and hey presto our dependancy circle was broken.</p>
<p>I think this is a great example of how you can get sucked in to a solution. To begin with we found it hard to see another way out of the problem. Simply stopping, looking at the problem form the beginning again, and listening to the code which was telling us not to cross the road, we were able to walk up the street for a few metres and find a safe zebra crossing.</p>
]]></content:encoded>
			<wfw:commentRss>http://jimbarritt.com/non-random/2008/12/21/when-to-stop-refactoring-stop-look-and-listen-before-you-cross-the-road/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What does &#8220;Refactor&#8221; mean ?</title>
		<link>http://jimbarritt.com/non-random/2008/12/21/what-does-refactor-mean/</link>
		<comments>http://jimbarritt.com/non-random/2008/12/21/what-does-refactor-mean/#comments</comments>
		<pubDate>Sun, 21 Dec 2008 11:16:59 +0000</pubDate>
		<dc:creator>Jim Barritt</dc:creator>
				<category><![CDATA[anecdotes]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[thoughtblog]]></category>

		<guid isPermaLink="false">http://jimbarritt.com/non-random/2008/12/21/what-does-refactor-mean/</guid>
		<description><![CDATA[I was reading Kent Becks&#8217; Smalltalk Patterns1 and he mentioned quite early on that it is important to factor the design correctly in terms of small, cohesive classes. It wasn&#8217;t until I read this that I had made the connection between &#8220;Refactor&#8221; and &#8220;Factor&#8221; and hence the mathematical definition of a factor. Suddenly I thought [...]]]></description>
			<content:encoded><![CDATA[<p>I was reading Kent Becks&#8217; <a href="http://www.amazon.co.uk/Smalltalk-Best-Practice-Patterns-Kent/dp/013476904X">Smalltalk Patterns</a><sup>1</sup> and he mentioned quite early on that it is important to <strong>factor</strong> the design correctly in terms of small, cohesive classes. It wasn&#8217;t until I read this that I had made the connection between &#8220;Refactor&#8221; and &#8220;Factor&#8221; and hence the mathematical definition of a factor. </p>
<p>Suddenly I thought I saw what he was talking about. <strong>Code</strong> can be broken down into <strong>factors</strong> which is when it becomes more flexible or <strong>supple</strong> (in the language of domain driven design).</p>
<p>In mathematical terms, a factor is a number which can be combined with another number to produce a given result. So the factors of <code>10</code> are <code>{1, 2, 5 and 10}</code>. Those of <code>12</code> are <code>{1, 2, 3, 4, 6 and 12}</code>.</p>
<p>Let&#8217;s say you represent a piece of code as say <code>12</code>, perhaps it is one big class doing several things. Ok, you can have one big class, maybe its easy to find everything there because its all in one place. However, you could also break it into two classes. Perhaps one is equivalent to say <code>3</code> and the other is <code>4</code>. So <code>3 * 4</code> still equals <code>12</code> right?</p>
<p>What happens if you factor the <code>4</code> into <code>{2, 2}</code>. Now you have <code>{2, 2, 3, 4}</code> You can now make quite a few different &#8220;products&#8221; of these factors, not <strong>just</strong> <code>12</code>, e.g. <code>2 * 3 = 6</code>. Further, you can make combinations of these intermediate products aswell, e.g. <code>2 * 2 * 4 = 16</code></p>
<p>I thought this was an interesting way to look at factoring (or re-factoring), and possibly a useful analogy to explain it. I had not heard it being talked about in this way before.</p>
<p><sup>1</sup> Kindly lent to me by <a href='http://www.thekua.com/atwork/'>Pat</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jimbarritt.com/non-random/2008/12/21/what-does-refactor-mean/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Initialisation inline or in Constructor ?</title>
		<link>http://jimbarritt.com/non-random/2008/12/03/initialisation-inline-or-in-constructor/</link>
		<comments>http://jimbarritt.com/non-random/2008/12/03/initialisation-inline-or-in-constructor/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 12:00:31 +0000</pubDate>
		<dc:creator>Jim Barritt</dc:creator>
				<category><![CDATA[anecdotes]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://jimbarritt.com/non-random/2008/12/03/initialisation-inline-or-in-constructor/</guid>
		<description><![CDATA[private Map&#60;String, String&#62; nodeValueMap = new HashMap&#60;String, String&#62;(); private Map&#60;String, NodeHandler&#62; nodeHandlers = new HashMap&#60;String, NodeHandler&#62;(); Or public MyClass() { this.nodeValueMap = new HashMap&#60;String, String&#62;(); this.nodeHandlers = new HashMap&#60;String, NodeHandler&#62;(); } Well, I think I might have a rule of thumb&#8230; If you don&#8217;t actually have a constructor, then you should use inline instantiation. As [...]]]></description>
			<content:encoded><![CDATA[<pre name="code" class="java:nogutter:nocontrols">
private Map&lt;String, String&gt; nodeValueMap = new HashMap&lt;String, String&gt;();
private Map&lt;String, NodeHandler&gt; nodeHandlers = new HashMap&lt;String, NodeHandler&gt;();
</pre>
<p>Or</p>
<pre name="code" class="java:nogutter:nocontrols">
public MyClass() {
    this.nodeValueMap = new HashMap&lt;String, String&gt;();
    this.nodeHandlers = new HashMap&lt;String, NodeHandler&gt;();
}
</pre>
<p>Well, I think I might have a rule of thumb&#8230; If you don&#8217;t actually have a constructor, then you should use inline instantiation. As soon as you need to do some more complex logic in the constructor (in the case above I might want to initialise the map with some objects) then move <strong>all</strong> the initialisers into the constructor ? </p>
<p>i.e. don&#8217;t have multiple patterns going on.</p>
<p>As an aside, a quick discussion with Alistair (of <a href='http://www.howbigismypotato.com/'>how big is my potato</a> fame) reminded me of the fact that you can actually do an instance initialisation like:</p>
<pre name="code" class ="java:nogutter:nocontrols">
{
    this.nodeValueMap = new HashMap&lt;String, String&gt;();
    this.nodeHandlers = new HashMap&lt;String, NodeHandler&gt;();
}
</pre>
<p>Although what the case for doing so is im not sure, might be nice if you are making a STRUCT kind of object.</p>
<p>Also I didn&#8217;t realise that you can declare a class inside a method:</p>
<pre name="code" class ="java:nogutter:nocontrols">
public void doSomething() {
    public class InnerClasss{
        public void sayHello() { System.out.println("hello");}
    }

    InnerClass inst = new InnerClass();
    inst.sayHello();
}
</pre>
<p>Alistair had used this for testing some reflection code, when he needed some test classes to work with. </p>
]]></content:encoded>
			<wfw:commentRss>http://jimbarritt.com/non-random/2008/12/03/initialisation-inline-or-in-constructor/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Referential transparency</title>
		<link>http://jimbarritt.com/non-random/2008/10/01/referential-transparency/</link>
		<comments>http://jimbarritt.com/non-random/2008/10/01/referential-transparency/#comments</comments>
		<pubDate>Wed, 01 Oct 2008 14:11:18 +0000</pubDate>
		<dc:creator>Jim Barritt</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[design]]></category>

		<guid isPermaLink="false">http://jimbarritt.com/non-random/2008/10/01/referential-transparency/</guid>
		<description><![CDATA[Just learned this term. from wikkipedia basically if a function can be evaluated and then replaced by that value in the algorithm, it is referentially transparent e.g. the function plusOne(int x) { return x + 1; } is referentially transparent as it will always return the same value for each value of x. e.g. the [...]]]></description>
			<content:encoded><![CDATA[<p>Just learned this term.</p>
<p>from <a href='http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)'>wikkipedia</a></p>
<p>basically if a function can be evaluated and then replaced by that value in the algorithm, it is <i>referentially transparent</i></p>
<p>e.g. the function <code>plusOne(int x) { return x + 1; }</code> is referentially transparent as it will always return the  same value for each value of x.</p>
<p>e.g. the function <code>today()</code> is not transparent because if you replaced it on one day with its value of &#8220;25th July 2008&#8243; and then ran the program on a different day the result of the progam would be different.</p>
<p>easy eh?</p>
]]></content:encoded>
			<wfw:commentRss>http://jimbarritt.com/non-random/2008/10/01/referential-transparency/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
