1

During a code review I presented a method quickly to the team that I had made static and one person agreed that there was no reason for it to not be static and a person disagreed saying that he would not make it static because it wasn't necessary and just to be on the safe side for future modifications and testing.

So I did quite a bit of research and obviously it's a specialized case but I would like to know what you would do in this situation and why?

(Its basically a helper method I call from a few different methods, a very low traffic page. More for my knowledge and learning on Static.)

private IEnumerable<Category> GetCategoryByID(int id, Context context)
{
    var categoryQuery = from selectAllProc in context.SelectAll_sp()
                        where selectAllProc.CategoryID == id
                        select selectAllProc;
    return categoryQuery;
}
Adam
  • 3,615
  • 6
  • 32
  • 51
  • 2
    What about an extension method to the type of `context`? – mellamokb Apr 02 '12 at 22:50
  • 2
    The method is `private`; I don't see how making it static would make anything more difficult. – Paul Phillips Apr 02 '12 at 22:52
  • This is a bit subjective and argumentative. But according to some guidelines, if you find yourself passing the same values/references to objects into many instance methods, make them private fields instead. I don't see any reason as to why you would want to have it static if it's private. – Filip Ekberg Apr 02 '12 at 22:54
  • 1
    There is annoying warning in Resharper just for your case. Suggest you to read about making methods static http://stackoverflow.com/questions/169378/c-sharp-method-can-be-made-static-but-should-it – Sergey Berezovskiy Apr 02 '12 at 22:58

3 Answers3

5

Making private methods static is a form of micro-optimization; the method call is slightly faster. But the difference is almost too small to be meaningful.

Generally speaking, you should mark a method static when it:

  1. Doesn't interact in any way with instance members, and
  2. You would like to have the ability to call it without instantiating the class, as in Class.Method()

Ordinarily, methods like your example would go into their own static helper class, if they are used in more than one place.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
1

If I were you I would ask my self the following questions.

Is it something which is related to type or instance of type?

If the answer is yes, I would be slightly inclined to make it static else, make it non static.

If you can give us some more information, the community can come up with some good options.

Sandeep
  • 7,156
  • 12
  • 45
  • 57
0

The first remark that comes to my mind is that by declaring this method static and possibly using it in multiple places in your code you are introducing a Service-locator kind of dependency.

As far as I know the main problem with it is that implicit dependencies are introduced, i.e. they can't be inferred by looking at method signatures.

As a consequence it can be much harder to assess the impact of a modification of your static method on the rest of your system.

Andrea Scarcella
  • 3,233
  • 2
  • 22
  • 26