Why are global variables a bad idea? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 24

Why are global variables a bad idea?

Is it because in large scale software, a developer can modify a global and cause an unexpected change in the application? How does Object Oriented software solve this problem? Through encapsulation and getter/setter methods? If so, do the variables that replace globals have to be class or static variables and not instance variables?

29th Sep 2018, 8:33 AM
Sonic
Sonic - avatar
21 Answers
+ 24
Anthony Inglimo yes. Globals reside in memory for the duration of the whole program.
29th Sep 2018, 5:09 PM
Sonic
Sonic - avatar
+ 13
Eduardo Petry thanks. So use scope rather than OOP to solve this issue. I'll remember that.
29th Sep 2018, 5:10 PM
Sonic
Sonic - avatar
+ 12
Sharon I think global variables are only a good idea for small programs, especially those that won't be maintained by many developers over a long period. Also was that comment about your mom a joke? 😊 What do you mean by big ideas,... global variables?
30th Sep 2018, 6:18 AM
Sonic
Sonic - avatar
+ 10
I agree Rick Shiffman.
30th Sep 2018, 6:16 AM
Sonic
Sonic - avatar
+ 7
Scope solves this problem by making variables local to certain blocks of code (no need of OOP). Encapsulation serves a different purpose: it hides data inside a class so its state can't be accessed by an object from the outside. https://en.m.wikipedia.org/wiki/Scope_(computer_science)
29th Sep 2018, 10:02 AM
Eduardo Petry
Eduardo Petry - avatar
+ 6
Globals also pollute the namespace so you may run into different kinds of problems with them. If you use IDE it will show dozens of global variables that will obstruct the search of a needed one.
29th Sep 2018, 9:01 AM
WittyBit
WittyBit - avatar
+ 5
Do global variables also have to sit in memory for the running of the whole program? Like defining a variable in a function means that the variable will only exist while the function is run.
29th Sep 2018, 11:24 AM
Anthony Inglimo
Anthony Inglimo - avatar
+ 5
If you aren't vary careful one function change the value of a global variable out from under an other function in your program, this is even worse in a multi-threaded code.
30th Sep 2018, 6:10 AM
Rick Shiffman
Rick Shiffman - avatar
+ 4
Global variables are not bad in themselves, but they should be used with caution. In Python, one use of a list is to append variables from the internal workings of a function without altering its return statement (or even invoking it for that matter).
30th Sep 2018, 6:05 PM
Jonathan Shiell
+ 4
The problem with non-constant global variables is merely this: *state* is the root of all evil. When you call a function like sqrt(), you expect the result to depend only on the arguments you pass in, and not to have any side-effects (excepting IO functions). If a function is influenced by globals outside that is bad design. If a function reads the global "outfile", and outputs to it, this is generally bad. What if you want to write to a different file. Then you have to modify outfile for the duration of that function call. What then if you have multiple threads and some of them actually want to write to outfile? The paradigm that respects these common sense realisations is called functional programming, and while some languages are built around, you can write functional-style code in almost any programming language. OOP only solves the problems superficially, but you get much worse problems down the road.
25th Oct 2018, 1:39 PM
Vlad Serbu
Vlad Serbu - avatar
+ 3
Global variables are a good idea. They help the code stay simple.
30th Sep 2018, 12:57 AM
Sharon
Sharon - avatar
+ 3
Thank goodness I found this forum. My mum is not interested in big ideas for programming.
30th Sep 2018, 12:59 AM
Sharon
Sharon - avatar
+ 3
Flash can you please help me with a problem
30th Sep 2018, 7:04 PM
Junie B.
+ 3
They're not a bad idea if they are used correctly. Using them as a way of maintaining very critical information required by the whole program can make them super useful. However they should not be used in a lazy fashion to move data around. They would not exist if they did not serve a useful purpose.
30th Sep 2018, 8:27 PM
Nathan Johnson
Nathan Johnson - avatar
+ 3
because global variables can be change from any where in the program. And leads more chances to cause error.
30th Sep 2018, 9:00 PM
Muhammad Hashir Mohsneen
+ 2
Right Sonic. Global variables should be used collaborative projects, as they can be listed at the top of the script, like in Java Google Fit, so you know immediately what the important variables are. The global variables can then be modified by functions anywhere in the script. I so detest collaborative programming. It is really difficult to understand another person's code. I did not joke about my mum. I do get big ideas in programming. I have an ambitious project called Stackpirt, which is an artificial intelligence for stocks and shares. I don't think it is ever going to be finished, though.
30th Sep 2018, 7:44 AM
Sharon
Sharon - avatar
+ 2
Right Sonic. Now about joking. It is important to enjoy programming. Otherwise, how do you keep going? Encapsulated variables, Eduardo Petry, allow you to control access to variables. You can even give variables the same name as each other, in different functions.
30th Sep 2018, 7:55 AM
Sharon
Sharon - avatar
+ 2
Java Google Fit imports libraries at the beginning of the code, sorry. It has nothing to do with global variables. You can go find it if you want. GitHub is easy to navigate these days. Er, is it libraries or dependencies? I meant bits of code from Google that you import, preceded by the import keyword.
30th Sep 2018, 8:13 AM
Sharon
Sharon - avatar
1st Oct 2018, 12:26 AM
Junie B.
+ 2
Global variables can be changed
20th Dec 2018, 2:09 AM
Rasmitha N