Resharper Complains When Method Can Be Static, But Isn'T

ReSharper complains when method can be static, but isn't

I find that comment very useful as it points out two important things:

  1. It makes me ask myself if the method
    in question should actually be part
    of the type or not. Since it doesn't use
    any instance data, you should at
    least consider if it could be moved
    to its own type. Is it an integral part
    of the type, or is it really a general
    purpose utility method?

  2. If it does make sense to keep the
    method on the specific type, there's
    a potential performance gain as the
    compiler will emit different code
    for a static method.

Why does resharper suggest const, static operations?

When compiler encounters a static method it emits call instructions but when it encounters an instance method it emits callvirt instruction. Now the callvirt instruction checks the object whether it is null before making the call. so there is a performance penalty attached to it .But it helps in making the method call polymorphycally.

so if the method is not associated with a change of state of any property of the class it is advisiable to make that method static as it improves the peformance

Regarding the use of const it is a compile time association of the value rather than at runtime. so all variables of the const get replaced by the value at compile time itself which obviously improves the performance.

Method can be made static, but should it?

Static methods versus Instance methods


Static and instance members of the C# Language Specification explains the difference. Generally, static methods can provide a very small performance enhancement over instance methods, but only in somewhat extreme situations (see this answer for some more details on that).

Rule CA1822 in FxCop or Code Analysis states:

"After [marking members as static], the compiler will emit non-virtual call sites to these members which will prevent a check at
runtime for each call that ensures the current object pointer is
non-null. This can result in a measurable performance gain for
performance-sensitive code. In some cases, the failure to access the
current object instance represents a correctness issue."

Utility Class

You shouldn't move them to a utility class unless it makes sense in your design. If the static method relates to a particular type, like a ToRadians(double degrees) method relates to a class representing angles, it makes sense for that method to exist as a static member of that type (note, this is a convoluted example for the purposes of demonstration).

Can't make a method static in ReSharper because menus are greyed out

Please check that your method is not virtual and is not used to implement an interface. Otherwise, please provide a code sample.

Good reasons for static methods?

By defining a method static, so a procedure that computes something, you manifest an intent to a consumer of your API about statelessness of your function.

When we use static function, we do not expect it saves a state of computation or some computed internal value somewhere in it's internal static private variables, so the next call to that function may have different result even with the same parameters passed during the first call.

In short: whenever you see a function that just executes an action over parameter and not preserve some state, it is a good candidate for making it static.

Good reasons for static methods?

By defining a method static, so a procedure that computes something, you manifest an intent to a consumer of your API about statelessness of your function.

When we use static function, we do not expect it saves a state of computation or some computed internal value somewhere in it's internal static private variables, so the next call to that function may have different result even with the same parameters passed during the first call.

In short: whenever you see a function that just executes an action over parameter and not preserve some state, it is a good candidate for making it static.

Explaination for To static method invocation ReSharper suggestion

There are no difference between the cases. That is actually what happens with an extension method under the covers. It is a matter of personal preference, although direct invocation is less commonly used.

Should I always make my methods static where possible?

Static isn't evil. Static is evil if used incorrectly, like many parts of our programming toolkit.

Static can be very advantageous. As the accepted answer here points out, static can have a potential speed improvement.

As a general rule if the method isn't using any fields of the class then its a good time to evaluate its function, however ultimately utility methods that can be called without instantiating an object can often be useful. For instance the DirectoryInformation and FileInformation classes contain useful static methods.

Edit

Feel obligated to point out that it does make mocking a lot harder but it is still definitely testable.

It just means you need to think harder about where static methods go, so that you can always test them without needing to rely on a mock/stub. (ie: don't put them on your DTO that requires a persistent connection to the database).

Should we always convert private method to private static?

Private static is good when the method does not rely on the object's fields.

If it does, ReSharper doesn't suggest anything like that.

So when your method depends only on it's parameters, it's good to make it static.

A compiler can take advantage of such methods - it was explained somewhere here on SO.

Resharper complains with a Path Error when using extensionless links

It is possible to suppress the error.
ReSharper -> Options -> Code Inspection -> Inspection Severity -> HTML -> Potential Code Quality Issues -> Path error

I don't think that a wildcard mapping possible is.

But maybe the Resharper Community Forum can help you.



Related Topics



Leave a reply



Submit