<?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>sellmic.com &#187; Code Bloopers</title>
	<atom:link href="http://sellmic.com/blog/category/code-bloopers/feed/" rel="self" type="application/rss+xml" />
	<link>http://sellmic.com/blog</link>
	<description>Augusto's corner of art, code and fun</description>
	<lastBuildDate>Fri, 14 Aug 2009 01:07:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The &#8220;constructor that doesn&#8217;t construct&#8221; blooper of the day</title>
		<link>http://sellmic.com/blog/2008/02/15/the-constructor-that-doesnt-construct-blooper-of-the-day/</link>
		<comments>http://sellmic.com/blog/2008/02/15/the-constructor-that-doesnt-construct-blooper-of-the-day/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 09:46:50 +0000</pubDate>
		<dc:creator>Augusto</dc:creator>
				<category><![CDATA[Code Bloopers]]></category>

		<guid isPermaLink="false">http://sellmic.com/blog/2008/02/15/the-constructor-that-doesnt-construct-blooper-of-the-day/</guid>
		<description><![CDATA[
Two people sent me this code blooper the other day, again, class and parameter names have been changed to protect the identity of the offender(s).

public SomeObject(
boolean lowSetting,
boolean highSetting,
boolean currentSetting,
Color lowColor,
Color highColor,
Color currentColor,
Color otherColor)
{
   super();
   lowSetting     = this.lowSetting;
   highSetting    = this.highSetting;
   [...]]]></description>
			<content:encoded><![CDATA[<p align="center"><img src="http://sellmic.com/blog/wp-content/uploads/2008/02/dead_fish1.jpg" alt="DeadFish2" /></p>
<p>Two people sent me this code blooper the other day, again, class and parameter names have been changed to protect the identity of the offender(s).</p>
<pre name="code" class="java">
public SomeObject(
boolean lowSetting,
boolean highSetting,
boolean currentSetting,
Color lowColor,
Color highColor,
Color currentColor,
Color otherColor)
{
   super();
   lowSetting     = this.lowSetting;
   highSetting    = this.highSetting;
   currentSetting= this.currentSetting;
   lowColor        = this.lowColor;
   highColor       = this.highColor;
   currentColor   = this.currentColor;
   otherColor      = this.otherColor;
}</pre>
<p>I have to admit I didn&#8217;t get it in the first couple of second, but it should be clear that this constructor is basically not doing anything. It basically overrides the constructor parameters with the current values in the object, which is more than likely not what we want to do.</p>
<p>BTW it also has a pet peeve of mine, it calls &#8220;super()&#8221; (the parent&#8217;s public no arg constructor). A lot of people don&#8217;t know that this is done automatically for you, no need to write code to for it.</p>
<p>Anyways, this one could have been prevented if you had a policy saying that method parameters should be named differently from class members. However, I personally like naming them the same. I guess it shouldn&#8217;t be unreasonable for the compiler to give you a warning when you do this, I&#8217;m going to guess most of the time you don&#8217;t want to do this.</p>
]]></content:encoded>
			<wfw:commentRss>http://sellmic.com/blog/2008/02/15/the-constructor-that-doesnt-construct-blooper-of-the-day/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The &#8220;I can&#8217;t find the object I put in the map&#8221; code blooper of the day</title>
		<link>http://sellmic.com/blog/2007/10/17/the-i-cant-find-the-object-i-put-in-the-map-code-blooper-of-the-day/</link>
		<comments>http://sellmic.com/blog/2007/10/17/the-i-cant-find-the-object-i-put-in-the-map-code-blooper-of-the-day/#comments</comments>
		<pubDate>Wed, 17 Oct 2007 21:25:29 +0000</pubDate>
		<dc:creator>Augusto</dc:creator>
				<category><![CDATA[Code Bloopers]]></category>

		<guid isPermaLink="false">http://sellmic.com/blog/2007/10/17/the-i-cant-find-the-object-i-put-in-the-map-code-blooper-of-the-day/</guid>
		<description><![CDATA[
This one is fairly easy to catch, but it was a self inflicted code blooper that had me thinking for a bit.

public class Key implements java.io.Serializable
{
  String key;

  public Key(String key)
  {
    this.key = key;
  }

  @Override
  public String toString()
  {
    return [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center" align="center"><img src="http://sellmic.com/blog/wp-content/uploads/2007/01/dead_fish1.jpg" alt="dead_fish1.jpg" /></p>
<p>This one is fairly easy to catch, but it was a self inflicted code blooper that had me thinking for a bit.</p>
<pre name="code" class="java">
public class Key implements java.io.Serializable
{
  String key;

  public Key(String key)
  {
    this.key = key;
  }

  @Override
  public String toString()
  {
    return key;
  }

  @Override
   public int hashCode()
   {
      super.hashCode();
   }

   @Override
   @SuppressWarnings("unchecked")
   public boolean equals(Object obj)
   {
       boolean retval = false;

       if (obj != null &amp;&amp; getClass() == obj.getClass())
       {
         Key other = (Key)obj;
         retval = other.getKey().equals(getKey());
       }

      return retval;
   }
}</pre>
<p>I used this class as a key into a Map. We would send this map over a serialized stream, and on the other side we couldn&#8217;t get the values by using the same keys (the keys are static finals/constants).</p>
<p>See if you can spot the error &#8230; it&#8217;s a bit obvious &#8230;</p>
<p>OK, you found it.</p>
<p>Yeah, it&#8217;s the <code>hashCode()</code>. You probably know about the <a href="http://www.ibm.com/developerworks/java/library/j-jtp05273.html">equals()/hashCode() rule when using Maps</a>.<br />
Don&#8217;t know how that &#8220;super&#8221; got in there, but that was the problem. Simple of course.  As a side note, I&#8217;ve been using Netbeans 6 and it has some interesting suggestions for genearting your equals and <code>hashCode()</code> methods. On the equals, I was a bit surprised to see it was using &#8220;<code>getClass() == obj.getClass()</code>&#8220;, I thought it would do &#8220;<code>getClass().equals(obj.getClass())</code>&#8220;, just because when I compare non primitive types I&#8217;m used to using the <code>equals()</code> method. Somehow this works, I guess because class definitions are loaded once? Intuitively I feel this is not the right way, but it works and seems to be accepted convention.</p>
<p>To be quite honest, I&#8217;ve been using &#8220;<code>instanceof</code>&#8221; for the longest time, and up until recently I didn&#8217;t know there was a <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=4744">debate </a>about that one.</p>
]]></content:encoded>
			<wfw:commentRss>http://sellmic.com/blog/2007/10/17/the-i-cant-find-the-object-i-put-in-the-map-code-blooper-of-the-day/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The sad case of the Java object that couldn&#8217;t be instantiated in Java</title>
		<link>http://sellmic.com/blog/2007/05/04/the-sad-case-of-the-java-object-that-couldnt-be-instantiated-in-java/</link>
		<comments>http://sellmic.com/blog/2007/05/04/the-sad-case-of-the-java-object-that-couldnt-be-instantiated-in-java/#comments</comments>
		<pubDate>Fri, 04 May 2007 05:17:22 +0000</pubDate>
		<dc:creator>Augusto</dc:creator>
				<category><![CDATA[Code Bloopers]]></category>

		<guid isPermaLink="false">http://sellmic.com/blog/2007/05/04/the-sad-case-of-the-java-object-that-couldnt-be-instantiated-in-java/</guid>
		<description><![CDATA[
Today&#8217;s code blooper involves a Java object that cannot be instantiated in Java.
 I was helping somebody unit test some code and we kept getting a null pointer exception on the constructor. First of all, the object in question had overly complicated constructors. Imagine something like this.
public FruitBasket(){
     // Does nothing [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center" align="center"><img src="http://sellmic.com/blog/wp-content/uploads/2007/01/dead_fish1.jpg" alt="dead_fish1.jpg" /></p>
<p align="left">Today&#8217;s code blooper involves a Java object that cannot be instantiated in Java.</p>
<p align="left"> I was helping somebody unit test some code and we kept getting a null pointer exception on the constructor. First of all, the object in question had overly complicated constructors. Imagine something like this.</p>
<p align="left"><code>public FruitBasket(){</code></p>
<p align="left"> <code>    // Does nothing important</code><br />
<code>}</code></p>
<p align="left"><code> public FruitBasket(String ownerName,  Fruit[] fruits,  Color color,  String brand, Date purchaseDate, boolean hasLid, boolean hasHandles, boolean hasLock, int maxFruits, int width, int height) {</code></p>
<p align="left"><code>    // Copies all values, and copies array to a local array (does not keep reference to one in parameters)</code></p>
<p align="left">}</p>
<p align="left"> Now imagine that constructor has 12-14 parameters, and 1 more constructor. It&#8217;s begging for some factory, or heck, just using an empty constructor and rely on &#8220;set&#8221; methods. Anyways, the thing was blowing up in the constructor and it was hard to see why. It was when it was copying the array to the array in the object. After a bit of debugging I noticed that the array was never initialized, so it was trying to reference a null array. Doh! OK, but this code runs in a real product, how could this work at all?</p>
<p align="left">The answer? <strong>JNI</strong>. The object in question usually gets created by a JNI program. The array is created in the JNI code, which has full access to the Java private fields, so it&#8217;s not a problem. The irony is that without fixing this class, it can never be created in Java. I thought that was  a bit ironic, but what this really shows is that this class is lacking basic unit testing in the first place. During integration test, you won&#8217;t see these problems because every bit of the application is doing its job. But when you apply unit tests, you tend to catch things like &#8230; not being able to instantiate the class!</p>
<p align="left"> The bug is a common mistake, but the biggest problems are having multiple constructors share zero code, long constructors that are only large because people keep adding features, and zero unit testing for core classes that are used all over the place.</p>
]]></content:encoded>
			<wfw:commentRss>http://sellmic.com/blog/2007/05/04/the-sad-case-of-the-java-object-that-couldnt-be-instantiated-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8216;this&#8217; can&#8217;t be null</title>
		<link>http://sellmic.com/blog/2007/01/19/if-this-null-systemoutprintlnits-the-end-of-the-world/</link>
		<comments>http://sellmic.com/blog/2007/01/19/if-this-null-systemoutprintlnits-the-end-of-the-world/#comments</comments>
		<pubDate>Fri, 19 Jan 2007 20:36:52 +0000</pubDate>
		<dc:creator>Augusto</dc:creator>
				<category><![CDATA[Code Bloopers]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://sellmic.com/blog/software-development/if-this-null-systemoutprintlnits-the-end-of-the-world</guid>
		<description><![CDATA[
&#160;
This is the first entry on a category I&#8217;d like to name &#8220;Code bloopers&#8221;.  I recently got a message about a piece of code that was throwing a null pointer, it was basically of this form:
synchronized(this.foobar)
This is an easy one. If you think it&#8217;s because &#8220;this&#8221; is null, you have to go back to [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center"><img src="http://sellmic.com/blog/wp-content/uploads/2007/01/dead_fish1.jpg" id="image12" alt="dead_fish1.jpg" /></p>
<p style="text-align: center">&nbsp;</p>
<p>This is the first entry on a category I&#8217;d like to name &#8220;Code bloopers&#8221;.  I recently got a message about a piece of code that was throwing a null pointer, it was basically of this form:</p>
<p><code><strong>synchronized(this.foobar)</strong></code></p>
<p>This is an easy one. If you think it&#8217;s because <em>&#8220;<strong>this</strong>&#8221; </em>is <em>null</em>, you have to go back to programming 101 and don&#8217;t collect $200 if you pass Go. I don&#8217;t want to sound mean, but yes, this is one of those bloopers that if you get it wrong, I hope it&#8217;s because you are half asleep or on drugs.</p>
<p>Remember kids, <em>this </em>can&#8217;t be <em>null</em>.  In case you have been a victim of this code blooper, don&#8217;t feel bad, you are not the only one confused by <a href="http://forum.java.sun.com/thread.jspa?threadID=246134&amp;messageID=904350" target="_blank" title="Discussion on java forum">this</a>. It&#8217;s the only case (that I could find in the internet) of somebody else thinking <em>this </em>can be <em>null</em>, and I hope that gives you some comfort.</p>
]]></content:encoded>
			<wfw:commentRss>http://sellmic.com/blog/2007/01/19/if-this-null-systemoutprintlnits-the-end-of-the-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
