TL;DR
The problem with a global state is that most of the time you discover that it shouldn’t be so global.
A global variable is like a dark alley that you use as a shortcut to get somewhere more easily, but you never know when you’ll be surprised by something unexpected. ;)
Problems with variables and global states
Using variables to share data
Many developers use a global object or variable to share something between different components of a system.
This is done either by laziness or by failing to adequately structure the dependency relationship between components so that they can more adequately limit the scope of what they want to share.
I’ve seen several cases where the developer decided not to use parameters, so he made a logic like this:
var valorGlobal;
function rotina1() {
valorGlobal = 1;
rotina2();
}
function rotina2() {
alert(valorGlobal);
}
The equivalent with the use of parameters is:
function rotina1() {
rotina2(1);
}
function rotina2(valorLocal) {
alert(valorLocal);
}
As you can notice, the chance of some error on the part of the developer in the first case is much higher.
Code spaghetti
Continuing with the example and several methods start using this global variable, sooner or later the code will be indecipherable.
For example:
var valorGlobal;
function rotina1() {
valorGlobal = 1;
rotina2();
}
function rotina2() {
valorGlobal = 2;
rotina3();
alert(valorGlobal); //qual o valor aqui?
}
function rotina3() {
valorGlobal = valorGlobal + 1;
}
The abuse of global variables seems to cause an effect similar to the indiscriminate use of goto
. You simply get lost in the order of execution and what the code really wants to do.
Competition and performance
Think of the above examples performed in parallel processes. It would be the chaos on earth!
Changes to global variables are problematic because they need to be synchronized. But synchronization costs expensive and can create bottlenecks in a system multithreading.
The more limited the scope of states, preferably a single object, the more efficient the code.
Scope, scope, scope, scope
At the beginning of a project, when there are still few classes, code, functionalities and, consequently, little confusion, encapsulating data seems unnecessary. Therefore, global states are extremely tempting to be simpler to implement.
However, in the course of the project, what is usually discovered is that it will be necessary to repeat a lot of code because what is already done has been "tied" with global variables and new functionalities require new global variables.
In short, it turns out that what at first seemed to be useful to the whole system is actually not. The scope needs to be limited, but the use of global variables does not allow this.
That’s pretty close to what it was discussed about Singleton. For example, if we have a routine that saves reports in a folder whose configuration is global and now it is necessary to save only one of the reports in another folder, we will have to change the existing routine or duplicate it.
When global variables are useful and necessary
There are cases where it is really necessary to use global variables.
The most obvious case I can think of is for the development of frameworks or libraries.
Frameworks usually break several Object Orientation rules (à la Matrix), such as changing private attributes of objects and maintaining many global states.
A Dependency Injection framework, for example, must maintain a global map with the dependencies it manages. The difference is that this mechanism will be extremely well thought out and tested.
Completion
The use of global states should be avoided at all costs in development considered "normal", that is, it has to do with the ordinary functions of a system.
However, for non-functional functionalities, frameworks and libraries, which implement very specific use cases, usually have well-defined scope and are developed by more experienced people, the use of global states is acceptable and sometimes even necessary.
possible duplicate of Why using global variables is not a good practice?
– talles
It’s not duplicate. Global status and global variables in Javascript are different things. The problem is much more complex than the one addressed in the other question in a specific way. The answers here if given there would not make much sense and the answers posted there would explain part of the problem and in a way that does not fit here. http://meta.pt.stackoverflow.com/questions/607/lidando-com-perguntas-duplicadas/609#609
– Maniero