What are the differences between Helper and Utility classes?

How to determine how to call the XHelper or XUtils class?

In my opinion:

A helper class is a class that can be created and perform some business operations.

The Utils class is a static class that performs small and repetitive operations on some instance (example of the utils classes ArrayUtils or IOUtils from Apache)

+95
java
Aug 30 '12 at 7:37
source share
4 answers

There are many naming styles. I would suggest Utils just because it is more common.

A utility class refers only to static methods and does not have statelessness. You would not create an instance of such a class.

The helper may be a utility class, or it may be stateful or require instantiation. I would avoid this if possible.

If you can make the name more specific. for example if it has sorting methods make it XSorter

For arrays you can find helper classes like

Array Arrays ArrayUtil ArrayUtils ArrayHelper 

BTW short hand for utility class is listing without instances

 enum XUtils {; static methods here } 

If you need to implement an interface, I would use a stand-alone Singleton.

 enum XHelper implements RequiredInterface { INSTANCE; // no instance fields. } 
+82
Aug 30 2018-12-12T00:
source share

Generally? This is completely arbitrary. There are no rules for this.

+12
Aug 30 2018-12-12T00:
source share

A utility is a “leaf node” of a public class. That is, it does not have any dependencies on your project and can be transferred from project to project without breaking or becoming useless. Examples: Vector3 , RandomNumberGenerator , StringMatcher , etc.

The “helper” seems to be any class whose purpose is to help another class. It may or may not depend on your project. If you create the GameNetworkClient class, you can say that the GameNetworkConnection class is a "helper" because it "helps" the GameNetworkClient .

The way developers relate to tools reflects the general use of these words. If you can recall hearing aids described as “useful” or “useful,” a useful tool tends to have some context (a cheese grater helps rub cheese, a corn stripper helps clean corn, a high-speed loader helps reload firearms). It is expected that the "utility" will work in different contexts (WD-40, duct tape, army knives, glue, flashlight, etc.).

+3
Jan 28 '19 at 6:41
source share

There is no final answer for this. Find out one naming scheme and stick to it. Naming your packages and classes is an important part of the software architecture, and no one can make this decision from you.

I personally like XHelper better, but I see XUtils more often in someone else's code.

I also like the plural naming scheme you'll find in both the JDK and Guava :

if a class deals with Collection objects, it is called Collections

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

and etc.

+2
Aug 30 2018-12-12T00:
source share



All Articles