<?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; netbeans</title>
	<atom:link href="http://martinaharris.com/tag/netbeans/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>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>

