Should You Avoid Using Static?
Tags: javascript, architecture, oop
Published: 2020-06-22 17:29:44

---

firstPublishedAt: 1592487040840
latestPublishedAt: 1592831843179
slug: should-you-avoid-using-static
title: Should You Avoid Using Static?

---

### You must have heard that static is considered bad or even evil, a complete disaster for your software architecture. But why? Surely there must be a trade-off between advantages and disadvantages, so let’s have a look.

<sup>*Originally published at [Meduim](https://medium.com/att-israel/should-you-avoid-using-static-ae4b58ca1de5)*</sup>

---

As a young junior software engineer, I was taught that static is a bad way of coding and that one should always avoid using it no matter what. Back then I didn’t understand why. I thought that static could be useful and more efficient. Over the years I learned what the disadvantages are, and I completely agree that **one should always avoid using static**.

---

In this article, I am using [Typescript](https://www.typescriptlang.org/index.html) for references and examples but the same idea can be applied for any other programming languages that support static.

Let’s break down the different use cases of static.

# 1. Static Methods

#### Classes with static and non-static methods.

This is the most common use of static that I keep seeing. Here’s an example:

{% gist https://gist.github.com/IsraelZablianov/ed9654eda2a3459367b6ecb79932ef93 %}

What is wrong with it?

- Static methods inside a class are usually a good indication of **a method that doesn’t belong to this particular class**. It doesn’t use the state of the class or other non-static members that the class has, and therefore, they break the [Single Responsibility Principle](https://en.wikipedia.org/wiki/Single-responsibility_principle).

- **Static methods are** **not [polymorphic](<https://en.wikipedia.org/wiki/Polymorphism_(computer_science)>)**. The definition of polymorphism is the use of a single [interface](<https://en.wikipedia.org/wiki/Interface_(computing)>) for entities of different [types](https://en.wikipedia.org/wiki/Data_type). So, by definition, static is not polymorphic. The Static method belongs to the class and not to the class instance, therefore you can’t achieve polymorphism with static.

- **Static methods** **can’t be used for** [abstraction](<https://en.wik**ipedia.org/wiki/Abstraction_(computer_science)>) and [inheritance](<https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)>). You can’t declare a static method in an interface or static abstract method in an abstract class. A static method cannot access non-static class level members, not its own, nor its base class. (Even though in TypeScript and Java, a derived class inherits its base class static members, it still doesn’t fit well as mentioned).

- **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. Even if you succeed, it will affect other tests that rely on the original implementation and lead to **mutations** that you didn’t expect to be.

---

Static methods aren’t associated with any object. It is even wrong to call them methods. They are functions. [What’s the difference between a method and a function?](https://stackoverflow.com/questions/155609/whats-the-difference-between-a-method-and-a-function)

**For the above reasons, it is easy to notice that static functions break the OOP concepts.**

---

Given all these downsides, surely there must be some advantages, some trade-off against all these disadvantages? Well, not really.

> One of the most common arguments is, why demand that consumers of our class create an instance for invoking this single method, while having no use for the instance afterwards? Creating an instance of a class is a very very cheap operation in most languages, so speed is not an issue. Adding an extra line of code to the consumer is a low cost for laying the foundation of a much more maintainable solution in the future. And finally, if you want to avoid creating instances, simply create a singleton wrapper of your class that allows for easy reuse.

---

# 2. Static Classes

#### Classes with only static methods.

In C#, for example, you even have the feature of a static class. Let’s look at an example:

{% gist https://gist.github.com/IsraelZablianov/110d15a24589f0c20e57ec0f185d2c81 %}

A static class can’t contain non-static members.

> A [static](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static) class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated.

> A [static](https://www.c-sharpcorner.com/article/when-to-use-static-classes-in-c-sharp/) class cannot be instantiated. All members of a static class are static and are accessed via the class name directly, without creating an instance of the class.

As for today in [TypeScript](https://www.typescriptlang.org/index.html) [v3.9](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html), we still don’t have this feature. We can achieve a similar behavior, for example, using a private constructor. In this article, I will refer to classes with only static members as static classes.

There isn’t much of a difference between a static and non-static class regarding the use of them. All the reasons mentioned above in “**Classes with static and non-static methods**” except the [Single Responsibility Principle](https://en.wikipedia.org/wiki/Single-responsibility_principle) still apply here.

There are some arguments in favor of static classes when it makes sense. Here are some examples:

> [A Math class with all static methods. Static classes are useful and provide an easy way to access its members that does not need to work differently for different objects.](https://www.c-sharpcorner.com/article/when-to-use-static-classes-in-c-sharp/)

> [App Configuration class that has all static settings about an app and the values of settings don’t change based on the objects or users.](https://www.c-sharpcorner.com/article/when-to-use-static-classes-in-c-sharp/)

> [A DatabaseConfig class that may have members such as database name, server name, port number, and even a connection string. We know that these values will not change for objects.](https://www.c-sharpcorner.com/article/when-to-use-static-classes-in-c-sharp/)

I don’t find these arguments very convincing. Those use cases can be easily done using singletons or constant variables, which makes it easier for future maintenance without getting the cons of static.

---

# 3. Static variables

#### Classes with static fields/properties.

{% gist https://gist.github.com/IsraelZablianov/ed9654eda2a3459367b6ecb79932ef93 %}

Besides all the cons that we mentioned above regarding static methods, static fields have more disadvantages and are considered by many as much worse.

- **Static variables represent a global state**. Without proper handling, a global state can be a nightmare. The modification of the state can be from everywhere, leading to unexpected mutations across your software and tests.

- **Memory leaks**. The [Garbage Collector](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management) is the memory manager of your app. Objects that are no longer referenced by any variable or collection are garbage collected. Static members are referenced by their classes and not by a particular object instance, and since the class itself won’t be [Garbage Collected](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management) even once you’re done using it, the memory of a static variable that referenced by this class cannot be garbage collected. **A static variable will persist for the lifetime of the app**.

---

# Summary

There are different uses for static and all of them have some serious disadvantages. In this article, I focused on **Static Methods**, **Static Classes,** and **Static Variables**.

The disadvantages I mentioned are:

- Single responsibility principle

- Polymorphism

- Inheritance

- Abstraction

- Testability

- Global state

- Memory leaks

There are more use cases and disadvantages for static and they are different from one programming language to another. In this article, I tried to bring the most common use cases I keep seeing.

For the use cases above I didn’t find a good enough reason to use static except for laziness :) or maybe a quick solution for a [POC](https://en.wikipedia.org/wiki/Proof_of_concept). Although there may be some different use cases for the static keyword, e.g., [extension methods](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods) in C# and [static nested class](https://www.javatpoint.com/static-nested-class) in Java.

In my opinion, one should always avoid using static.

I hope you enjoyed the read.

---

Extra articles and resources to look at:

- [https://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil](https://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil)

- [https://gbracha.blogspot.com/2008/02/cutting-out-static.html](https://gbracha.blogspot.com/2008/02/cutting-out-static.html)

- [https://www.beyondjava.net/should-you-avoid-or-embrace-static](https://www.beyondjava.net/should-you-avoid-or-embrace-static)

- [https://simpleprogrammer.com/static-methods-will-shock-you/](https://simpleprogrammer.com/static-methods-will-shock-you/)

- [http://misko.hevery.com/2008/12/15/static-methods-are-death-to-testability/](http://misko.hevery.com/2008/12/15/static-methods-are-death-to-testability/)