Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad form to have a a MiscUtilities class? [closed]

Tags:

oop

Our company keeps a MiscUtilities class that consists solely of public static methods that do often unrelated tasks like converting dates from String to Calendar and writing ArrayLists to files. We refer to it in other classes and find it pretty convenient. However, I've seen that sort of Utilities class derided on TheDailyWTF. I'm just wondering if there's any actual downside to this sort of class, and what the alternatives are.

like image 732
Bad Request Avatar asked Aug 20 '10 14:08

Bad Request


3 Answers

Rather than giving personal opinion, I will quote from an authoritative source in the Java community, and examples from 2 very reputable third party libraries.

A quote from Effective Java 2nd Edition, Item 4: Enforce noninstantiability with a private constructor:

Occasionally you'll want to write a class that is just a grouping of static methods and static fields. Such classes have acquired a bad reputation because some people abuse them to avoid thinking in terms of objects, but they do have valid uses. They can be used to group related methods on primitive values or arrays, in the manner of java.lang.Math or java.util.Arrays. They can also be used to group static methods, including factory methods, for objects that implements a particular interface, in the manner of java.util.Collections. Lastly, they can be used to group methods on a final class, instead of extending the class.

Java libraries has many examples of such utility classes.

  • Apache Commons Lang follows the TypeUtils naming convention
    • ArrayUtils, StringUtils, ObjectUtils, BooleanUtils, etc
  • Guava follows the Types naming convention
    • Objects, Strings, Throwables, Collections2, Iterators, Iterables, Lists, Maps, etc.
    • The package summary actually has a specific section on classes of static utility methods
    • Another entire package consists of nothing but utility classes for working with Java primitives, Ints, Floats, Booleans, etc.

Short summary

  • Prefer good OOP design, always
  • static utility classes have valid uses to group related methods on:
    • Primitives (since they're not objects)
    • Interfaces (since they can't have anything concrete of their own)
    • final classes (since they're not extensible)
  • Prefer good organization, always
    • Group utility methods for SomeType to SomeTypeUtils or SomeTypes
    • Avoid a single big utility class that contains various unrelated tasks on different types/concepts
like image 197
polygenelubricants Avatar answered Nov 20 '22 01:11

polygenelubricants


Convenient, most likely.

Possible to grow into a scary, hard to maintain swiss-army-rocket-chainsaw-and-floor-polisher, also most likely.

I'd recommend separating the various tasks into separate classes, with some logical grouping besides "won't fit anywhere else".

The risk here is that the class becomes a tangled mess nobody fully comprehends and noone dares to touch - or replace. If you feel that is an acceptable risk and/or avoidable under your circumstances, nothing really prevents you from using it.

like image 4
Piskvor left the building Avatar answered Nov 20 '22 00:11

Piskvor left the building


I've never been a fan of the MiscUtilities class. My biggest issue is that I never know what is in it. Anything filed under miscellaneous is not discoverable. Instead I prefer to use a common dll that I can import into my projects that contains well named, separated classes for different purposes. The difference is subtle, but I find that it makes my life a little easier.

like image 3
Matthew Vines Avatar answered Nov 20 '22 00:11

Matthew Vines