In what situations is static method a good practice?

后端 未结 11 1004
走了就别回头了
走了就别回头了 2020-12-04 10:04

I have read the following discussions:

Should private helper methods be static if they can be static , and
Should all methods be static if their class has no mem

相关标签:
11条回答
  • 2020-12-04 10:57

    Think of this for a moment. In OO coding, every single function call actually looks like this:

    method(object this, object arg1, object arg2) where this is the object you are calling. All it really is is syntax sugar for this. Additionally it allows you to clearly define scope of variables because you have object variables etc.

    Static methods simply don't have a "this" parameter. Ie you pass variables in and possibly get a result back out. 1.) is the main reason people avoid them, you can't create an interface to a static method (yet) so you can't mock out the static method to test it.

    Secondly OO are procedures functions etc. Static methods make a lot of sense in certain situations, but they can always be made into a method on an object.

    Mind you, you couldn't remove this without a hack:

    static void Main(string[] args)
    {
    }
    

    The code that starts your application MUST be callable WITHOUT a reference to an object. So they give you flexibility, whether you choose to use them in your scenario will be predicated by your requirements.

    0 讨论(0)
  • 2020-12-04 10:57

    Another good scenario for static methods are implementations of the Factory pattern, where you are allowing for instances of a class to be constructed in a specific manner.

    Consider the Calendar class, which has a set of static getInstance methods, each returning instances primed with the desired TimeZone and/or Locale or the default.

    0 讨论(0)
  • 2020-12-04 10:59

    First of all, you can't dismiss static-methods there is a reason people still use it.

    1. some design patterns are based on static methods, singleton for example:

      Config.GetInstance();

    2. Helper functions, lets say you have a byte-stream and you want a function to convert it into a string of Hex numbers.

    there are many uses for static-methods, saying that does not mean that some people abuse it too much(Code Review is best option when people abuse code).

    0 讨论(0)
  • 2020-12-04 11:00

    I think a definite case for static methods is when you cannot make them dynamic, because you cannot modify the class.

    This is typical for JDK objects, and also all objects coming from external libraries, and also primitive types.

    0 讨论(0)
  • 2020-12-04 11:05

    I'd say that static methods are definitely OK when they are functions, i.e. they don't do any IO, don't have any internal state and only use their parameters to compute their return value.

    I'd also extend this to methods that change the state of their parameters, though if this is done excessively, the static method should properly be an instance method of the parameter class that it mainly operates on.

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