Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are static methods good for scalability?

Does static methods and class are good for scalability ? I think so static class/method improves scalability of application and instance methods doesn't scales much. So is it good programming practice to write static method where ever it is possible ?

like image 634
Silent Warrior Avatar asked Aug 16 '09 16:08

Silent Warrior


People also ask

What is the advantage of static methods?

A static method belongs to the class rather than the object of a class. A static method can be invoked without the need for creating an instance of a class. A static method can access static data member and can change the value of it.

Why static methods are not recommended?

Static methods are bad for testability. Since static methods belong to the class and not a particular instance, mocking them becomes difficult and dangerous. Overriding a static method is not that simple for some languages.

Are static methods more efficient?

They are faster — Static methods are slightly faster than instance methods because in instance methods, you are also working with an implicit this parameter. Eliminating that parameter gives a slight performance boost in most programming languages.

Is it good practice to use static methods?

In short: Yes. There are many disadvantages and static methods should almost never be used. Static methods allow procedural/functional code to be shoe-horned into an Object Oriented world. Using static methods and variables breaks a lot of the power available to Object-Oriented code.


2 Answers

Does static methods and class are good for scalability?

One has little to do with the other.

I think so static class/method improves scalability of application and instance methods doesn't scales much.

Wrong. Why do you think so?

So is it good programming practice to write static method where ever it is possible?

No. In fact, it is very bad practice because it abandons the maintainability advantages of object-oriented programming.

like image 138
Michael Borgwardt Avatar answered Sep 18 '22 13:09

Michael Borgwardt


It depends on WHY the method is static. If it's static because it truly does not need context, then it will probably scale very well compared to something of similar complexity that is not static because it requires context.

However, if it is static merely because you cannot retain the needed context and must still pass it in, or because of some artificial goal of having more static methods, then I would suspect that it will actually scale LESS than the comparable method as non-static.

In fact I think that ASP Classic proved this point.

like image 44
RBarryYoung Avatar answered Sep 18 '22 13:09

RBarryYoung