On global and current states
Global state is evil – this is a typical phrase with regard to any usage of global variables or shared mutable state in software programs. It’s hard to argue against this statement. Here are the most notable drawbacks of using globals:
- Global state introduces hidden dependencies between application components (and abstraction layers).
- Mutable global state makes code execution unpredictable, since it can be changed outside the current context. In multithreaded environments, that can lead to bugs due to race conditions.
- Understanding and testing code relying on globals is more complicated.
Doesn’t this mean we should avoid global state as much as possible? The answer depends on what your goal is – building software products or creating ideal code (whatever that means for you). Ruby on Rails is a framework to build web products; thus, it can afford to use unpopular patterns to improve developers’ productivity...