3

I use static methods for things I really MEANT to be static. I use ReSharper for better code quality. Sometimes ReSharper suggests that a method can be made static.

When I got the following class:

public class WhatEverClass {
    private string DoSomethingFancy(string input) 
    {
       string fancyStuff;
       // Fancy Stuff here
       return fancyStuff;
    }

    public WhatEverClass() {
       string awesome=DoSomethingFancy("some fancy string");
    }
}

ReSharper might say "DoSomethingFancy can be made static".

I know it could be made static, but is there a good reason to really do this? Or should I just ignore these suggestions?

4

6 回答 6

6

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.

于 2013-06-26T11:43:09.253 回答
1

If your method doesn't need to say or change the state of an instanciated object, then it should be static.

于 2013-06-26T11:45:29.407 回答
1

The usual notion is , if you are not creating an instance of anything, you could declare it static. As to where it should be used, ReSharper gives you suggestions based on standard programming practices. However, i take 'standard programming practices' with a grain of salt. Its a matter of personal programming preference for some. Here is a detailed reference on the topic :

http://msdn.microsoft.com/en-us/library/79b3xss3.aspx

于 2013-06-26T11:47:23.227 回答
0

Because you will invoke the WhatEverClass() method from outside the class by creating WhatEverClass instance. So the value for every instance will be different, because the variable is local, and will be created every time you create an instance of the class.

But if you want to keep the same value for all instances, then you can make it static so it will be created once in a memory and all instances will use it.

于 2013-06-26T11:44:38.090 回答
0

Beware the consequences of making a method static!

By making your method static, you make it that much harder for consumers to stub out your implementation of the algorithm and replace it with one of their own (obviously if the method is private you have no such worries).

Consumers of your static method have your implementation baked in to their code - they cannot use dependency injection to resolve a specific instance of your algorithm (without a bit of work). This makes their system that much harder to test, and in general lends itself to a less extensible code base.

于 2013-06-26T12:01:12.503 回答
-1

If the method DoSomethingFancy does not use anything in the object WhatEverClass then it should, in my book, be made static since it does not in fact have anything to do with the object in which it is used.

于 2013-06-26T11:43:22.970 回答