Why is using global variables not good practice?

Asked

Viewed 3,888 times

42

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

  • 3

    Just a curiosity: are evil is some expression used in Brazil with a negative connotation?

  • 5

    @Zuul Yes, it would be like "globals are evil"

  • There is additional information on the subject in the tag [tag:variables-global]

3 answers

42


Difficult understanding

A variable makes more sense when declared and used next to the code that manipulates it. Often in a global context it is difficult to understand (completely) the role of that variable.

Coupling

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

Rules on access and competition

With everyone accessing the variable it is difficult to force constraints on it and, mainly, coordinate multiple-threaded access.

Conflicts of namespace

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

With the word, Crockford

Follow an excerpt from the book that you quoted:

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 isn’t just that it Allows them, it requires them. Javascript does not have a Linker. All Compilation Units are Loaded into a common global Object.

  • I think his fourth point should be the first - at least the quote from Crockford is all about him, and the question stems from that author’s position.

  • @bfavaretto, the points I quoted are not in any specific order. And I only added the quote from Crockford for illustrate the answer, the points are mine same ;)

20

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 someone else who also uses a global of the same name, one code will interfere with the other. Bugs will appear, and until you understand the reason, much 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 the B script will reset the your counter to zero - because it is not only his...

Therefore the recommendation is to avoid to the maximum the number of global variables in your code, reducing it to zero if possible, or creating a single global with an object representing a namespace yours, and hanging properties on this object. Thus, increase the chances of your code coexisting in harmony with the code of others.

  • 1

    "creating a single global with an object representing your namespace, and hanging properties on that object" good observation! Sometimes it is inevitable to use global, but it is always good to keep them to a minimum.

  • @bfavaretto no problem in referring to the title of the question in reply comment, Chapeleiro :) Anyway I removed the comment.

  • I like the "Cracker," @Diego! :)

2

Reflection of an inadequate design (design) (a possibility)

As the answer in Alternatives for global variables, the suspicion is that the design is adding an accidental complexity. In other words, the domain of the problem in question does not require a design for which the implementation naturally makes use of global variables. Instead, the design accidentally adds such complexity at the designer’s option.

When global variables are employed, or global data in general, multiple modules, routines or classes, or whatever the name of snippets of the code in question, are referencing such global data. As a result, the change in the structure of such data, for example, tends to generate a cascading effect of changes. The code relevant to what such global data represent is not localized but dispersed.

The above paragraph presents a reason to avoid global data, especially of course whether they were introduced due to an accidental complexity of the design.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.