What is a Utils class?
Many objects combine state and function: that's one of the key distinguishing features of object-oriented programming. But not all objects are that well-formed. Some objects have state and no function, just constructors, getters and setters. These look a lot like C structs. Ivan Moore suggests calling these NOJOs.
At the other extreme are objects that have no state, but do have function. You can always spot one of these because there are no references (implicit or otherwise) to "this": each method operates only on its arguments.
Because there's no state, there's no real need to instantiate classes like this, and so it's usually simpler to keep all the methods static. Classes like this are often called “utils”, and many experts dislike them.
When shouldn't I use one?
If you have lots of NOJOs, then you need somewhere to put the behaviour associated with them. So one style of programming is to put the data in NOJOs, and the behaviour in Utils classes. This is to miss the point of object-oriented programming, and is generally seen as a bad code smell.
When is it OK to use one?
The very short answer: not very often.
The slightly longer answer: it's ok to use a Utils class when the natural owner of the method is either a data type that isn't a class in your programming language, or the natural owner is a class, but you can't modify the class.
The class java.util.Arrays is a good example: methods like
Arrays.sort(byte[] a);
look like they ought to belong to whatever class a is an instance of. However, Java isn't quite as object-oriented as all that, and in fact a is not an instance of a class at all – arrays are a primitive data type. Some primitive types have their equivalents as first-order classes (think of int and Integer) but that’s really a clumsy workaround for the problem that primitive data types aren’t classes.
Things get worse though: suppose we want to add our own method to Arrays. A real example from the Sun SPOT project was a group of methods for putting numbers into and plucking them out of byte arrays. Here's an example:
public static void writeLittleEndShort(byte[] byteArray, int offset, int value);
We've already seen that we can't put this on the non-existent "byte[]" class. However, we can't even put it on the Arrays class with the existing system-supplied utility methods. J2SE developers can't put it there because you simply can't rebuild the system library. In the Sun SPOTs project, we authored the library, but we still weren't allowed to put it there because changing the interface of any of the system library classes would make it "not proper Java", which Sun wouldn't want to be in the business of shipping. So you end up with not one but two Utils classes holding methods that operate on byte arrays.
Here's another example from the Sun SPOT project:
/**
* Convert an Enumeration to a Vector
* @param items the Enumeration to convert
* @return the Vector
*/
public static Vector enumToVector(Enumeration items) {
Vector result = new Vector();
while (items.hasMoreElements()) {
result.addElement(items.nextElement());
}
return result;
}
This really wants to be be an instance method called toVector() on the class Enumeration: but for the reasons above, it can't be.
In summary
When you meet a Utils class, ask yourself: for each of its methods, could any of the arguments own this method? If yes, then move it. If there are methods left after this, you're stuck with a Utils class.
And finally, could we avoid this?
Well, in Java, today, we can't.
What the world needs is a programming environment where you can add methods to system classes, so you can just add the toVector() method to the Enumeration class. A nice addition to that would be a code repository that doesn't impose the restriction that all of one class has to be in one module, but lets you store a group of methods in a separate module, so that you can manage your toVector() method independently. And finally, it'd be nice if the programming language treated everything as an object, because then we could put that writeLittleEndShort() method directly on the "byte[]" class where it belongs. Given those three things, there really wouldn't be any need for Utils classes.
Those with memories as long as mine will be muttering "Smalltalk and the ENVY/Developer code repository" at this point. Perhaps we'll reinvent that wheel eventually....
Friday, 28 November 2008
Subscribe to:
Posts (Atom)