Global Variables Considered Harmful
Prof. William Wulf and Mary Shaw wrote a paper of this title in 1973, although I couldn't find the original paper online I did find some references.
Here are the key disadvantages:
- Side effects - hidden access
- Indiscriminant access - can't prevent access - may be difficult to make changes later
- Screening - may lose access via new declaration of variable
- Aliasing - control shared access to prevent more than one name for reference variables.
Backgound: I have been adding unit tests to existing stand-alone code to support refactoring and eventual migration to our current framework. The code is full of global variable references. Not only that, but work-arounds have been developed that depend on side effects caused by the use of a single global to mean more than one thing depending on which code ran last.
Because of all the things identified by Prof. Wulf I cannot be sure that if I change anything the code will still run as originally created. However, I must change things to make them testable. Luckily I do have access to other developers who have worked on the application (just fix the bug, don't change anything else) for some level of confidence about what is supposed to happen. Also, I don't have any refactoring tools (VB6) so all changes must be tediously made by hand with great attention to detail. What fun!