Global Variables will Break the Internet

You may have heard that global variables are dangerous and should be avoided at all costs. They may even break the internet. How scary!

However, it turns out on investigation that the claims are exaggerated and that in some situations, it’s OK to use global variables. One of these situations is when learning the programming skills you need for Computer Science GCSE and A Level.

It is true that there are often better alternatives to using global variables, such as passing parameters to functions or using Object Oriented Programming, but there is a reason that the global key word exists in many languages, including Python. When first learning to program, one of the concepts you will come across is variable scope. Let’s look at a couple of examples to understand what this means.

# Global vs. local variables
name = 'Susan'
print(name)

def print_name():
    name = 'Peter'
    print(name)

print_name()
print(name)

What do you expect the three lines of output to be for this program?

 

So the name variable wasn’t modified inside the function. This is because by default, variables inside functions are local. They exist only inside the function and are destroyed once the function call is complete.

Now look at this code:

# Global vs. local variables
name = 'Susan'
print(name)

def print_name():
    global name
    name = "Peter"
    print(name)

print_name()
print(name)

What do you expect the three lines of output to be for this program?

 

Can you see why this happened? Because we used the global version of the variable name inside of the print_name() function, the value of name outside of the function was modified too!

Think about scope as a type of visibility. If a variable’s scope is global it can be seen by the whole program. If it is local it can only be seen within the current function. This does get more complex when we start nesting functions, but the basic idea is quite simple.


Context is everything

Learning to program and understand new concepts is a particular context. Writing complex applications is a different context. The “evilness” of global variables is relative. For larger applications in particular, using global variables can make your code hard to maintain and follow, and should generally be avoided.

However, far from breaking the internet, global variables are actually fairly common in WordPress, which is the platform on which approximately 30% of websites worldwide are built (including this one). Much of the core functionally of WordPress uses PHP. Here’s some code that you might find on a WordPress site:

global $wpdb;

$result = $wpdb->get_results ( "
    SELECT * 
    FROM  $wpdb->posts
        WHERE post_type = 'page'
" );

foreach ( $result as $page )
{
   echo $page->ID.'<br/>';
   echo $page->post_title.'<br/>';
}

The global $wpdb; statement gives access to all the functionality of the WordPress Database Object.

Here’s the main takeaway from this article:

Global variables are sometimes OK. You probably won’t break the internet if you use them.

Sharing is caring!

2 Comments on “Global Variables will Break the Internet

Leave a Reply

Your email address will not be published. Required fields are marked *