<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Let&#8217;s Fix Some Crap Code</title>
	<atom:link href="http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/</link>
	<description>Technology and Geek Stuff by Eric Burke</description>
	<lastBuildDate>Thu, 04 Nov 2010 22:37:00 -0400</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Eric Burke</title>
		<link>http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/comment-page-1/#comment-34</link>
		<dc:creator>Eric Burke</dc:creator>
		<pubDate>Sat, 15 Sep 2007 18:35:51 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/#comment-34</guid>
		<description>shevken: you won&#039;t need getters and setters for Hibernate. You can declare access=field, for instance. You will have a problem declaring the fields final, however. Not sure about other ORM tools, though.</description>
		<content:encoded><![CDATA[<p>shevken: you won&#8217;t need getters and setters for Hibernate. You can declare access=field, for instance. You will have a problem declaring the fields final, however. Not sure about other ORM tools, though.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shevken</title>
		<link>http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/comment-page-1/#comment-33</link>
		<dc:creator>shevken</dc:creator>
		<pubDate>Sat, 15 Sep 2007 12:32:50 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/#comment-33</guid>
		<description>We might need the getters/setters if the class is used in an ORM tool?</description>
		<content:encoded><![CDATA[<p>We might need the getters/setters if the class is used in an ORM tool?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: afsina</title>
		<link>http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/comment-page-1/#comment-31</link>
		<dc:creator>afsina</dc:creator>
		<pubDate>Fri, 14 Sep 2007 20:10:03 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/#comment-31</guid>
		<description>you may make the properties public in this case, since class is immutable and you already give access with getters.</description>
		<content:encoded><![CDATA[<p>you may make the properties public in this case, since class is immutable and you already give access with getters.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/comment-page-1/#comment-30</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Fri, 14 Sep 2007 17:11:21 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/#comment-30</guid>
		<description>See my (aging) discussion of Value Types in Java:
http://creativekarma.com/ee.php/weblog/comments/value_types_in_java/

In addition to all of the changes that you&#039;ve made, I would recommend:

1. &#039;final&#039; class -- already mentioned by Jesse and Lance
2. implements Serializable or perhaps Externalizable
3. implements Comparable -- probably not needed by this class
4. implements Cloneable -- probably not needed by this class
5. override toString() -- not absolutely needed but always nice
6. scrap the getters and make the fields &#039;public final&#039;.

If the class is something that might need to be extended by a decorator in the future, item 6 is changed to:
6. define the getters in a separate interface that is implemented by this class.</description>
		<content:encoded><![CDATA[<p>See my (aging) discussion of Value Types in Java:<br />
<a href="http://creativekarma.com/ee.php/weblog/comments/value_types_in_java/" rel="nofollow">http://creativekarma.com/ee.php/weblog/comments/value_types_in_java/</a></p>
<p>In addition to all of the changes that you&#8217;ve made, I would recommend:</p>
<p>1. &#8216;final&#8217; class &#8212; already mentioned by Jesse and Lance<br />
2. implements Serializable or perhaps Externalizable<br />
3. implements Comparable &#8212; probably not needed by this class<br />
4. implements Cloneable &#8212; probably not needed by this class<br />
5. override toString() &#8212; not absolutely needed but always nice<br />
6. scrap the getters and make the fields &#8216;public final&#8217;.</p>
<p>If the class is something that might need to be extended by a decorator in the future, item 6 is changed to:<br />
6. define the getters in a separate interface that is implemented by this class.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan Lewis</title>
		<link>http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/comment-page-1/#comment-29</link>
		<dc:creator>Dan Lewis</dc:creator>
		<pubDate>Fri, 14 Sep 2007 13:18:03 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/#comment-29</guid>
		<description>This looks nicer, but I noticed the Google version uses java.util.Arrays.hashCode(Object a[]):

public static int hashCode(Object a[]) {
    if (a == null)
        return 0;

    int result = 1;

    for (Object element : a)
        result = 31 * result + (element == null ? 0 : element.hashCode());

    return result;
}


Is this a better implementation than commons lang?


import com.google.common.base.Objects;

public class CustomerKey4 {
    private final int customerId;
    private final int districtId;

    public CustomerKey4(final int customerId, final int districtId) {
        this.customerId = customerId;
        this.districtId = districtId;
    }

    public int getCustomerId() {
        return customerId;
    }

    public int getDistrictId() {
        return districtId;
    }

    @Override
    public boolean equals(final Object obj) {
        if (obj instanceof CustomerKey4) {
            CustomerKey4 that = (CustomerKey4) obj;

            return Objects.equal(customerId, that.customerId)
                    &amp;&amp; Objects.equal(districtId, that.districtId);
        } else {
            return false;
        }
    }


    @Override
    public int hashCode() {
        return Objects.hashCode(customerId, districtId);
    }
}
</description>
		<content:encoded><![CDATA[<p>This looks nicer, but I noticed the Google version uses java.util.Arrays.hashCode(Object a[]):</p>
<p>public static int hashCode(Object a[]) {<br />
    if (a == null)<br />
        return 0;</p>
<p>    int result = 1;</p>
<p>    for (Object element : a)<br />
        result = 31 * result + (element == null ? 0 : element.hashCode());</p>
<p>    return result;<br />
}</p>
<p>Is this a better implementation than commons lang?</p>
<p>import com.google.common.base.Objects;</p>
<p>public class CustomerKey4 {<br />
    private final int customerId;<br />
    private final int districtId;</p>
<p>    public CustomerKey4(final int customerId, final int districtId) {<br />
        this.customerId = customerId;<br />
        this.districtId = districtId;<br />
    }</p>
<p>    public int getCustomerId() {<br />
        return customerId;<br />
    }</p>
<p>    public int getDistrictId() {<br />
        return districtId;<br />
    }</p>
<p>    @Override<br />
    public boolean equals(final Object obj) {<br />
        if (obj instanceof CustomerKey4) {<br />
            CustomerKey4 that = (CustomerKey4) obj;</p>
<p>            return Objects.equal(customerId, that.customerId)<br />
                    &amp;&amp; Objects.equal(districtId, that.districtId);<br />
        } else {<br />
            return false;<br />
        }<br />
    }</p>
<p>    @Override<br />
    public int hashCode() {<br />
        return Objects.hashCode(customerId, districtId);<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lance Finney</title>
		<link>http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/comment-page-1/#comment-28</link>
		<dc:creator>Lance Finney</dc:creator>
		<pubDate>Fri, 14 Sep 2007 13:15:34 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/#comment-28</guid>
		<description>I agree with Jesse, but I&#039;d go even further.  If you want the class to be immutable, it must be declared as final.  Otherwise, you could be passed an object that is a CustomerKey2 child that reimplements the getters and setters in a non-threadsafe way.</description>
		<content:encoded><![CDATA[<p>I agree with Jesse, but I&#8217;d go even further.  If you want the class to be immutable, it must be declared as final.  Otherwise, you could be passed an object that is a CustomerKey2 child that reimplements the getters and setters in a non-threadsafe way.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan Lewis</title>
		<link>http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/comment-page-1/#comment-27</link>
		<dc:creator>Dan Lewis</dc:creator>
		<pubDate>Fri, 14 Sep 2007 12:50:42 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/#comment-27</guid>
		<description>I will definitely check out the Google collections stuff.  Thanks for the tip!  FYI there is a port of commons collections to Java 5 that we are using with success: http://sourceforge.net/projects/collections.</description>
		<content:encoded><![CDATA[<p>I will definitely check out the Google collections stuff.  Thanks for the tip!  FYI there is a port of commons collections to Java 5 that we are using with success: <a href="http://sourceforge.net/projects/collections." rel="nofollow">http://sourceforge.net/projects/collections.</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Burke</title>
		<link>http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/comment-page-1/#comment-26</link>
		<dc:creator>Eric Burke</dc:creator>
		<pubDate>Fri, 14 Sep 2007 12:31:33 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/#comment-26</guid>
		<description>When I wrote my example last night I used &lt;a href=&quot;http://publicobject.com/2007/09/coding-in-small-with-google-collections_8175.html&quot; rel=&quot;nofollow&quot;&gt;Objects.equals and Objects.hashCode&lt;/a&gt;, from Google. The real-world example I based this on used hand-coded equals and hashCode methods with tons of null checking, it was ugly. I&#039;ve used commons-collections and commons-lang on many projects personally, but they are still pre-Java 5 syntax which has turned into a serious weakness.</description>
		<content:encoded><![CDATA[<p>When I wrote my example last night I used <a href="http://publicobject.com/2007/09/coding-in-small-with-google-collections_8175.html" rel="nofollow">Objects.equals and Objects.hashCode</a>, from Google. The real-world example I based this on used hand-coded equals and hashCode methods with tons of null checking, it was ugly. I&#8217;ve used commons-collections and commons-lang on many projects personally, but they are still pre-Java 5 syntax which has turned into a serious weakness.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan Lewis</title>
		<link>http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/comment-page-1/#comment-25</link>
		<dc:creator>Dan Lewis</dc:creator>
		<pubDate>Fri, 14 Sep 2007 12:23:22 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/#comment-25</guid>
		<description>I like it.  How would you do equals and hashCode?  Here&#039;s how I&#039;d do it:

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

public class CustomerKey3 {
    private final int customerId;
    private final int districtId;

    public CustomerKey3(final int customerId, final int districtId) {
        this.customerId = customerId;
        this.districtId = districtId;
    }

    public int getCustomerId() {
        return customerId;
    }

    public int getDistrictId() {
        return districtId;
    }

    @Override
    public boolean equals(final Object obj) {
        if (!(obj instanceof CustomerKey3)) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        final CustomerKey3 rhs = (CustomerKey3) obj;
        final EqualsBuilder builder = new EqualsBuilder();
        builder.append(customerId, rhs.customerId);
        builder.append(districtId, rhs.districtId);
        return builder.isEquals();
    }


    @Override
    public int hashCode() {
        final HashCodeBuilder builder = new HashCodeBuilder(71, 499);
        builder.append(customerId);
        builder.append(districtId);
        return builder.toHashCode();
    }
}</description>
		<content:encoded><![CDATA[<p>I like it.  How would you do equals and hashCode?  Here&#8217;s how I&#8217;d do it:</p>
<p>import org.apache.commons.lang.builder.EqualsBuilder;<br />
import org.apache.commons.lang.builder.HashCodeBuilder;</p>
<p>public class CustomerKey3 {<br />
    private final int customerId;<br />
    private final int districtId;</p>
<p>    public CustomerKey3(final int customerId, final int districtId) {<br />
        this.customerId = customerId;<br />
        this.districtId = districtId;<br />
    }</p>
<p>    public int getCustomerId() {<br />
        return customerId;<br />
    }</p>
<p>    public int getDistrictId() {<br />
        return districtId;<br />
    }</p>
<p>    @Override<br />
    public boolean equals(final Object obj) {<br />
        if (!(obj instanceof CustomerKey3)) {<br />
            return false;<br />
        }<br />
        if (this == obj) {<br />
            return true;<br />
        }<br />
        final CustomerKey3 rhs = (CustomerKey3) obj;<br />
        final EqualsBuilder builder = new EqualsBuilder();<br />
        builder.append(customerId, rhs.customerId);<br />
        builder.append(districtId, rhs.districtId);<br />
        return builder.isEquals();<br />
    }</p>
<p>    @Override<br />
    public int hashCode() {<br />
        final HashCodeBuilder builder = new HashCodeBuilder(71, 499);<br />
        builder.append(customerId);<br />
        builder.append(districtId);<br />
        return builder.toHashCode();<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jesse</title>
		<link>http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/comment-page-1/#comment-23</link>
		<dc:creator>Jesse</dc:creator>
		<pubDate>Fri, 14 Sep 2007 02:58:16 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/2007/09/13/lets-fix-some-crap-code/#comment-23</guid>
		<description>Version 2 is much better. I&#039;d be tempted to add &#039;final&#039; to the class declaration, but I&#039;m paranoid.</description>
		<content:encoded><![CDATA[<p>Version 2 is much better. I&#8217;d be tempted to add &#8216;final&#8217; to the class declaration, but I&#8217;m paranoid.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.259 seconds -->

