Skip to content

Instantly share code, notes, and snippets.

View jhwheeler's full-sized avatar

Jackson Holiday Wheeler jhwheeler

View GitHub Profile

In Your Own Words: Scope

What is scope?

Scope is the part or section of a program to which a given function, variable, or object has access. Global scope is accessible by any and all functions, while local scope is only accessible to the encasing function and any function(s) inside said encasing function.

Why are global variables avoided?

Global variabes are to be avoided for various reasons. One being they pollute the namespace, i.e. you may use a global variable accidentally when intending to use a local one. They are also difficult to track: similarly to the point above, you may forget about the global variable if your program gets very large and accidentally modify it without intending to. Furthermore, they make it more likely to have unintended side effects pop up in your code, creating a codebase that is difficult to test, debug, and develop.