However, one should not overestimate the creation of static utility methods? how to avoid this?


Over time, the java project introduces many useful methods for a more complex and simple task.

When using static methods, we introduce a hard link in our code, and this makes our code more difficult to test, especially if the utility methods are quite complicated.

I just think it's hard to manage and test these utilities right now. please help me avoid these utility methods and how can I organize an existing project to remove all STATIC utilities.

Can you help me avoid the static method?

+4
source share
4 answers

To contradict other currently available answers: Static methods are bad!

They make a strong connection. Yes, there are times when this is acceptable. Yes, you can make a seam inside the static method using the strategy used inside the shift method. But, as a rule, static ones are still bad.

To answer the question of how to get rid of static methods. Simple: put them on the appropriate object. All statics are gone. Have we improved our code? not a lot yet. If we replace

callToStaticMethod() 

with

 new X().callToNoLongerStaticMethod() 

we replaced the static call with a constructor call, which is essentially another static method. But now your X is just another dependency, so you can enter it:

 class A{ private final X x; A(X aX){ x = aX; } } 

Note: There is no need to use Spring or any other infrastructure for this. If it seems to you that it provides a constructor that uses the default implementation. If you are a purist, enter the interface for X.

Testing A , not relying on the implementation of X , becomes trivial and obvious. The same goes for replacing X any way.

+2
source

There is nothing wrong with having a lot of static methods.

Static methods (or should be read) are stateless , which makes them the easiest methods to test - no configuration, just call them.

You do not need to mock because there is no condition to deal with.

Regarding statelessness, technically static methods can be restrained if they use static variables to store the state. If so, then from a good design point of view, they should be converted to instance methods using instance variables in order to maintain state, using the singleton pattern if necessary.

+6
source

Static utilities are not so bad. You can hide the package-related strategy behind a static call. This can be easily checked (and replaced), given that the test case applies to the same package. Moreover, it makes the code very readable. Of course, static utility method clients can use only one implementation in their tests. So here is some inflexibility.

Bohemian is right in speaking of the state. If your static utilities have state, you are doing something wrong.

About your question: if you want to avoid static methods, you can use the spring structure and define various implementations of utilities that you use and test in different contexts. However, in this case access to these objects is not so convenient, since you must first get a link to the context that your utility object knows.

+1
source

There is nothing wrong with a set of static utility methods that belong together in a class. See for example java.util.Collections . If every method of this class that works with List is specified in the List interface itself, they must be implemented by all subclasses. While they can be implemented by the public methods of List , there are no problems.

Of course, as soon as you start adding methods to the interface (or in the case of a class, making methods publicly available) just to be able to put functions in static methods instead of the class itself, then you are on the wrong track.

+1
source

Source: https://habr.com/ru/post/1488852/


All Articles