In what situations is static method a good practice?

后端 未结 11 1048
走了就别回头了
走了就别回头了 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 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.

提交回复
热议问题