Get the latest tech news

Global variables are not the problem


In this article I'll show an example where avoiding a global variable has led to a bug, I'll define what global variables are, explain the problem, and then give examples where I have used them successfully. Global Variables Are Not the Problem We're all taught that global variables are bad.

They can be modified from anywhere, sometimes force functions to be called in a specific order, and can be impossible to debug if the program is large enough or the state is random enough. Such as a logger verbosity level, or a variable to enable recalculation of some data so we can detect if logic is wrong. Beginners might be lazy and use a global variable instead of changing the signature of a dozen functions, which blows up in their faces when they overwrite a value they need.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of problem

problem

Photo of global variables

global variables

Related news:

News photo

Live AI on Meta’s smart glasses is a solution looking for a problem

News photo

People are bad at reporting what they eat. That's a problem for dietary research

News photo

Let the user help solve their own problem