Global Variable Basics

John Long
JL Codes
Published in
4 min readNov 26, 2017
Art by Noah Bradley

While working on an expanding project of mine, I ran into a error that caused my program to crash. As usual, I started to sift through the various lines of code I assumed were involved in the problem. This is a debatable way to solve a problem, but I digress.

To my knowledge, everything looked clean. Well, clean to me.

In order to preface the upcoming problem I would like to mention that the program being manipulated was in its early stages of development. Almost everything was organized, functionally. Which, in programmer language, is to say not really organized at all. In my attempt to improve the organization and dry up the code, I had added a few methods but didn’t go as far as to create an elaborate OOP (Object Oriented Programming) structure.

This introduction of methods into my organizational structure is important, because it is what ultimately led to my new problem. Here is the error message I was receiving when I tried to run the front end of my program:

frontend.rb:30:in `compile_book_menu': undefined local variable or method `base_url' for main:Object (NameError)from frontend.rb:72:in `<main>'

Not too bad as far as error messages are concerned. Obviously the first places to start digging were lines 30 and/or 72. Line 30 was a part of my new method so I simply began there. Here is the exact method in question:

28 . def compile_book_menu
29 . book_menu_options = {}
30 . response = Unirest.get("#{base_url}books")
31 . books = response.body
32 . books.each do |indiv|
33 . book_menu_options[indiv["title"]] = indiv["id"]
34 . end
35 . return book_menu_options
36 . end

But I would like to focus on one line of the code. Line 30.

response = Unirest.get("#{base_url}books")

The line in question was the piece of the method that communicates to the server and pulls the needed information. Take notice of the string interpolated variable inside the quotation marks —

#{base_url}

In fact, it’s really the only variable that could have been causing an issue. Making identification of the problem fairly easy. However, my confusion stemmed from the fact that the error message said the variable was undefined… how could this be? I had clearly established the variable in one of my first few lines of code.

base_url = "http://localhost:3000/v1/"

In all other areas where the base_url variable was used I wasn’t having any trouble. So what was going wrong?

Some of you capable developers out there might laugh at this puzzle. Or lack there-of.

You see, when using a function to perform a task, that function operates within itself. Which is to say, unless you have a special kind of variable, the function won’t pick up a variable from outside the function. I, brilliantly as always, had defined base_url outside the function.

The solution? In this case, the simplest short-term fix to my problem was to use a different kind of variable: a global variable.

A global variable is a powerful tool, that should be used sparingly, to set a variable so that it can be used anywhere in the program. Including, inside methods.

Setting up a global variable is simple, just put a $ before the variable name. Easy as that. Well, on the surface at least.

base_url = "http://localhost:3000/v1/"becomes$base_url = "http://localhost:3000/v1/"

As I mentioned earlier, creating a global variable should be done only when necessary, as there is some potential for greater problems down the road.

The idea behind a variable is something that can be read and written from specific areas of your codebase. Creating a global variable surpasses these innate assumptions and can make code harder to read and manipulate in the future.

Global variables should also, almost, never (I hesitate to say never with totality) be given common variable names such as:

$name = John

Something like this would be code suicide, as a variable created by you, or someone on your team, in the near future could easily have a name like - name.

That being noted, global variables do have a time and a place, and for something as basic as what I was putting together in my program, using a global variable was a wonderful solution to my problem.

The new Line 30 became

response = Unirest.get("#{$base_url}books")

And I also had to change all the places I used base_url to include the ‘$’ as a prefix. Just like that, I was back on my feet.

Stay on yours too, and happy coding,

--

--