3 ms·
One of these — global shared mutable state - is almost always horrible bad practice and leads to unwieldy code that is hard to test or compose with other code.
by api 5y ago
One of these — global shared mutable state - is almost always horrible bad practice and leads to unwieldy code that is hard to test or compose with other code. It’s also a major culprit in bugs that arise when shared objects are used in multithreaded programs. I practice the rule that shared variables are always const and use e.g. const_cast if they need to be lazily initialized. I also make sure to make any lazy initialization thread safe.
There are very few cases when a global mutable is good. Usually these arise in things like embedded or kernel code that by definition is only run as a single instance or if you need something like a global random number generator or global singleton access to something by definition unique.
Even in the latter cases it makes me itch a little.