<?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>Transient Technology &#187; ide</title>
	<atom:link href="http://martinaharris.com/tag/ide/feed/" rel="self" type="application/rss+xml" />
	<link>http://martinaharris.com</link>
	<description>Next time you look it might be gone</description>
	<lastBuildDate>Tue, 24 Jan 2012 18:14:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Leveraging Eclipse for TDD</title>
		<link>http://martinaharris.com/2010/02/leveraging-eclipse-for-tdd/</link>
		<comments>http://martinaharris.com/2010/02/leveraging-eclipse-for-tdd/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 05:00:03 +0000</pubDate>
		<dc:creator>Martin Harris</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[ide]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[re-factor]]></category>

		<guid isPermaLink="false">http://martinaharris.com/?p=722</guid>
		<description><![CDATA[I have found that quite a few programmers struggle to work in a completely test driven fashion. I myself whilst writing tests for many years, took a long time to move over to writing the test first. I think one &#8230; <a href="http://martinaharris.com/2010/02/leveraging-eclipse-for-tdd/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I have found that quite a few programmers struggle to work in a completely test driven fashion.  I myself whilst writing tests for many years, took a long time to move over to writing the test first.  I think one of my problems was that I always wanted to do my thinking in the implementation class.  Recently I have found a great way to leverage the Eclipse IDE&#8217;s code complete functions so that its more natural to work from the test class.</p>
<p>The idea is to save effort and time by getting Eclipse to create the implementation side.  I have long been used to changing the code, and then using code complete to refactor.  For instance you can change a class name or package name, and use CTRL-1 to get a list of options on the code that will not compile.  Then use an option to move or rename the class performing the re-factor.</p>
<p>This turns out to be a very powerful technique for driving development from the test.</p>
<p>Try out this simple example I think it makes it more natural to TDD, but I would be interested in your comments.<br />
<span id="more-722"></span><br />
Start by creating a test class the usual way, and put your test method in it.</p>
<pre class="brush: java; light: true">@Test
public void testDrivingFromTheIde () {

}</pre>
<p>Next I need a new service type, and an implementation.  So in the test just type what you think you will need.</p>
<pre class="brush: java; light: true">public void testDrivingFromTheIde () {

long factor;
NumberCruncher numberCruncher = new ComplexNumberCruncher(factor);

}</pre>
<p>The new code does not compile as its missing the classes, so driving from the test, press your code complete key, CTRL+1 in eclipse.  Up pops a list of operations like this:</p>
<div id="attachment_727" class="wp-caption alignleft" style="width: 160px"><a href="http://martinaharris.com/wp-content/uploads/2010/01/1first-interface.jpg"><img class="size-thumbnail wp-image-727" title="1first-interface" src="http://martinaharris.com/wp-content/uploads/2010/01/1first-interface-150x150.jpg" alt="Code complete options showing create class and interface" width="150" height="150" /></a><p class="wp-caption-text">Code complete options showing create class and interface</p></div>
<p>Select create new interface, remember to change the location from test to main or the class will be created in the wrong folder.  Note if your using netbeans you don&#8217;t get as rich an editing experience, it only gives you the option to create a class, and in the wrong folder, so you have to move it and change it to an interface.  This undoes what I am trying to achieve, which is to spend as much time in the test class as possible.</p>
<div id="attachment_728" class="wp-caption alignright" style="width: 160px"><a href="http://martinaharris.com/wp-content/uploads/2010/01/2first-interface.jpg"><img class="size-thumbnail wp-image-728" title="2first-interface" src="http://martinaharris.com/wp-content/uploads/2010/01/2first-interface-150x150.jpg" alt="Creating an interface" width="150" height="150" /></a><p class="wp-caption-text">Creating an interface</p></div>
<p>Back in the ide you will be looking at your new class, hit ALT-left to return to the test class.  Note its still not compiling so again, as an alternative this time just hover over the problem and select create new constructor from the options.</p>
<p>Finally back in the class you can fix the remaining problem and initialize the variable.  Now add the call under test to the code, and again use code complete to add the method to the interface.  Navigate to the ComplexNumberCruncher class and a quick CTRL+1 will give you the option to add unimplemented methods from the interface.  Save and move back to the test again.  Add your missing assertions and run the test, it should fail.</p>
<p>At this point you have written a few lines of test, and managed to generate the implementation skeleton.  All that remains is to finally implement the compute method.  This bit eclipse will not do for you.  <img src='http://martinaharris.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />   So switch back to your compute method, and implement it.  In a more complex implementation you may find yourself adding more assertions, and implementation incrementally.  This is pretty normal.  For me leveraging eclipse makes it much easier to write the code in a true TDD way.  Less typing and I am sure it takes less time.</p>
<pre class="brush: java; light: true">public void testDrivingFromTheIde () {

		long factor = 5;
		NumberCruncher numberCruncher = new ComplexNumberCruncher(factor);

		long value = 10;
		long result = numberCruncher.compute(value);

		assertNotNull(result);
		assertEquals("5 x 10 = 50",(factor*value),result);
}

public interface NumberCruncher {

		long compute(long i);
}

public class ComplexNumberCruncher implements NumberCruncher {

	private final long factor;

	public ComplexNumberCruncher(long factor) {
		this.factor = factor;
	}

	@Override
	public long compute(long value) {

		return (this.factor * value);
	}

}</pre>
<div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2010%2F02%2Fleveraging-eclipse-for-tdd%2F&amp;title=Leveraging+Eclipse+for+TDD" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2010%2F02%2Fleveraging-eclipse-for-tdd%2F&amp;title=Leveraging+Eclipse+for+TDD" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2010%2F02%2Fleveraging-eclipse-for-tdd%2F&amp;title=Leveraging+Eclipse+for+TDD" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fmartinaharris.com%2F2010%2F02%2Fleveraging-eclipse-for-tdd%2F&amp;headline=Leveraging+Eclipse+for+TDD" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=Leveraging+Eclipse+for+TDD&amp;url=http%3A%2F%2Fmartinaharris.com%2F2010%2F02%2Fleveraging-eclipse-for-tdd%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=Leveraging+Eclipse+for+TDD&amp;u=http%3A%2F%2Fmartinaharris.com%2F2010%2F02%2Fleveraging-eclipse-for-tdd%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=Leveraging+Eclipse+for+TDD&amp;url=http%3A%2F%2Fmartinaharris.com%2F2010%2F02%2Fleveraging-eclipse-for-tdd%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=Leveraging+Eclipse+for+TDD&amp;url=http%3A%2F%2Fmartinaharris.com%2F2010%2F02%2Fleveraging-eclipse-for-tdd%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=Leveraging+Eclipse+for+TDD&amp;url=http%3A%2F%2Fmartinaharris.com%2F2010%2F02%2Fleveraging-eclipse-for-tdd%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fmartinaharris.com%2F2010%2F02%2Fleveraging-eclipse-for-tdd%2F&amp;title=Leveraging+Eclipse+for+TDD&amp;summary=&amp;source=" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fmartinaharris.com%2F2010%2F02%2Fleveraging-eclipse-for-tdd%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fmartinaharris.com%2F2010%2F02%2Fleveraging-eclipse-for-tdd%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fmartinaharris.com%2F2010%2F02%2Fleveraging-eclipse-for-tdd%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div><div class="dzone_button" style="float: right; margin-left: 5px;">
<script type="text/javascript">
var dzone_url = 'http://martinaharris.com/2010/02/leveraging-eclipse-for-tdd/';
var dzone_title = 'Leveraging Eclipse for TDD';
var dzone_blurb = '';
var dzone_style = '2';
</script>
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
</div>]]></content:encoded>
			<wfw:commentRss>http://martinaharris.com/2010/02/leveraging-eclipse-for-tdd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Eclipse, Static imports quickly and GitHub gist.</title>
		<link>http://martinaharris.com/2010/01/eclipse-static-imports/</link>
		<comments>http://martinaharris.com/2010/01/eclipse-static-imports/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 07:45:03 +0000</pubDate>
		<dc:creator>Martin Harris</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[ide]]></category>
		<category><![CDATA[junit]]></category>

		<guid isPermaLink="false">http://martinaharris.com/?p=698</guid>
		<description><![CDATA[Every one has a collection of favorite Eclipse templates.  I find the simple ones are often the most useful.  The examples below create you a static import and finish the method your typing.  They even put the cursor in the &#8230; <a href="http://martinaharris.com/2010/01/eclipse-static-imports/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Every one has a collection of favorite Eclipse templates.  I find the simple ones are often the most useful.  The examples below create you a static import and finish the method your typing.  They even put the cursor in the right place if you need arguments in the method.  I am going to collect more of them using the incredibly handy GitHub gist: <a title="My Eclipse Templates" href="http://gist.github.com/279944" target="_blank">My Static Import Templates For Eclipse</a>.  GitHub gists are great for these kinds of programmer reminders.  In addition edits get version control.</p>
<p><strong>Typical Eclipse template for Google Collections</strong></p>
<pre class="brush: java; light: true">
${:importStatic(com.google.common.collect.Lists.newArrayList)}newArrayList();
${cursor}
${:importStatic(com.google.common.collect.Sets.newHashSet)}newHashSet();
${cursor}
</pre>
<p>To use them, just open Eclipse Preferences: Java Editor: Templates.  Add new templates, and give them a name similar to the method.  I.e newArrayList.  Then your code completion hot key will run the template completing the text your typing in.  i.e you type newArrayList [code complete key] and it adds the static, and the rest of the method, placing your cursor at the end of the line.  Neat!</p>
<p><strong>Note:</strong>  See the comment from Yuri below, the above templates are no longer required for static imports.  Its possible to configure the Content Assist to bring in a whole type, or member using Prefs:Java:Editor:Content Assist:Favorites.  This is better as it scans all possible available methods in the type.</p>
<p>The code you end up with is tidy too:</p>
<pre class="brush: java; light: true">List myList = newArrayList();</pre>
<p>There is another thing I can never remember the syntax for! <img src='http://martinaharris.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />   The markup to add the syntax highlight for this blog.  Available as a <a title="Google Syntax Highlight plugin for WPress as gist hint" href="http://gist.github.com/234709" target="_blank">gist: http://gist.github.com/234709</a></p>
<div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2010%2F01%2Feclipse-static-imports%2F&amp;title=Eclipse%2C+Static+imports+quickly+and+GitHub+gist." target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2010%2F01%2Feclipse-static-imports%2F&amp;title=Eclipse%2C+Static+imports+quickly+and+GitHub+gist." target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2010%2F01%2Feclipse-static-imports%2F&amp;title=Eclipse%2C+Static+imports+quickly+and+GitHub+gist." target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fmartinaharris.com%2F2010%2F01%2Feclipse-static-imports%2F&amp;headline=Eclipse%2C+Static+imports+quickly+and+GitHub+gist." target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=Eclipse%2C+Static+imports+quickly+and+GitHub+gist.&amp;url=http%3A%2F%2Fmartinaharris.com%2F2010%2F01%2Feclipse-static-imports%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=Eclipse%2C+Static+imports+quickly+and+GitHub+gist.&amp;u=http%3A%2F%2Fmartinaharris.com%2F2010%2F01%2Feclipse-static-imports%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=Eclipse%2C+Static+imports+quickly+and+GitHub+gist.&amp;url=http%3A%2F%2Fmartinaharris.com%2F2010%2F01%2Feclipse-static-imports%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=Eclipse%2C+Static+imports+quickly+and+GitHub+gist.&amp;url=http%3A%2F%2Fmartinaharris.com%2F2010%2F01%2Feclipse-static-imports%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=Eclipse%2C+Static+imports+quickly+and+GitHub+gist.&amp;url=http%3A%2F%2Fmartinaharris.com%2F2010%2F01%2Feclipse-static-imports%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fmartinaharris.com%2F2010%2F01%2Feclipse-static-imports%2F&amp;title=Eclipse%2C+Static+imports+quickly+and+GitHub+gist.&amp;summary=&amp;source=" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fmartinaharris.com%2F2010%2F01%2Feclipse-static-imports%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fmartinaharris.com%2F2010%2F01%2Feclipse-static-imports%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fmartinaharris.com%2F2010%2F01%2Feclipse-static-imports%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div><div class="dzone_button" style="float: right; margin-left: 5px;">
<script type="text/javascript">
var dzone_url = 'http://martinaharris.com/2010/01/eclipse-static-imports/';
var dzone_title = 'Eclipse, Static imports quickly and GitHub gist.';
var dzone_blurb = '';
var dzone_style = '2';
</script>
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
</div>]]></content:encoded>
			<wfw:commentRss>http://martinaharris.com/2010/01/eclipse-static-imports/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>IDE Generation of equals methods has its moments</title>
		<link>http://martinaharris.com/2009/10/ide-generation-of-equals-methods-has-its-moments/</link>
		<comments>http://martinaharris.com/2009/10/ide-generation-of-equals-methods-has-its-moments/#comments</comments>
		<pubDate>Sat, 24 Oct 2009 21:26:29 +0000</pubDate>
		<dc:creator>Martin Harris</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[software quality]]></category>
		<category><![CDATA[compareto]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[equals]]></category>
		<category><![CDATA[generation]]></category>
		<category><![CDATA[ide]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[quality]]></category>

		<guid isPermaLink="false">http://www.koitok.net/2009/10/ide-generation-of-equals-methods-has-its-moments/</guid>
		<description><![CDATA[This blog entry looks at the code generated by Eclipse and Netbeans and demonstrates why its a good idea to both test the code and review it thoroughly. <a href="http://martinaharris.com/2009/10/ide-generation-of-equals-methods-has-its-moments/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h3>Generating java bugs in equals methods using an IDE</h3>
<p>Its no revelation that Eclipse, Netbeans and others can generate code.  Its particularly useful if getters, setters or constructors are required.  Implementing an interface is very simple indeed as these IDE will stub out the interface methods.  More complex methods though require some thought.  Generation of the equals method is such a case.</p>
<p>How to write a good equals method is well documented by Joshua Bloch in <a href="http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1254412228&#038;sr=8-1" target="_blank">Effective Java, Chapter 3 by Joshua Bloch</a>.</p>
<h3>Eclipse V Netbeans who generates the best code?</h3>
<p>Before we get deeper into the code here are two efforts at generating the equals method for this class:</p>
<pre class="brush: java; gutter: false;">
public class SimpleBean  {
    private final BigDecimal bigDecimal;
    public SimpleBean(BigDecimal bigDecimal) {
        this.bigDecimal = bigDecimal;
    }
}
</pre>
<p><span id="more-19"></span></p>
<p>The results for even such a simple class are interesting.</p>
<p><strong>Netbeans</strong></p>
<pre class="brush: java; gutter: false;highlight: 6">
      @Override
      public boolean equals(Object obj) {
          if (obj == null) {
              return false;
          }
          if (getClass() != obj.getClass()) {
              return false;
          }
          final SimpleBean other = (SimpleBean) obj;
          if (this.bigDecimal != other.bigDecimal &#038;&#038;
                (this.bigDecimal == null ||
                !this.bigDecimal.equals(other.bigDecimal))) {
              return false;
          }
          return true;
     }
</pre>
<p>The Netbeans attempt is reasonable at first sight.  Even with the above there are a few observations and a bug which I will come to later on.  Can you spot it?</p>
<ul>
<li>The <strong>getClass</strong> system for checking that our object can be tested for equality is a little inflexible.  For classes that implement a common interface its nice to be able to compare across implementations.  This is often done using the <strong>if (!(obj instanceof MyType))</strong> check.  GetClass is optimal, but it would be good to have an option in the generation.</li>
<li>The style of the value check is a little compressed for some tastes.</li>
</ul>
<p><strong>Eclipse</strong></p>
<pre class="brush: java; gutter: false;highlight: 7">
  @Override
  public boolean equals(Object obj) {
      if (this == obj)
	  return true;
      if (obj == null)
	  return false;
      if (getClass() != obj.getClass())
          return false;
      SimpleBean other = (SimpleBean) obj;
      if (bigDecimal == null) {
        if (other.bigDecimal != null)
          return false;
       } else if (!bigDecimal.equals(other.bigDecimal))
          return false;
       return true;
}
</pre>
<p>Eclipse generates very similar code.  The bug is still there though, can you see it yet?</p>
<ul>
<li>Eclipse has extra options.  You can switch out that <strong>getClass</strong> for the more flexible <strong>instanceof</strong> check.</li>
<li>The style above is better and can be improved with the introduction of blocks on all if statements.</li>
<li>Eclipse does more optimization assuming that more often than not an object is passed in. </li>
</ul>
<h3>Neither are good! The generated equals bug exposed!</h3>
<p>It should be clear when I write a test that will fail for either of the above implementations.</p>
<pre class="brush: java; gutter: false; wrap-lines: false">
@Test
    public void testObjectInconsistantWithEquals_Equals() {
        SimpleBean testSimpleBean = new SimpleBean(new BigDecimal("0"));
        SimpleBean expectedEqualSimpleBean = new SimpleBean(new BigDecimal("0.0"));
        assertTrue("Equality Test Fail", testSimpleBean.equals(expectedEqualSimpleBean));
    }
}
</pre>
<p>The above test fails because BigDecimal has an equals implementation that will return false when it checks <strong>100</strong> against <strong>100.00</strong>.  Its implementation of the compare method is declared as being inconsistent with equals.  This in itself is interesting but for this example it means we can modify the generated code to provide a fix using <strong>this.bigDecimal.compareTo(other.bigDecimal) != 0</strong> instead of relying on the generated use of equals.</p>
<h3>Recommendations</h3>
<p>In light of the above I would suggest the following points.</p>
<ul>
<li>Apart from primitive and well known objects generated equals methods can be very bad indeed.</li>
<li>Always write a test for equals, do not rely on an IDE.</li>
<li>If you change your object, remember to re-visit the equals method.</li>
</ul>
<div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fide-generation-of-equals-methods-has-its-moments%2F&amp;title=IDE+Generation+of+equals+methods+has+its+moments" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fide-generation-of-equals-methods-has-its-moments%2F&amp;title=IDE+Generation+of+equals+methods+has+its+moments" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fide-generation-of-equals-methods-has-its-moments%2F&amp;title=IDE+Generation+of+equals+methods+has+its+moments" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fide-generation-of-equals-methods-has-its-moments%2F&amp;headline=IDE+Generation+of+equals+methods+has+its+moments" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=IDE+Generation+of+equals+methods+has+its+moments&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fide-generation-of-equals-methods-has-its-moments%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=IDE+Generation+of+equals+methods+has+its+moments&amp;u=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fide-generation-of-equals-methods-has-its-moments%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=IDE+Generation+of+equals+methods+has+its+moments&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fide-generation-of-equals-methods-has-its-moments%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=IDE+Generation+of+equals+methods+has+its+moments&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fide-generation-of-equals-methods-has-its-moments%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=IDE+Generation+of+equals+methods+has+its+moments&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fide-generation-of-equals-methods-has-its-moments%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fide-generation-of-equals-methods-has-its-moments%2F&amp;title=IDE+Generation+of+equals+methods+has+its+moments&amp;summary=&amp;source=" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fide-generation-of-equals-methods-has-its-moments%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fide-generation-of-equals-methods-has-its-moments%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fide-generation-of-equals-methods-has-its-moments%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div><div class="dzone_button" style="float: right; margin-left: 5px;">
<script type="text/javascript">
var dzone_url = 'http://martinaharris.com/2009/10/ide-generation-of-equals-methods-has-its-moments/';
var dzone_title = 'IDE Generation of equals methods has its moments';
var dzone_blurb = '';
var dzone_style = '2';
</script>
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
</div>]]></content:encoded>
			<wfw:commentRss>http://martinaharris.com/2009/10/ide-generation-of-equals-methods-has-its-moments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

