3 ms·
IMHO the main problem with mutable global state is - you don't know what caused it to have the value it has. The solution is traditionally access restriction (
by ajuc 9y ago
IMHO the main problem with mutable global state is - you don't know what caused it to have the value it has.
The solution is traditionally access restriction (OO) or immutability (FP), but it can also be - strict no aliasing rule.
Imagine if there were only 2 kinds of functions: pure functions (that can accept parameters have local cariables, and return values, but cannot change nor reference global state), and impure procedures (that cannot accept parameters and doesn't return anything, but can reference and modify global state, always starting from root object and going through all nested objects).
Then if you want to know ALL plaes which could have influenced or depended on the value root.objects.player.x - you simply search for that exact string.
You can start with all code using global state directly, and slowly move parts into pure functions. It can even be automated.