Consistency in equals and hashcode implementations

Overriding the equals/hashcode methods defined on the Object class is very difficult to implement correctly (see Effective Java by Josh Block for a description of the minefield buried here). We should instead try to delegate the responsibility of overriding these methods to a framework to insure correct and consistent implementations. Possibilities her are:

Solution

Pros

Cons

Use the IDE's built-in code generators to generate equals and hashcode methods

  • No new framework need to be added to the project
  • Will not be consistent across the system because of the different IDE's and configurations used by the developers. 
  • May also be difficult to maintain, as a developer needing the update the attributes used in the equals and hashcode methods will properly have to generate to regenerate the full implementation, thereby cluttering the SCM with bloated changeds.
  • Developers need to remember to add any new attributes to both the hashcode and equals methods.

Use a framework with hashcode and equals generators, like the EqualsBuilder and HashCodeBuilder from the Apache Commons Lang library.

  • Short and concise hashcode and equals implementation code.
  • Consistent implementations.
  • Relatively easy to maintain.
  • May need inclusion of a new framework.
  • More dependent on the developers using the EqualsBuilder and HashCodeBuilder correctly.
  • Developers need to remember to add any new attributes to both the hashcode and equals methods.

Use a data object generation framework, which would take care of many of the common data object challenges. An example of such a framework can be found in the JEntity project, which is maintained by our own M.

  • No developer involvement in implementing any data object code, nor any test code.
  • Consistent data object implementation.
  • Correct data object implementations
  • Formal domain model.
  • Data objects will be auto generated, decreasing the source base.
  • Introduction of new framework.
  • Some JEntity development work might be needed.
Unknown macro: {vote}

Continue as usual
Use IDE geenration
Use Apache framework
Use Jentity