1

When its recommended to use a private static method instead of a private [instance] method?

EDIT: i am looking for a good (or best practice) about that .i am wondering Is this technic used by microsoft or not?does anybody know something about that ? i coudnt find any blog ,article or sample source code that explains this topic.

any help would be highly appreciated.

siamak
  • 669
  • 1
  • 10
  • 28
  • Other similar questions: [here](http://stackoverflow.com/questions/169378/c-method-can-be-made-static-but-should-it/169423#169423) and [here](http://stackoverflow.com/questions/731763/should-c-methods-that-can-be-static-be-static) – oleksii Jul 23 '11 at 16:06

1 Answers1

3

Static word in the beginning of the method declaration, basically is a sign of stateless, so what is happening inside is a pure action, or at least should be.

If you want to use private static: use it like API functions of your class that just make some calculations/reports... and not change the actual state of the object, which basically is done by instance methods.

This is an expected way of implementing, which doens't mean that is mandatory, but as it's expected, it will help other developers understand your code, and help understand code to you after a couple of years, when you will come back to your project and already have forgot everything.

Regards.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • 1
    `static` methods often have side effects, on static variables or method arguments. It's no sign of "statelessness" whatsoever. – Ben Voigt Jul 23 '11 at 15:56
  • @Ben: infact, I wrote that this is expected behaviour, but not mandatory. You always can pass to a static method instance of class which changed inside of that method. Matter of design choice. But in general, at least me, when looking on static method, I immagine that it SHOULD be something that doesn't affect state. – Tigran Jul 23 '11 at 15:58