1

I'm new to programming and I'm quite confused why some people are insisting that global variables are bad. I'm currently writing a program with several classes and a lot of checking and rewriting data stored in variables. I found global variables extremely helpful since I don't need to create a getter and setter for each of the local or possibly class variables. By using global variables as bridges between different classes, I found it surprisingly easy to exchange information between those classes.

But again, I'm quite new to programming and I would be much appreciated if someone could explain to me why this is not advisable to do.

Edit: My apologies! I didn't know that global variables and public class variables are completely different and apparently I misidentified public fields as global variables. You never know how silly of a mistake a beginner programmer can make Lol. Thanks everyone for bringing this to my attention.

Anshul Sharma
  • 1,018
  • 3
  • 17
  • 39
Echidna
  • 164
  • 11
  • 1
    Are you asking about why people [use getters and setters](https://stackoverflow.com/questions/1568091/why-use-getters-and-setters-accessors) instead of just making the fields public? Public fields are not really the same thing as global variables. – that other guy Aug 06 '20 at 23:43
  • Global variable have been discouraged since structured programming became a thing. That was a couple decades before Java. – NomadMaker Aug 06 '20 at 23:55
  • 1
    I think you mean Javascript. These are two different languages. You can't really have a global property in Java. Every property is defined within the scope of an object (or a class in the case of static props.) – Nate T Aug 07 '20 at 00:01
  • There are no global variables in Java. Unclear what you're asking. Also unclear how this could possibly be a duplicate of a question about Python that has itself been closed. – user207421 Aug 07 '20 at 00:15
  • @MarquisofLorne While the duplicate _question_ specifically mentioned Python the _answer_ is language-agnostic. And while there's no true global variables in Java you can get close with public, static, non-final fields. – Slaw Aug 07 '20 at 00:33
  • @Ekidona This Q&A from [softwareengineering.se] should help: [Why is Global State so Evil?](https://softwareengineering.stackexchange.com/questions/148108/why-is-global-state-so-evil). – Slaw Aug 07 '20 at 00:40
  • *"surprisingly easy to exchange information"* - THIS is the very problem with global variables. Exchanging information is how bugs happen - it should happen in a controlled manner, in few well specified places, it should *not* be easy. – Joni Aug 07 '20 at 00:41
  • Related: [Why are there no global variables in Java?](https://stackoverflow.com/questions/5581234/why-are-there-no-global-variables-in-java) – Mark Rotteveel Aug 07 '20 at 08:17

2 Answers2

2

The problem is that this practice doesn't scale. As projects get more complex or involve more collaborators, globals invite confusion and bugs as it becomes ever harder to keep track of possible side effects. "The left hand doesn't know what the right hand is doing." Or at least, not without considerable testing and research--adding to the cost of maintenance.

You might peruse a book on best practices, such as "Clean Code" by Robert C. Martin, a classic.

A similar situation arises in security, where we have the concept of granting least privileges.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41
1

It’s going to take a while to really understand object-oriented programming. The big issue with global variables is the same issue with getters and setters, making anything public or extending a superclass. You’re making a commitment to every piece of code that touches a global variable, or a setter or a getter that /you will not change the implementation/ that you’ve exposed. Right now that’s fine. Two years from now, what happens when 100,000 lines depend on that variable and now you realize that your “int x, y” implementation detail really should be “int x, y, z”? Feel like spending the weekend tracking down and fixing thousands of references to this code? Now imagine this problem multiplied by all the instances of state you’ve needlessly exposed to users of your code...

Jonathan Locke
  • 243
  • 2
  • 6