tags:

views:

882

answers:

8

Suppose you have some method that could be made static, inside a non-static class.
For example:

private double power(double a, double b)
    {
        return (Math.Pow(a, b));
    }

Do you see any benefit from changing the method signature into static? In the example above:

private static double power(double a, double b)
    {
        return (Math.Pow(a, b));
    }

Even if there is some performance or memory gain, wouldn't the compiler do it as a simple optimization in compile time?


Edit: What I am looking for are the benefits by declaring the method as static. I know that this is the common practice. I would like to understand the logic behind it.
And of course, this method is just an example to clarify my intention.

+1  A: 

this method should be static because it is not related to your class or member of classes. it just works with the inputs to this function.

maybe you may need to call it without creating that class. so if it is static it is ok but if it is not, you cant call it without any instance of that class.


Advantages of a static method:

There's no need to create an object first. The method is available immediately.

It's a good when you have generic functionality that does not depend on the state of a particular object. For example, look at the Arrays class or the Collections class from java.util.

Static methods can be good for factories. Pass your requirements as parameters, get a brand new object back. Not a constructor in sight.

Disadvantages of a static method:

You don't have an object instance, so you only have access to static members and your own local variables. If you want an instance, you probably have to create it yourself.

You can create subclasses, but a static method can't be abstract, so it's harder to decouple your implementation from a caller.


(ps: i dont think compiler will optimize if it is going to be static or not.. but not sure)

ufukgun
+14  A: 

As defined, power is stateless and has no side effects on any enclosing class so it should be declared static.

This article from MSDN goes into some of the performance differences of non-static versus static. The call is about four times faster than instantiating and calling, but it really only matters in a tight loop that is a performance bottleneck.

Jason
Where is the link?
niko
@niko: Amended. Sorry and thanks.
Jason
+1 for the msdn link.
Amby
Good link, but take it with a grain of salt as it's from 2003. For example, invoking a delegate is not 8x slower than invoking an instance method, but today it's closer to 2x slower.
JulianR
A: 

To me an easy question to decide is "Should I have to instantiate this object just to call this function". In the case of your function, I would say the answer is no, so it should be static. This method does not seem to be related to your object so again, I vote static.

Cody C
@Cody C: But how does one go about answering the question "Should I have to instantiate this object just to call this function?"?
Jason
IMO its just a matter of "Do I need an object with state if I want to use this functionality?" If I'm just instantiating an object to do a simple function, then that answer is typically no.
Cody C
+2  A: 

There should be a slight performance improvement if you declare the method static, because the compiler will emit a call IL instruction instead of callvirt.

But like the others said, it should be static anyway, since it is not related to a specific instance

Thomas Levesque
A: 

I believe the compiler will not optimise this into a static method. There is a performance gain since the pointer will not have to be checked to see if it is null at runtime. Have a look at the FXCop rule for reference.

AdamRalph
A: 

The compiler will likely consider inlining this when it "JITs" the code as it's so short and if it does so then will likely be able to optimise out any reference to the unused this parameter. But you can't rely on any of that.

You still have to make an object to call it on unless you make it static which has a much bigger overhead anyway if you don't need one for other reasons.

John Burton
A: 

My guess is that it wouldn't make a difference in performance. However it seems that if you have this confusion that your power method is probably in the wrong class. It should be in some sort of Math library, which would then determine what it should be: static.

emddudley
+2  A: 

Note that it is highly unlikely the compiler is even allowed to make that change on your behalf since it changes the signature of the method. As a result, some carefully crafted reflection (if you were using any) could stop working, and the compiler really cannot tell if this is the case.

jerryjvl
Good point. I didn't think about it, Thanks.
Elad