I'm curious as to know if these are the same thing.
I understand a global variable is a variable present outside a function which can be used my any function and a local variable is a variable which can only be used in and by a particular function.
What is global state? What is local state? Are they just terms used to describe the effects of using global/local variables?
Also what is the difference between global and local states and how can they affect a program?
Thank you.
Basically you are assuming correctly. The set of all global variables is known as the global environment or global state. It is a way of affecting the execution of code, hidden from sight - and should be avoided, except in special circumstances (see below). It becomes a nightmare really fast.
Local state is the opposite and is preferable. Use local variables to have complete control over your local state. It makes it easier to read your code, change it, and much easier to find errors in it. Also you do not affect other parts of your code.
It boils down to a matter of having much more control over your code, when you use encapsulation of your variables, functions/methods, and so on.
Globals can be useful, e.g. when your code needs to run in different environments (e.g. dev/staging/integration/production). Configurations are usually global. Other than that -> use locals.
Hope that helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With