views:

517

answers:

8

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: http://stackoverflow.com/questions/169378/c-method-can-be-made-static-but-should-it

+20  A: 

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.

Andrew Hare
A: 

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

Botz3000
what do you mean by side effects and how would I determine if they do or do not have side effects
MedicineMan
Basically it means "if they can be made static". Since if they access instance members, it's not possible to make them static anyway.
Botz3000
If it has side effects on an instance of an object, it won't compile. (Side effect = modifying a specific object's properties or environment rather than just taking arguments and optionally providing a result).
richardtallent
+3  A: 

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.

Dan Diplo
eeee, not so sure about this one - the semantic correctness of the objects modelled and therefore their readability and maintainability should take precedence over marginal performance concerns. Be concerned with what is *right*. Just my $.02
annakata
I agree with you - correctness should win over performance. But, when all thinks are equal, then there are advantages, too. Remember, we are just talking about private methods of a class, and the fact that a method is static helps with readability to the extent that it indicates that the method doesn't mess with the state of the class. I believe Resharper recommends this, too, if that holds any sway!
Dan Diplo
A: 

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.

Guffa
I am not sure that I agree. I would be interested to see an instance method that was faster to call than a static method. That being said I do agree that for most cases the performance difference will be indiscernible.
Andrew Hare
+2  A: 

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.

Yar
A: 

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.

richardtallent
I disagree. Promoting the idea of helper classes is a much larger code smell. There's very little I hate more in software development excluding datasets/datatables than helper classes. The only helper classes you should write in 3.5 is if you create extension methods since creating extension methods is a global usage as opposed to targetting some contrived specific cases. Not to mention almost every single helper class fails SRP and becomes a huge code failure point due to the number of dependencies to it.
Chris Marisic
A: 

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.

Chris Marisic
+1  A: 

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.

Adrian
I think I know where you are going with this, but can you elaborate anyways?
MedicineMan