I think 'don't' is a little harsh, here's a quote from Steve McConnell:
Used with discipline, global variables
are useful in several situations
I think just like a good carpenter has the right tool for the job and will use the right tool if the need arises, programmers should also use all of the tools at their disposal.
Straight from the 'Tour de Force' Code Complete are several reasons to use global data:
- Preservation of global values
- Streamlining use of extremely common
data
- Eliminating tramp data
McConnell also says:
Use Global Data Only as a Last Resort.
Before you resort to using global
data, consider a few alternatives.
here are the alternatives he lists:
- Begin by making each variable local
and make variables global only as you need to
- Distinguish between global and class
variables
- Use access routines
The things i've mentioned here get great coverage in the fantastic book Code Complete