Why are global variables a bad idea? | Sololearn: Learn to code for FREE!

+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?

9/29/2018 8:33:13 AM

Sonic

21 Answers

New Answer

+24

Anthony Inglimo yes. Globals reside in memory for the duration of the whole program.

+13

Eduardo Petry thanks. So use scope rather than OOP to solve this issue. I'll remember that.

+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?

+10

I agree Rick Shiffman.

+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)

+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.

+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.

+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.

+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).

+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.

+3

Global variables are a good idea. They help the code stay simple.

+3

Thank goodness I found this forum. My mum is not interested in big ideas for programming.

+3

Flash can you please help me with a problem

+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.

+3

because global variables can be change from any where in the program. And leads more chances to cause error.

+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.

+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.

+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.

+2

Nathan Johnson

+2

Global variables can be changed