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?
9/29/2018 8:33:13 AMSonic
21 AnswersNew Answer
Anthony Inglimo yes. Globals reside in memory for the duration of the whole program.
Eduardo Petry thanks. So use scope rather than OOP to solve this issue. I'll remember that.
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?
I agree Rick Shiffman.
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)
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.
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.
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.
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).
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.
Global variables are a good idea. They help the code stay simple.
Thank goodness I found this forum. My mum is not interested in big ideas for programming.
Flash can you please help me with a problem
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.
because global variables can be change from any where in the program. And leads more chances to cause error.
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.
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.
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.
Global variables can be changed