I may have lost a bunch of Tech Tuesday readers with that post on semantics last week, so today I am hoping to gain some back with a much easier topic! We are continuing the cycle on programming by looking at how programs refer to things. Again let’s start by looking at human language. If I say “William Henry Gates III” then I am referring to the co-founder of Microsoft by his full given name. In the right context I could simply say “Bill” and everyone would know who it was. Alternatively, I could say “co-founder of Microsoft” and that could mean either Bill Gates or Paul Allen.
alert("William Henry Gates III");
Now you might find it takes you too long to type that everywhere in the program that you want to do this. So you could instead write
const BILL = "William Henry Gates III"; alert(BILL);
Why might you want to have a constant instead of a literal? We already saw one reason - to be able to use a shorter name to refer to something longer (not much longer in the example, but could be much much longer). Another and stronger reason is if you have a number that has a meaning. Compare the following two pieces of code
const MEANING_OF_LIFE = 42; alert(MEANING_OF_LIFE);
Now instead of having the literal number 42 throughout, you can refer to it as MEANING_OF_LIFE. That’s not only clearer for someone reading the code but also lets you subsequently make a change to the code should you realize that the value should really have been 13. The change now needs to be made only in one location where the constant is defined, i.e. const MEANING_OF_LIFE = 13 — all the rest of the code can stay the same. That’s much preferred over a global search and replace, because what if you have a 42 somewhere else in the program that is supposed to stay 42 because it refers to something else?
That brings us to variables. Like a constant, a variable is a name that we can use to refer to a value, but unlike a constant that value can change over time. So we might have code as follows:
var coFounderOfMicrosoft = "Bill Gates"; alert(coFounderOfMicrosoft); coFounderOfMicrosoft = "Paul Allen"; alert(coFounderOfMicrosoft);
The ability to have names to refer to different values is essential to programming. Programs based only on literals would be quite boring. You would not be able to keep track of intermediate results and it would be quite difficult to write programs that take inputs from the user (where would you keep those?) as part of their processing. So the concept of a variable seems obvious and straightforward and yet as we will see when we look a bit deeper in a future Tech Tuesday there is a lot more to it and you can get a glimpse of that by looking at the following code:
var coFounderOfMicrosoft = "Bill Gates"; var myName = coFounderOfMicrosoft; coFounderOfMicrosoft = "Paul Allen";
In the second line we are declaring a new variable and assigning an existing variable to it. What does that do? The answer is far from obvious and is something that gives rise to a lot of issues which we will look at in subsequent posts. For now, just think about what you expect myName to refer to once the third line of code has also been executed. Is it “Bill Gates” or “Paul Allen”?
One important thing to note is that just because you give a variable a name that is meaningful to a human it doesn’t mean anything to the computer (remember that discussion of semantics?). For instance, the following is a perfectly legit bit of code that won’t produce a syntax error since it is syntactically correct and won’t produce a complaint about semantics (because the computer has no external knowledge):
var coFounderOfMicrosoft = "Napoleon Bonaparte";
This is the first example of something I call “The Program Is All There Is” or TPIATI. For you as the human reader of the code there is a lot of knowledge that tells you this is wrong. For the computer it just says make a variable named “coFounderOfMicrosoft” and give it the value “Napoleon Bonaparte” — and it will happily do just that.
Tech Tuesday will be taking a break for the next two weeks as we will be going to China on a family vacation and I am not planning to take a laptop along. We will resume in the first week of June.