Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private helper methods vs. public static utility methods in Java

I have a Java class that is becoming long. When I run it through code quality tools, I get flagged for number of lines in the class.

This is a lower layer class, used by upper layers with Spring's @Autowired. The class has a lot of private instance methods which are not static. They don't use any instance fields, working only on method parameters.

Can I safely move these methods as public static in some separate utility class? What are the downsides?

like image 777
Sabir Khan Avatar asked Jun 28 '17 09:06

Sabir Khan


People also ask

Should private helper methods be static?

1 Answer. I prefer such helper methods to be private static; which will make it clear to the reader that they will not modify the state of the object.

Are helper methods private or public?

Helper methods are often declared as public static so they can be invoked with the class name like Classname.

What is the difference between utility and helper classes?

A utility class is a special case of a helper class in which the methods are all static. In general, helper classes do not have to have all static methods, but may have instance variables. Multiple instances of the helper class may exist as well.

Should a private helper method be defined as static or non-static?

Thirdly a non-static private method can be called from a constructor of the class also, it need not be static. Now coming to your question if a private helper method should be defined as static or non-static.

Are utility & helper classes the same class in Java?

Some will insist Utility & Helper classes are different, while others will insist they are the same. They certainly share a number of features in idiomatic Java. All their methods will be static, so it will make no sense to instantiate the class, This will be prevented with a constructor that is private and/or throws an exception.

What is the use of private modifier in Java?

If a variable or methods or constructor is declared as private then we can access them only from within the class i.e from outside the class we can’t access them. This modifier is applicable for both top-level classes and interfaces.

What is the difference between public and private in Java?

In Java, public and private are keywords that are known as an access modifier or specifier. It restricts the scope or accessibility of a class, constructor, variables, method s, and data members. It depends on which it is applied. Java provides the four types of access modifiers: public, private, protected, and default.


1 Answers

"Wrong" mindset here.

You do not rework your classes because tools are complaining about this or that.

You want to improve the quality of your source code; and such tools can help with identifying "topics worth thinking think about". You take their feedback as hint; not as "order".

Therefore you don't worry about "lines of codes" within a class. Instead, you worry about the responsibilities that this class has. Meaning: the line number count isn't a problem per se - but violations of the Single Responsibility Principle are.

Thus: you step back and check out what exactly your class is doing. And when it is clearly doing more than one thing, you extract such aspects into other classes!

Meaning: if you actually find that all this code "belongs" to the responsibility of that class; then keep it in there. Don't start putting internal implementation details into unrelated helper classes; just because some tool warns you about number of lines.

On the other hand, turning private methods into something that is static/package protected would allow you to unit test those methods. Which could be an advantage. But as said: as long as we are talking about implementation details, they should stay private; and they shouldn't be unit tested anyway.

Long story code: know and understand what "clean code" is about; and try to follow the ideas outlined there.

like image 56
GhostCat Avatar answered Sep 23 '22 00:09

GhostCat