Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good or bad practice to make stateless methods static?

If a class method doesn't rely on the class' state then it can be static. Is it good or bad practice to make such methods static where this is the case?

(Tagged as C# but probably applies to many OO languages where methods must be class members.)

like image 258
Mr. Boy Avatar asked Jan 05 '17 14:01

Mr. Boy


People also ask

Is it good practice to make methods static?

Static methods are fine in most situations where the singleton pattern gives too much flexibility. For example, take a simple utility such as raising a primitive to a power - obviously you never need to have any polymorphism in that.

Is it bad practice to use static methods?

Static methods are bad for testability. Since static methods belong to the class and not a particular instance, mocking them becomes difficult and dangerous.

Are private static methods bad?

private or public doesn't make a difference - static methods are OK, but if you find you're using them all the time (and of course instance methods that don't access any instance fields are basically static methods for this purpose), then you probably need to rethink the design.

Which is better static or non static method?

A static method can access only static members and can not access non-static members. A non-static method can access both static as well as non-static members. Static method uses complie time binding or early binding. Non-static method uses run time binding or dynamic binding.


2 Answers

Visual Studio Code Analysis and ReSharper suggest to make those methods static, because there is a tiny performance advantage:

From documentation:

Members that do not access instance data or call instance methods can be marked as static (Shared in Visual Basic). After you mark the methods as static, the compiler will emit nonvirtual call sites to these members. Emitting nonvirtual call sites will prevent a check at runtime for each call that makes sure that the current object pointer is non-null. This can achieve a measurable performance gain for performance-sensitive code. In some cases, the failure to access the current object instance represents a correctness issue.

For non "performance-sensitive" code this is a matter of taste. I personally obey to ReSharper's suggestions if I have no good reason to not do so.

like image 190
René Vogt Avatar answered Oct 04 '22 14:10

René Vogt


Is it good or bad practice to make such methods static where this is the case?

Honestly, there is no way to say if 100% this is good or bad practice. A number of people follow the general rule that if it can be made static then do so. It shows that there isn't a requirement on state, and technically (at least in C#) it is a little faster.

That being said, it all depends on the composition of the code around the methods, and how the application is going to evolve.

like image 33
kemiller2002 Avatar answered Oct 04 '22 15:10

kemiller2002