directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Emmanuel Lecharny" <elecha...@gmail.com>
Subject Code cleaning + simple rules
Date Sun, 23 Dec 2007 15:24:35 GMT
Hi guys,

with Felix's help, I was able to fix some bad code (he has generated a
lot of reports using PMD, CheckStyle, etc).

There are some specific pieces of code we could improve. I just got
through the hashCode and equals method, and there are pretty
inconsistant. Here are the very basic rules we should follow all over
the code (I didn't invented them, Joshua Bloch exposed them in his
very valuable book)

1) Equals methods :
a) compare this with obj, and return true if they are equal ( if (this
== obj ) return true ) ). This is for efficiency
b) do a if ( ! ( obj instanceof <class> ) ) return false
c) cast obj to <class>
d) do whatever operation needed to compare obj and this members

This is the most efficent way to write an equals method. There is no
need to check (obj == null ), as the obj instanceof will take care of
that

2) Hashcode
to be consistent, this is the way to write this method :

int h = 37;

for each element
  h = h * 17 + element.hashcode()

return h

3) We have a HashCodeBuilder class which should be replaced by a
direct hashCode method

4) We should have a toString() method for each class, or at least for
each data structure. This is helpful for debugging purpose

5) Use logs as much as possible, and use common sense when using them
(use error when needed, debug when needed, etc...). Usually, log an
error when catching an exception is good policy...

Have fun !

-- 
Regards,
Cordialement,
Emmanuel L├ęcharny
www.iktek.com

Mime
View raw message