7

How often should I use static methods generally? If I have like:

Class1 _class34 = new Class1(parameter);

Class1.DoSomething(_class34, parameter1, parameter2, parameter3, parameter4).

or

_class34.DoSomething(parameter1, parameter2, parameter3, parameter).

I'm having a tendency of calling static method of a class and passing an object of the class like in the first example?

What is the best practice concerning these two examples? Is there any performance, design and general practices things I should pay attention to? Which one should I use generally and which one would you choose like in every day coding scenarios. The first example seems more simple to read (you pass all the parameters and do something), in the second you have to read twice that you are working on an object?

It is not really a big deal, just wondering.

7 Answers 7

11

Generally speaking, static methods should only be used when whatever you want to do is independent of any one instance of the class. If you need to directly access or affect the state of a particular instance, a non-static method is usually the way to go.

Sign up to request clarification or add additional context in comments.

3 Comments

Agreed. Of course there are always exceptions but, in general, if you find yourself passing an instance of class A to a static method of class A, you're probably doing it wrong.
What about in cases where you are passing two instances of Class A to a method where neither instance 'owns' or takes precedence in the operation?
You mean like a commutative binary operation? I've seen it both ways, but would probably still use a non-static method.
2

There's no "more often" answer.
It all depends on the type of usage.

The bottom line is: If an object of the specified class is effected/used, you should always use non-static methods. However, if there's no single instance of the class that is effected/used, you should always use static methods.

In your example you are doing Class1.DoSomething(_class34, parameter1, parameter2, parameter3, parameter4) which is not a good approach, as it strips away all the possibilities Object Oriented programming gives you(such as polymorphism, etc.).

A good example for a scenario where a static function would be needed is factory methods such as String.Parse - which begin without any specific instance of String, yet are connected to the string class.

1 Comment

Another common example is loading an object from a file - that is often implimented as a static returning the object loaded from the file.
2

This is what the runtime already does, every instance method has a hidden 1st argument that passes this. Exposed in the syntax of an extension method.

Explicitly doing the work of the runtime is not particularly useful, the syntax just gets more verbose. And painful given the kind of name you have to come up with. Consider _this instead of _class34 :)

Comments

0

if you need the object, i think you should make the call like "_class34.DoSomething(parameter1, parameter2, parameter3, parameter)."

when you read it is like: Over the object _Class34, do Something, with this parameters.

Comments

0

The whole idea behind object oriented programming is that you don't have to write somefunction(somestruct) as you would have to do in the past, but you can write somestruct.somefunction() instead, which makes it much clearer that somefunction belongs to somestruct.

Of course you can do it any way you want, but remember that your second example is the way, the reason that member functions were invented.

Comments

0

Static members may complicate unit testing so you have to be careful about how you use them.

For most of the cases, I end up adding non static wrappers for classes containing static members since otherwise I wouldn't be able to mock them.

Static Methods are Death to Testability might give you a better idea of this.

Comments

0

But by default I use instance methods. I tend to use statics only when it makes more sense than instance methods or when there is no other way. Using statics may complicate unit testing. Static methods which also use static fields are a potential source of memory leaks (yes, even in .NET).

So for example I use statics when I want to have a special creation method to make more clear what happens during creation, like when I create object by deserializing it from a file:

MyClass myObject = MyClass.ReadFromFile("serialized.xml");

Which is more readable than:

MyClass myObject = new MyClass("serialized.xml");

Also I use statics methods (and members) when I want to share some class-wide state information between all instances.

Of course static member are a must when your whole class is static.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.