Static Variable - A Code Smell

Introduction

Static variable represents a global state so it is globally accessible. Also, it is not attached with any particular object instance. It is accessed with the name of class name and doesn't modify the state of the object. But, it has several disadvantages and excessive usage of it gives birth to code smell.

The usage of Static variable has several disadvantages, as mentioned below.
  • Less Object Oriented
  • Object Lifetime is very long
  • Difficult to write Unit test
  • Prevents from re-usability
  • Thread-Safety
  • Difficult to Serialize
  • It is not Re-Entrant
I will explain each term one by one and all these terms will prove that Static variable is a part of code smell.

Less Object Oriented

Static variable does not respect the encapsulation because object doesn't remain in the complete control of its state. It maintains a variable as Global which is known to everyone. Private Static variable also maintains state at a global level but restricts the access level to outside.
It doesn't follow concepts like Inversion of Control, Loose Coupling, Dependency Injection, etc. Hence, Static variable is less object oriented.

Object Lifetime is Very Long

Static variables remain in the memory for a long time and its garbage collection is very difficult. Excessive usage of static variables can result in the memory overflow. Static variables always remain in the memory whether these are being used or not. Developers don't have control on destroying or creating static variables.

Prevents from Re-usability

Static variables can't be overridden. These can't be used to implement an interface.

Difficult to write Unit Test

It is very difficult to write unit test for the static variables. It can't be easily mocked. Static Variable maintains state across instances hence making it more difficult to write unit tests for it. It is very difficult to isolate changes from static variable to single test.

Thread Safety

Static variables are not thread safe. Instance variables do not require thread synchronization unless shared among threads. But, static variables are always shared by all the threads in the process. Hence, access to static variable is not thread safe.

Difficult to Serialize

It is very difficult to serialize static variables. Serializers serialize instances and static variables do not belong to any instances so, it can't be serialized.

It is not Re-Entrant

"A code is called re-entrant if it can be interrupted in the middle of its execution, and then be safely called again before its previous invocations complete execution." - Wikipedia.

Static variable or Global variable are not advised to become re-entrant because the interruption can change the certain global values and after resuming, value may change .

Conclusion

Static variables are completely unnecessary and can be avoided. It has a habit to behave like a global variable. Static variable is not completely evil but it should not be overused because it has some evil behaviors. Sometimes, it is considered as beneficial if variable is required for a longer time in the memory. So, it should be used wisely.


Similar Articles