简体   繁体   中英

Should I make my private class methods static?

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?

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 .

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.

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.

当我确定它们没有任何副作用时,我只会使方法静止。

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.

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.

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.

如果你有一个静态私有方法,那就好像它可能是一个根本不应该在类中的方法,而应该是一个静态方法库,可以被其他类重用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM