Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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?

+5
−0

Many languages discourage global variables.

Why is this?

History

0 comment threads

Post
+2
−0

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:

  1. 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:

  1. (as before) which arguments you call it with
  2. which global variable foo uses
  3. how its value impacts foo’s behaviour (which, along with the previous point, invalidates black-box testing)
  4. the value of that global variable before calling foo; and
  5. you need to pray that nothing else changes its value unexpectedly between your check and when foo actually 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?

History

1 comment thread

Not limited to global variables (2 comments)
Not limited to global variables
Olin Lathrop‭ wrote about 1 month ago

Your point applies to any case where foo accesses additional state. This has nothing to do with that state being global or not. Consider the case of foo's job being to return a random number. It might keep internal private state that is altered each call. It might reference external inputs. None of these need to be global for the behavior of foo to be unpredictable.

Indi‭ wrote about 1 month ago

I believe I said that, quote, global state “among other things” would cause the same problems. That other things might also cause those problems does not change the fact that globals cause them.