Util IS a code smell

The Fishbowl: Util is a language smell.

I beg to differ. Classes should never end in Util. Or Utils or Utilities or any synonym thereof for that matter. They always seem to end up as random collections of static methods that know far too much about the internals of the objects they manipulate.

Far better to rationalise your Util classes into concrete objects. The only time a utility class is justified is when working around final core classes like String. Even then, I find that ‘Strings’ is a perfectly good name for the class, rather than StringUtil.

I would always seek to add a wrapper or decorator class that encapsulates a single instance of the thing being worked on, rather than a static function library. Making java look like C is missing the point (and all the benefits) of working in an OO language.

4 thoughts on “Util IS a code smell

  1. Bah. The main criteria to apply is not “Do I like it?” but “Does it work?” and the response in this case is yes, it works. ClassloaderUtil works much better than Classes. It comes down to preference. You like one; I don’t care either way. Java’s lack of static methods outside of a class context is a weakness, one I can tolerate well.

  2. java.io.* is great example of the crazyness of decorator pattern. I’d rather have IOTool (or IO or whatever class) with static methods to create properly decorated IO objects instead of writing

    InputStream is = new Decorator4(new Decorator3(new Decorator2(new Decorator1(new InputStream())));

  3. Most of my “utils” methods are things where I find that I’m writing the same damn 3-10 lines of code every time I want to do something, like parse XML, or string tokenizing, or avoiding a checked exception with non-user-generated objects (URL and regular expressions). There’s no reason to add an Object construction and the memory wasted for such things. As for the name itself, that’s just a preference thing, and Java has done both. SwingUtilities.* for the util case, and “Arrays.*” and “Collections.*” for collections for the “s” case, both of which came out in Java2. I prefer Utils because I know what it means and its easier to find when looking down the javadocs than seeing “ArrayList” and “Arrays” side by side. From a programmer perspective of one who reads the javadocs constantly, Util sticks out and is easy to find, so I’ll look at it. I didn’t know about Arrays.asList for weeks after I started using collections, because “Arrays” doesn’t stick out.

  4. When I see “Strings”, I think “More than one String”.

    As for decorators and adapters, they have their place, but you have to take into account how they change the object’s type and identity – which can make them too heavyweight for just small operations.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s