Abstraction is one of the big ideas that we use every day without even noticing. Suppose, for example, that you’re going to meet a friend at a coffee shop. You realize that you’re going to be a few minutes late so you call your friend and ask them to save a little time by buying your coffee order for you. You reassure them that you will pay them for it when you get there, but you don’t recall exactly how much it costs. So you say, “I’ll repay you the price when I get there.”
The term “price” is an abstraction. It specifies the price of the coffee, but it can also be used for the price of a T-shirt, a smartphone, or tissue paper. “Price” is a handy-dandy word that can be used to specify a lot of different things. The price of something can be $12.35 or $0.99 or $10,423.12. That one word can have many different numerical values, depending upon the context.
Now suppose that, during your conversation with your friend, you mention a really great park that you discovered. Your urge your friend to go see it, but your friend is a bit reluctant. You say, “Look, the distance is only 12 miles.” There, you’ve done it again: used another abstraction. “Distance” is an abstraction that can have many different numbers. It can be 12 miles, or 37 feet, or 321 miles.
Our language is chock full of abstractions like this; we use them all the time. They’re useful because they allow us to discuss things whose values we may not know, but we still need to talk about. “While you’re out, would you pick up some printer paper for me at the store? But don’t bother if the price is more than $5.”
Computers use abstraction, too. Their abstractions are called variables, and are very similar to the abstractions we use. A programming variable is just a word that acts like a box holding a number. For example, we can make a variable called price that holds a number for the price of something. We call it a variable because the number inside the box can change while the program is running. That is, the value inside price can be $12.34 at the beginning of the program, but it can be $12.55 later on, and $13.06 even later. The variable price is a box that can hold any number at all.
The trick lies in the realization that the actual value at any given instant is not the essence of the thing. For example, suppose we talked about a different number: the time. Let’s say that you and I are having a conversation about time. You say, “What time is it?” I answer, “The time is 1:22:30.” Thereafter, whenever you think of time, do you think of 1:22:30? Of course not. Time is a variable whose value was 1:22:30 for one second. When we think about time, we don’t fixate on the number 1:22:30; we instead think of time as a variable that can take many different values. This is the essence of a variable: something that could be any of many different numbers, but at any given time has exactly one number. For example, your speed is a variable: sometimes you are going 55 mph and sometimes you are going 0 mph. Your bank balance is a variable: one day it might be $27.34 and another day it might be $5327.34.
The importance of indirection is that it allows us to focus our attention on grander relationships. Do you remember your grammar school arithmetic exercises: "You are traveling at 40 mph. How far can you travel in two hours?" This is a simple arithmetic problem, but behind it lies a much more interesting and powerful concept. It lies in the equation
distance = speed * time
The big idea here is that this equation only makes sense if you forget the petty details of exactly what the speed is, and what the time is. It is true whatever the speed is, and whatever the time is. When we use an equation like this, we transcend the petty world of numbers and focus our attention on grander relationships between real-world concepts. If, to understand this equation, you must use examples ("Well, 40 mph for 2 hours gives 80 miles"), then you have not fully grasped the concept of indirection. Examples are a useful means of introducing you to the concept, of supporting your weight as you learn to walk, but the time must come when you unbolt the trainer wheels and think in terms of the relationship itself, not merely its application in a few examples. Variables are the means for doing this.
The concept of indirection is not confined to mathematical contexts. We use indirection in our language all the time. When we say, “Children look like their parents”, we are making a general statement about the nature of human beings. Only the most literal of nincompoops is troubled by this statement, asking “Which children look like which parents?” We all know that the noun “children” applies to any child. It is a variable; if you want a specific case, then grab a specific child off the street and plug him into this verbal equation. Take little Johnny Smith; his parents are Fred and Wilma Smith. Then the statement becomes “Johnny Smith looks like Fred and Wilma Smith.” Again, the important concept is not about Johnny and Fred and Wilma, but about children and parents in general.
Suppose that you were the boss at a factory. It would be wasteful to stand over each worker, telling him or her what to do at each step of the manufacturing process. (“OK, now put that short screw into the hole at the top. Good. Now put the nut onto the bolt. Now…") A much more efficient way is to explain the entire process to the worker before they start work. Once the worker has memorized the process, you don’t have to worry about them any more. This is just like a computer program. When you download an app (a computer program) into your computer or smartphone, it is rather like the boss at the factory saying to the workers, “Here is a book of instructions for how to build a new machine. I don’t even know what the instructions are, but I like the machine. Follow these instructions.”
My favorite example of a program that doesn’t exist on a computer is the Constitution of the United States of America. This document is composed of a set of commands that prescribe how the government of the USA will operate. It specifies who will do what, and when, and how. Like a computer program, it has variables: the President, the Congress, the Supreme Court. Each variable can take different “values” – the President can be Washington, Lincoln, Roosevelt, and so on, but the commands are the same regardless of the “value” of the President, the Congress, or the Supreme Court. Like any computer program, a great deal of effort was expended getting each part of the Constitution just right, tightening up the sloppy wording, making sure that the commands would work in all conceivable situations. And like any real computer program, the programmers have spent a long time getting all the bugs out. Despite this, it has worked very well for nearly two hundred years now. Show me a computer program with that kind of performance record.
Now that you have the concept under your belt, you can try it out on an actual computer. Here’s the exercise to try your hand at. Once you’ve gotten that one down, here’s an exercise to really challenge you.