Java Comparable interface provides a way to do natural ordering for classes implementing the interface. Natural ordering makes sense for scalars and other quite simple objects, but when we come to more business oriented domain objects the natural ordering becomes much more complicated. A transaction object’s natural ordering from business manager’s point of view could be the value of the transaction, but from the system admin’s point of view the natural ordering could be the speed of the transaction. In most cases, there is no clear natural ordering for business domain objects.

Let’s assume that we have found a good natural ordering for a class like Company. We will use the company’s official name as the primary order field and the company id as the secondary. The Company class’ implementation could be as follows.

The implementation looks fine and works properly. The Company class isn’t enough for some use cases so we extend it to a CompanyDetails class that provides more information about the company. Instances of these classes could be used for example in a data table showing details of companies.

Again the implementation looks fine at first glance, but actually it isn’t. We can create a small test case to indicate the issues of the implementation. The problem arises when we don’t know what the actual interface is doing with our class and we don’t pay enough attention to all details of the super class we are extending.

We use two sets, but realize that they behave differently. Why is that? The other set is a HashSet that relies on the object’s hashCode() and equals() methods, but the other is TreeSet and relies only on the Comparable interface, which we didn’t implement for the subclass. This is quite common mistake when domain objects are extended, but more than that, this is about bad coding conventions. We used Apache Commons‘ builders to implement hashCode()equals() and compareTo() methods. The builders supply appendSuper() method that indicates that it should be used for the super class’ implementation for the method. If you have read the great book Effective Java by Joshua Bloch, you’d realize that this is not right. If we add fields in subclass, we cannot implement equals() or compareTo() methods properly without violating the symmetry rule. We should have used composition over inheritance. If we had used composition to create the CompanyDetails, there would have been no issue for the Comparable interface, because we don’t implement it automatically and allow misbehavior by default. And also we could satisfy the requirements of equals() and hashCode() properly.

The issues mentioned in this post are quite common but usually overlooked. The problems with Comparable interface actually arises from bad conventions and not understanding the requirements of the used interfaces. As a Java developer or architect, you should pay attention to things like this and obey good coding conventions and practices. The bigger the project, the more important it is to avoid errors created by the human factor. I tried to sum up a good best practices list for Comparable interface so the errors could be avoided.

Best practices for Java Comparable interface design and usage:

  • Understand the domain object you are creating and if there is no clear natural ordering for the object, do not implement Comparable interface.
  • Prefer Comparator implementations over Comparable. Comparator can be used more business oriented way depending on the use case.
  • If you need to create interfaces or libraries that rely on comparing objects, provide your own Comparator implementation if possible, otherwise create good documentation how the Comparator should be implemented for your interface.
  • Obey good coding conventions and practices. Effective Java is great book to start with.
Posted in Java | Tagged , ,
Share this post, let the world know

Leave a Reply

Your email address will not be published. Required fields are marked *