Is there anything wrong with a class with all static methods?

后端 未结 16 1049
余生分开走
余生分开走 2021-01-30 16:43

I\'m doing code review and came across a class that uses all static methods. The entrance method takes several arguments and then starts calling the other static methods passin

相关标签:
16条回答
  • 2021-01-30 17:15

    Well, personnally, I tend to think that a method modifying the state of an object should be an instance method of that object's class. In fact, i consider it a rule a thumb : a method modifying an object is an instance method of that object's class.

    There however are a few exceptions :

    • methods that process strings (like uppercasing their first letters, or that kind of feature)
    • method that are stateless and simply assemble some things to produce a new one, without any internal state. They obviously are rare, but it is generally useful to make them static.

    In fact, I consider the static keyword as what it is : an option that should be used with care since it breaks some of OOP principles.

    0 讨论(0)
  • 2021-01-30 17:16

    It depends on whether the passed arguments can really be classified as state.

    Having static methods calling each other is OK in case it's all utility functionality split up in multiple methods to avoid duplication. For example:

    public static File loadConfiguration(String name, Enum type) {
        String fileName = (form file name based on name and type);
        return loadFile(fileName); // static method in the same class
    }
    
    0 讨论(0)
  • 2021-01-30 17:17

    Does it help to rephrase the question:

    Can you describe the data that the static methods operates on as an entity having:

    • a clear meaning
    • responsibility for keeping it's internal state consistent.

    In that case it should be an instantiated object, otherwise it may just be a bunch of related functions, much like a math library.

    0 讨论(0)
  • 2021-01-30 17:19

    "state of a class is ...passed around amongst static methods using arguments?" This is how procedual programming works.

    A class with all static methods, and no instance variables (except static final constants) is normally a utility class, eg Math. There is nothing wrong with making a unility class, (not in an of itself) BTW: If making a utility class, you chould prevent the class aver being used to crteate an object. in java you would do this by explictily defining the constructor, but making the constructor private. While as i said there is nothing wrong with creating a utility class, If the bulk of the work is being done by a utiulity class (wich esc. isn't a class in the usual sense - it's more of a collection of functions) then this is prob as sign the problem hasn't been solved using the object orientated paradim. this may or maynot be a good thing

    The entrance method takes several arguments and then starts calling the other static methods passing along all or some of the arguments the entrance method received. from the sound of this, the whole class is just effectivly one method (this would definatly be the case is al lthe other static methods are private (and are just helper functions), and there are no instance variables (baring constants)) This may be and Ok thing, It's esc. structured/procedual progamming, rather neat having them (the function and it's helper)all bundled in one class. (in C you'ld just put them all in one file, and declare the helper's static (meaning can't be accesses from out side this file))

    0 讨论(0)
提交回复
热议问题