What are the differences between Helper and Utility classes?

Java

Java Problem Overview


How determine how call a class XHelper or XUtils ?

To my mind :

Helper class, is a class that can be instantiate and do some business work

Utils class, is a static class that perform small and repetitive operations on a kind of instance (example of utils classes ArrayUtils or IOUtils from Apache)

Java Solutions


Solution 1 - Java

There are many naming styles to use. I would suggest Utils just because its more common.

A Utility class is understood to only have static methods and be stateless. You would not create an instance of such a class.

A Helper can be a utility class or it can be stateful or require an instance be created. I would avoid this if possible.

If you can make the name more specific. e.g. if it has sorting methods, make it XSorter

For arrays you can find helper classes like

Array
Arrays
ArrayUtil
ArrayUtils
ArrayHelper

BTW a short hand for a utility class is an enum with no instances

enum XUtils {;
    static methods here
}

If you need to implement an interface, I would use a stateless Singleton.

enum XHelper implements RequiredInterface {
   INSTANCE;
   // no instance fields.
}

Solution 2 - Java

In general? It's entirely arbitrary. There are no rules for this.

Solution 3 - Java

A utility is a "leaf node" class of general use. That is, it doesn't have any dependencies into your project and can be ported from project to project without breaking or becoming useless. Examples: Vector3, RandomNumberGenerator, StringMatcher, etc...

A "helper" seems to be any class whose design is to aid another class. These may or may not depend on your project. If you're creating a GameNetworkClient class, you could say the GameNetworkConnection class is a 'helper', because it "helps" the GameNetworkClient.

The way developers refer to tools reflects common usage of these words. If you can recall hearing tools described as "helpful" vs "useful", a helpful tool tends to have some context (cheese grater helps to grate cheese, corn stripper helps to strip corn, speed loader helps to reload a firearm). A "utility" is expected to work in a variety of contexts (WD-40, duct tape, army-knives, glue, flashlight, etc...).

Solution 4 - Java

As Jesper said, it's entirely arbitrary. You can think of what works for your organization and make that the convention.

For me, it's something like this:

> utils - Static class, that can be freely moved and imported anywhere.

Doing general tasks that could be useful in different modules. As Peter Lawrey said, more specific names are useful.

> helper - Class helping another class or a module.

Tasks that are only used in the module it's placed and won't make sense to be imported elsewhere. Hence the name could be more specific - ModuleNameHelper (e.g. AdministrationHelper, LoginHelper)

Solution 5 - Java

There's no ultimate answer for this. Figure out one naming scheme and stick with it. Naming your packages and classes is an important part of software architecture, and nobody can take that decision away from you.

I personally like the XHelper better, but I see XUtils more often in foreign code.

I also like the "plural" naming scheme you will find both in the JDK and Guava:

if a class deals with Collection objects, it's called Collections

Array > Arrays (jdk)
List > Lists (guava)
Map > Maps (guava)

etc.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjakcamView Question on Stackoverflow
Solution 1 - JavaPeter LawreyView Answer on Stackoverflow
Solution 2 - JavaJesperView Answer on Stackoverflow
Solution 3 - JavaJames M. LayView Answer on Stackoverflow
Solution 4 - Javatyphon04View Answer on Stackoverflow
Solution 5 - JavaSean Patrick FloydView Answer on Stackoverflow