Why are global variables bad?
Mervin Nov 12 '17
Global variables are variables that are declared outside to be accessible by everyone in the application.
But if you search it in goole. The second query result is that global variables are bad. Why?
I think everything has its own pros and cons. And global variables' pros and cons depends on how it is being used. It also depends on how big or small your application is.
For smaller applications, global variables are not a problem. It helps your code to easily pass and access data from other classes and methods. You don't need to pass a variable every time you call a method or class.
Sharing data inside the application using global variables will also help you minimize local variable creation and lesser memory usage.
But for larger applications, using global variables are bad. It will make your application hard to maintain and read. Looking for global variables just to know how they were created or manipulated will take you some time. And in OOP, it destroys the idea of encapsulation.
But if you do understand what global variables are, then you just have to make your own decision in your implementation. Just code at your risk :D