Tech Tuesday: Literals, Constants and Variables

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.

In programming there are concepts that correspond to each of these three different expressions.  The first are so-called “literals” — they are values that appear directly in the program.  A value could be a number or it could be a piece of text.  For instance, the following piece of Javascript code when run in a browser will pop-up an alert box that says “William Henry Gates III”

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";

at every later time you want to do the same thing you now just need an alert(BILL).  The first line defines what is known as a constant (this is Javascript, but not supported by all browsers).  You can think of a constant as a way of referring to a value that does not change.  In this program BILL will always be replaced with “William Henry Gates III”.

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;

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";
coFounderOfMicrosoft = "Paul Allen";

The “coFounderOfMicrosoft” is the name of the variable and “Bill Gates” or “Paul Allen” are the values that it refers to in the example above.  The first line of this code snippet uses “var” to declare that what follows is a variable.  In Javascript this declaration is optional and you can just assign a value to a name without it but it is considered bad form to just introduce names without declaring them first.  The third line assigns a new value to the variable.  Here we omit the “var” because we have already declared the name above and only want to change its value.

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.

As an aside, you may have noticed that I used ALL CAPS for the name of the constant, but so-called camel case for the name of the variable.  In Javascript, those choices are based on convention — they are not a requirement of the syntax of the language.  In Javascript “mIX1ng_CAS3” would be perfectly allowed as either a variable or constant name.  The convention of using ALL CAPS for constants is relatively common across most programming languages.

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.

Posted: 15th May 2012Comments
Tags:  tech tuesday variables literals constants

Newer posts

Older posts

blog comments powered by Disqus
  1. disyessir reblogged this from continuations
  2. continuations posted this

Newer posts

Older posts