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

后端 未结 16 1096
余生分开走
余生分开走 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.

提交回复
热议问题