Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on Why is global evil?
Parent
Why is global evil?
Post
Another reason that, sadly, very rarely gets mentioned is testing. Global variables make testing hard, and in some cases, functionally impossible.
Any time a logical code entity—whether a class, a function, a module, whatever—is self-contained, it can be tested in isolation. The tests will also be completely deterministic.
Consider a function foo that is “pure”, meaning (among other things) it uses no global state whatsoever. In order to know what that function will do, you need to know:
- which arguments you call it with.
That’s it. That’s everything you need to know to have perfect knowledge of how foo will behave.
That means that once you know (via testing) that foo returns the correct answer for a given set of arguments, you know it will ALWAYS return the correct answer for that set of arguments. If foo(42) returns 69 today, it will return 69 tomorrow, and the day after, and every day until the heat death of the universe.
If you test foo with the entire set of arguments that an application will use, and they all give correct results, then you have confirmed with absolute certainty that foo works (at least for that particular application). No matter how complicated the system gets, no matter what else is going on, you know that foo works. You can bet lives on it.
Now imagine that foo uses some global variable. Let’s say just one. How bad could that be?
Suddenly, all bets are off. foo(42) may return 69 this time… but the next time, who knows? (For example, imagine foo(42) returns 69 if the global variable is even, and 96 if it is odd; you can’t know what you’re going to get unless you also know what the global variable is, and how foo is using it.) To regain anything even approaching certainty, you need to know:
- (as before) which arguments you call it with
- which global variable
foouses - how its value impacts
foo’s behaviour (which, along with the previous point, invalidates black-box testing) - the value of that global variable before calling
foo; and - you need to pray that nothing else changes its value unexpectedly between your check and when
fooactually uses it.
You see how quickly things got difficult once global variables got involved?
It is not necessarily impossible to once again confirm with certainty that foo is works. But it is a lot harder, and, even worse, it continues to get harder still as the system gets more complicated. Because the more complicated the system gets, the more chance that the global state will be changed in some unexpected way, at some unexpected time. Testing for all the possibilities becomes increasingly challenging, if not functionally impossible. Which means foo becomes increasingly unpredictable and untrustworthy. Would you still be willing to bet your life on it?
All we did was add a single global variable. 🤷🏼
Maybe that’s something that should be avoided if possible, hm?

0 comment threads