Should I make my private class methods static?

偶尔善良 提交于 2019-12-20 12:32:14

问题


Is there a best practice for making private methods in classes static? I have a class with a few methods. Some of these can easily be made static, since they simply process data.

Should I make them static or just leave them as is? Is this more of a style issue? Are there performance considerations?

Edit: Method can be made static, but should it?


回答1:


If the methods don't access any of the type's state then they should be static.

Static method calls provide a performance gain over instance methods and the presence of a static method tells future readers of your code that calling this method will create no side effects in the the state of the current instance of the type.

The performance gain of a static method comes from the fact that the compiler does not have to emit callvirt instructions to call a static method. The callvirt instruction is handy for instance calls in that it does a null check prior to calling the method. However, when you call a static methods there is no need to check for null so the compiler is free to emit the faster call instruction which doesn't check for null.




回答2:


As Dan Diplo says, if they can be made static, they should be made static. This is only true for private static methods (which is what you asked about). For public methods, however, they will just confuse users of your class. Public methods should be made static only if they will be used without an instance of the class by callers, the performance loss of not making them static be damned.




回答3:


I always think if they can be made static (and the compiler will soon let you know if they can't) then they should be made so. See this duplicate question on SO for further discussion.




回答4:


I only make methods static when i am sure that they don't have any side effects.




回答5:


If a method doesn't use any instance data in the class, it should be static. That way it's clear that it's independent of the instances, and that you don't have to create an instance to call it.

Any performance differences should not be a concern. There is of course a difference in how a static method and an instance method are called, but you will most likely not be able to measure any consistent performance difference. The difference is so small that sometimes calling an instance method might actually be faster just because the instructions happens to line up better in the execution.




回答6:


I agree that all private methods that can be static should be static. Just remember the performance variation between the 2 are insignificant so don't do this in an attempt to increase performance since early optimization is likely to cause more failures than successes until you know for sure you have a performance issue and profile your application.




回答7:


Think about the static methods and multi-threading. You must be very careful. This discusion is like pattern definition: "a solution for a problem in some context.". You must not say "This should be done like this." It depends of the context.




回答8:


If you have a private method that is static, that smells like it could be a method that shouldn't be in the class at all, but should rather be in a library of static methods that can be reused by other classes.



来源:https://stackoverflow.com/questions/1375227/should-i-make-my-private-class-methods-static

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!