MedicineMan
MedicineMan

Reputation: 15322

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?

Upvotes: 16

Views: 3174

Answers (8)

Adrian
Adrian

Reputation:

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.

Upvotes: 0

Chris Marisic
Chris Marisic

Reputation: 33128

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.

Upvotes: 0

richardtallent
richardtallent

Reputation: 35404

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.

Upvotes: -1

Dan Rosenstark
Dan Rosenstark

Reputation: 69787

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.

Upvotes: 6

Guffa
Guffa

Reputation: 700910

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.

Upvotes: 0

Andrew Hare
Andrew Hare

Reputation: 351758

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.

Upvotes: 40

Dan Diplo
Dan Diplo

Reputation: 25369

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.

Upvotes: 3

Botz3000
Botz3000

Reputation: 39670

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

Upvotes: 0

Related Questions