Variable Scope for New Programmers

I routinely see new programmers running into trouble with the concept of variable scope. It’s an extremely important concept, however, and one that must be understood in order to develop a reliable application. If you work in multiple programming languages, it can get even more confusing—each language has its own set of rules about how variable scope is handled.

First of all, what is variable scope? Essentially, variable scope describes when a piece of information is accessible. When a variable is created, the value it stores is only available for a certain part of the code execution. Memory is set aside to store that piece of information and then freed when it is no longer needed.

When all variables are always available, memory is wasted because it is set aside well before or after it’s needed. This also causes a lot of confusion when a programmer needs to recall used variable names. So, we have scope.

Scope Examples

JavaScript and PHP do not require you to declare a variable before use. This can be handy when you just need to get something done but problematic when troubleshooting.

For example, in JavaScript, a variable declared outside of a function is available inside a function:

Avoid Global Variables

With small scripts, you’re not going to run into these types of issues often, because it’s relatively easy to keep track of everything that’s going on. Inevitably, applications become larger and more complex, and tracking down the source of a problem becomes more laborious. One of the best ways to sidestep errors is to avoid using global variables.

#2 Use object-oriented programming (OOP)

This mantra is similar to the first point, but on a larger scale. Instead of a collection of functions that perform related tasks, the functions become methods of an object. (A method is a function that belongs to an object.) Values can then be passed into the object in a variety of useful ways:

By setting the properties of an object

By passing values as parameters of a method

By passing values as parameters of the constructor (the method that gets executed when an object is first instantiated)

Declaring your variables right away ensures that you haven’t accidentally embedded a variable declaration, like in a while loop, too deep in the code. It also helps ensure that you’re not reusing variable names that could erase data you’re expecting further on in the function.

Scope in a Recursive Function

A recursive function is one that calls itself, often over and over again. These are sometimes used to build tree views when data is hierarchical. When calling the same function over and over again, we’re reusing the same variable names. What happens to our variables? Take a look at this PHP example:

Ignore the fact that this loop is somewhat pointless—the basic concept is that $currentvalue never becomes greater than 2. Think of each execution of a function as a self-contained entity in memory. Even when called from within itself, that new call makes a copy of itself and any variable declarations made within that function stay within it.

Use Scope

Understanding scope will save you time troubleshooting and will make your code easier to follow. Keep your code minty fresh!

Jonathan Snook is a freelance web developer and consultant. When not working on one project or another, this proud father can be found spending time with
his son and wife in beautiful Ottawa, Ontario, Canada.