Should all methods be static if their class has no member variables

前端 未结 20 2148
悲哀的现实
悲哀的现实 2020-12-02 14:18

I\'ve just had an argument with someone I work with and it\'s really bugging me. If you have a class which just has methods like calculateRisk or/and calc

相关标签:
20条回答
  • 2020-12-02 15:21

    In above answers i read the argument of not making it static for testing reasons. I do not see why, but I am not familiair with the test procedure you're using.

    In our company we use unit and system testing with Junit. (org.junit) There it's no problem at all to test static methods with this test package.

    So I would say: yes make them static.

    (With the remark that I do not know the test procedure where testing static methods is a problem)

    0 讨论(0)
  • 2020-12-02 15:22

    There is a general feeling against static classes these days, for testability reasons.

    This is because static classes cannot implement an interface and so cannot be easily substituted for test classes if needed. I think that cases like this that deal with price are an excellent example of this. It is possible in future that there may be multiple ways of calculating a price, and it is nice to be able to substitute these calculators for one another as the need arises.

    It may not be useful now but I see more advantages in not using a static class than using one.

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