Why using global variables is not a good practice?

34

I'm reading a JavaScript book called "JavaScript The Good Parts" and in it several times the author says that one should not use global variables because they are "evil". Why are they considered "evil"? What kind of problems can they cause?

    
asked by anonymous 23.12.2013 / 21:57

2 answers

35

Difficult understanding

A variable makes more sense when declared and used next to the code that handles it. Often in a global context it becomes difficult to fully understand the role of that variable.

Coupling

When different parts of your code access this same global variable you end up coupling a large part of your code, making it difficult to modularize (reuse only that "piece" of your code).

Rules of access and competition

With everyone accessing the variable it becomes difficult to enforce constraints on it and especially coordinate multi-threaded access.

Namespace conflicts

The names of the global variables will affect your entire namespace. This ends up polluting the code because you will often be required to use another name or specify the name completely (with the namespace), which you would not normally do.

With the word Crockford

Here is an excerpt from book that you cited:

  

Because a global variable can be changed by any part of the program at any time, they can significantly complicate the behavior of the program. Use of global variables degrades the reliability of the programs that use them.

     

Global variables make it harder to run independent subprograms in the same program. If the subprograms happen to have global variables that share the same names, then they will interfere with each other and likely fail, usually in difficult to diagnose ways.

     

Lots of languages have global variables. For example, Java's public static members are global variables. The problem with JavaScript is not just that it allows them, it requires them. JavaScript does not have a linker. All compilation units are loaded into a common global object.

    
23.12.2013 / 22:13
14

The main problem is name collisions. If you write a code that depends on a global variable qualquer , and later includes a script made by another person that also uses a global one with the same name, one code will interfere with the other. Bugs will appear, and until you understand the reason, a lot of time will be lost.

Example:

Script A

var contador = 0;
setTimeout(function() {
    console.log(contador++);
}, 1000);

Script B

setTimeout(function() {
    contador = 0;
}, 5000);

Every 5 seconds, script B will reset your counter to zero - because it is not just your ...

So the recommendation is to avoid the maximum number of global variables in your code by reducing it to zero if possible, or by creating a global single with an object representing a namespace , and hanging properties on this object. This increases the chances that your code will live in harmony with the code of others.

    
23.12.2013 / 22:05