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

后端 未结 16 1046
余生分开走
余生分开走 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条回答
  • The biggest problem IMO is that if you want to unit test classes that are calling the class you mention, there is no way to replace that dependency. So you are forced to test both the client class, and the staticly called class at once.

    If we are talking about a class with utility methods like Math.floor() this is not really a problem. But if the class is a real dependency, for instance a data access object, then it ties all its clients in to its implementation.

    EDIT: I don't agree with the people saying there is 'nothing wrong' with this type of 'structured programming'. I would say a class like this is at least a code smell when encountered within a normal Java project, and probably indicates misunderstanding of object-oriented design on the part of the creator.

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

    Is there anything wrong with this pattern? Is this just a matter of personal choice if the state of a class is held in fields and properties or passed around amongst static methods using arguments?

    Speaking from my own personal experience, I've worked on 100 KLOC applications which have very very deep object hiearchies, everything inherits and overrides everything else, everything implements half a dozen interfaces, even the interfaces inherit half a dozen interfaces, the system implements every design pattern in the book, etc.

    End result: a truly OOP-tastic architecture with so many levels of indirection that it takes hours to debug anything. I recently started a job with a system like this, where the learning curve was described to me as "a brick wall, followed by a mountain".

    Sometimes overzealous OOP results in classes so granular that it actually a net harm.

    By contrast, many functional programming languages, even the OO ones like F# and OCaml (and C#!), encourage flat and shallow hiearchy. Libraries in these languages tend to have the following properties:

    • Most objects are POCOs, or have at most one or two levels of inheritance, where the objects aren't much more than containers for logically related data.
    • Instead of classes calling into each other, you have modules (equivalent to static classes) controlling the interactions between objects.
    • Modules tend to act on a very limited number of data types, and so have a narrow scope. For example, the OCaml List module represents operations on lists, a Customer modules facilitates operations on customers. While modules have more or less the same functionality as instance methods on a class, the key difference with module-based libraries is that modules are much more self-contained, much less granular, and tend to have few if any dependencies on other modules.
    • There's usually no need to subclass objects override methods since you can pass around functions as first-class objects for specialization.
    • Although C# doesn't support this functionality, functors provide a means to subclass an specialize modules.

    Most big libraries tend to be more wide than deep, for example the Win32 API, PHP libraries, Erlang BIFs, OCaml and Haskell libraries, stored procedures in a database, etc. So this style of programming is battle testing and seems to work well in the real world.

    In my opinion, the best designed module-based APIs tend to be easier to work with than the best designed OOP APIs. However, coding style is just as important in API design, so if everyone else on your team is using OOP and someone goes off and implements something in a completely different style, then you should probably ask for a rewrite to more closely match your teams coding standards.

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

    No, many people tend to create completely static classes for utility functions that they wish to group under a related namespace. There are many valid reasons for having completely static classes.

    One thing to consider in C# is that many classes previously written completely static are now eligible to be considered as .net extension classes which are also at their heart still static classes. A lot of the Linq extensions are based on this.

    An example:

    namespace Utils {
        public static class IntUtils        {
                public static bool IsLessThanZero(this int source)
                {
                    return (source < 0);
                }
        }
    }
    

    Which then allows you to simply do the following:

    var intTest = 0;
    var blNegative = intTest.IsLessThanZero();
    
    0 讨论(0)
  • 2021-01-30 17:04

    if there's no need of creating an object of a class, then there's no issue in creating all method as static of that class, but i wanna know what you are doing with a class fullof static methods.

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

    One of the disadvantages of using a static class is that its clients cannot replace it by a test double in order to be unit tested.

    In the same way, it's harder to unit test a static class because its collaborators cannot be replaced by test doubles (actually,this happens with all the classes that are not dependency-injected).

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

    I'm not quite sure what you meant by entrance method but if you're talking about something like this:

     MyMethod myMethod = new MyMethod();
     myMethod.doSomething(1);
    
     public class MyMethod {
          public String doSomething(int a) {
              String p1 = MyMethod.functionA(a);
              String p2 = MyMethod.functionB(p1);
              return p1 + P2;
          }
          public static String functionA(...) {...}
          public static String functionB(...) {...}
     }
    

    That's not advisable.

    I think using all static methods/singletons a good way to code your business logic when you don't have to persist anything in the class. I tend to use it over singletons but that's simply a preference.

     MyClass.myStaticMethod(....);
    

    as opposed to:

     MyClass.getInstance().mySingletonMethod(...);
    

    All static methods/singletons tend to use less memory as well but depending on how many users you have you may not even notice it.

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