4 ms·
Because of spooky action at a distance. Consider the following code fragment: glob = 5; f(); // what is the value of glob here? The problem with global
by mdpopescu 7y ago
Because of spooky action at a distance.
Consider the following code fragment:
glob = 5;
f();
// what is the value of glob here?
The problem with globals is that you can't know. f might change glob, directly or indirectly, and there is no way you can keep in mind all possible changes (especially with multiple people working on the same codebase).
(The same problem can happen, on a more limited scale, with class fields - which is why some of us insist on requesting that classes are kept small and cohesive.)
Note that this does not happen as often with database values (which are also globals that can be changed from any point in the program) because of expectations. When using those, we have all kinds of mechanisms - like transactions and isolation modes - that let us specify how much we want a value we have written to stay like that until we're done with it; when we don't use those mechanisms we generally expect that "this value could change between one statement and the next".