Menu

Work in progress

Recently I started to develop with Eclipse Kepler (version 4.2). I was quite astonished how much warnings it showed up for my code. Usually I trusted on the default configuration of Netbeans. Now I learned that for Netbeans I have to enable additional warnings. Like the javac compiler warnings which I found to be switched off.

I enabled these and moreover I checked my code with PMD and FindBugs. That brought more interesting hints. I improved one weakness after another. Now Findbugs doesn't show up weak points - in the default configuration. I have to learn about this.

One weakness I fixed was that clone() need to call super.clone() and therefore it needs to throw an CloneNotSupportedException.

This let me change the way some objects are created. Some classes had constructors which throw an IllegalArgumentException. I (hopefully) did it in a safer way along the CERT Coding Standards but ... If I attempt to create an object an get an exception - what does it mean? Maybe wrong arguments, technically. But domain-specific? I want a new car => boom! Exception. What was my fault? That I need a new car?

So I decided to provide a protected constructor with no checks of argument values and some public factory methods which will check the argument values and throw an IllegalArgumentException if necessary.

If there is a clone() method one of these factory methods will accept an object of this class as argument (objectToClone) and return objectToClone.clone as result. If the object is null we get an IllegalArgumentException. This factory method consumes the CloneNotSupportedException which may thrown by clone() and raises an RuntimeException instead. If a CloneNotSupportedException is thrown the we inherit from a class there cloning is not supported. This would be a fault of the programmer who build this library (me) and should fall out a testing. So this is nothing the client programmer needs to catch and raise an error message to the user or log it.

Edit: Meanwhile I was reading in the fine book of Joshua Bloch "Effective Java" (Second Edition). So I came to Item 11 and realized that I did implementig clone() wrong. Also, I learned to prohibit inheritance for all classes except those which are designed for inheritance. At last I decided to do so:

  • Constructors are required to construct instances with all valid states for this class. For example, the constructor of class String let you construct instances of any valid state for the class String. If someone tries to construct an instance with an invalid state a Runtime exception will occur. For classes which can not used to break the security of a system this should be OK because Runtime exceptions occur for programming errors. Testing our software before delivering should raise such problems.

  • Factory methods construct instances with a certain valid state. Imagine a String which is empty or contains "Hello world!". Among them are "copy methods" designed to provide a copy of another instance of this class. Both copies are completely independent. Using a factory method makes it impossible to access the fields of the other instance. This is a benefit because a copy method defined outside the package cant access the fields of the other instance too. Thus, it impossible that two copy methods behave different because one uses access the fields of the other instance while the other copy method can not.

Posted by Michael Groß 2014-03-05

Log in to post a comment.