Why wouldn't we always want to access variables from anywhere?


#1

Question

Why wouldn’t we always want to access variables from anywhere?

Answer

Having variables accessible from anywhere can be problematic. Sometimes we want some variables to be hidden from other parts of the code. This could be for reasons such as for privacy, reusing the same variable names, or just keeping the code seperate. Privacy is a big reason because you might not want another person/service that’s using your code to know all your variable values. For variable name reuse, we might want to use the variable name in a scope where we’re getting a users name, but we might want to reuse the variable name when we gather the name of the users pet. Lastly, sometimes it’s just nice to keep your code in its own separate confined spaces, that way you can worry less about variables somehow interfering with each other/accidentally using a variable from a different context (happens more than you’d think).