Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Variables whose lifetimes are longer than necessary have several drawbacks:
They make the program harder to understand and maintain: For example, should code update the module-wide path string if it only changes the current drive?
They pollute their context with their name: As a direct consequence of this, namespace-level variables, which are the most visible of all, are also the worst (see Item 10).
They can’t always be sensibly initialized: Never declare a variable before you can initialize it sensibly. Uninitialized variables are a pervasive source of bugs in all C and C++ programs, and they require our proactive attention because they can’t always be detected by compilers (see Item 19).