# Concept of variable

For a beginner Is it right to use the concept of hotel room and passenger as an analogy to explain the concept of variable?

The use of an analogy is to provide a familiar jumping-off point to introduce something new. Since variables are unique features of computing systems, different from how anything else in the universe functions, we need some way to cross the gap. Let's look at how useful the analogy of hotel room and guest is, bearing in mind that most analogies do not survive contact with the students, for more than a few minutes.

The essential feature of this analogy is that you have a fixed thing, the room, which can have different contents: varying guests over time. Good. The very first thing out of a student's mouth (or keyboard) is likely to be: "can there be multiple guests in a room?" Hooray! Thinking! This was the objective.

Yes, you could use a One Dimensional Array (please do not call it a vector, which means something quite different to most students), or you could use a List. Quick explanation there...

Next idea: There is more than one room in most hotels! If you look at the building from outside, you see a two dimensional array of rooms, usually. The analogy hardly even needs for this idea to be broached, it just falls right out of the image. So, yes: a computer is basically a vast rectangle of boxes to put values in. Modern computers allow for different sized and shaped boxes, and even on-the-fly shapes like something conveyed with XML.

Hotel rooms have identifying information. In retreat centers, the rooms can have names, often the names of flowers or something. For computers -- which are essentially mathematical -- we just use numerical addresses. But names are so handy, so we layer those on top. We can define any old names for our any old arrangement of any sized boxes.

Now the analogy runs out of steam (or gasoline, or battery, or protons...) and we have introduced the subject in a way that students can visualize and therefore, hopefully let go of the nuts in the Southeast Asian Monkey Trap and move on. Some one of them might "continue writing the figure '1' " and you have another Ramanujan on your hands, but it is unlikely.

In 8th grade, over 40 years ago, my math teacher wheeled a Teletype to the front of the room and proceeded to explain about computer memory and variables, and then we connected to the School System's computer (the Police Department had one too, it was pretty exciting) and we created simple programs and stored them on paper tape. The essential facts about computers have not changed, because they can't.

The other answers made good points about naming and paper forms, so those should be mentioned also. But computers, variables, programs... are not like anything else, and that is why we have them, otherwise we would just have the other things instead. Because computers are so singular and incredible, we need to explain how they actually work, not paper over that. A construction worker who could only pick out wallpaper would not be much use when it came to building walls, putting in wiring and plumbing and so on.

And because those who do not learn from the history of computer system development are doomed to wander in the wilderness for 40 years. Other people have made a lot of the mistakes for you! Learn why they made those mistakes. Most likely, it is because they took some fixed idea they had too far, and couldn't make themselves agree with other people about stupid things like big-endian vs little-endian, which should never have been a thing. If you pick up the analogy by the pointy end, you can get fatally bitten by it.

• If the staff lives in the hotel, then you have a stored-program computer, with a Von Neumann architecture... – Scott Rowe Aug 18 '20 at 13:13
• "Modern computers allow for different sized and shaped boxes, and even on-the-fly shapes like something conveyed with XML." - so do paper forms, and have done for centuries. Did you even read my existing answer before posting drivel like "variables are unique features of computing systems, different from how anything else in the universe functions"? – Steve Aug 18 '20 at 13:33
• @Steve I acknowledged your idea of forms in my answer, did you read it? But you didn't mention things like arrays or lists. Spreadsheets used to be on paper. I was just explaining how a checkbook register works to a young person the other day... Hard to have any concept of a bank account without that visual image. How did we ever manage before the Sumerians found a way to record things on clay 3000 years ago? "There is nothing new under the Sun." And yes, computers are different from paper. I have never seen paper carry out a set of instructions written on paper. Maybe someday. – Scott Rowe Aug 18 '20 at 13:42
• I said "modern computers" because of course early computers only had one size memory cell. For the record, this is why I rarely come on the field in this community to fly ideas (several analogies right there): the lack of collegiality, cooperation and most of all, sense of humor at this SE site. – Scott Rowe Aug 18 '20 at 13:48
• +1 from me. In 1989 Dijkstra said that computers are such a radical novelty that all attempts at metaphorizing from the existing store of human knowledge is bound to fail. Then he goes on to say that the solution is the metaphor: programming is math😉 – Rusi Aug 18 '20 at 14:09

Why complicate matters with contrived analogies like hotel rooms and guests?

It is much more straightforward to take the analogy of a simple paper "form" (which absolutely everyone in contact with the Western world has used and understands), and understand that there are spaces or "boxes" on the form which are referred to by names. The person completing the form enters data into those boxes. Additionally, there exists Tippex, and the data in any given box can usually be overwritten - with computers moreso than with paper, overwriting is common. The content of a box - the thing written into it (or the absence of any entry) - is generically called the "value".

The boxes in this paper analogy are the equivalent of computer variables. The declaration of a variable is the equivalent to defining a new box - that is, allocating space on the form and providing that space with an associated name.

This analogy of a paper bureaucracy fully extends to all other computer concepts.

A function call, for example, is the summoning of another form from the shelf. We transfer certain "input" values from the existing form to this new form. We then perform a set of calculations,.or other administrative operations (like gathering other stored data, searching filing cabinets, etc.), and finally we arrive at a set of "return" values which are transferred back to the existing form, and the "new" form is then thrown in the bin.

I often think a serious deficit in modern IT teaching and discourse, is that too few people are taught basic administrative principles and their equivalent implementations using paper.

• Great idea for introducing the concept of a variable. Two thoughts: paper was meant to be a permanent record, the erase-ability is an unwanted side-effect. (This is why we use gel-pens when writing a check.) For computer variables, the very point is that they vary. The for-loop is probably the most fundamental computer construct. Second: many students now have likely never seen a paper form, a printed bank statement or used a register, ledger, filled out tax forms, or most other paper methods. Many might have little experience even writing with a pencil. Voice and 'wizards' do the work now. – Scott Rowe Aug 19 '20 at 11:06
• @ScottRowe, don't talk wet. Paper has always been thrown away, crossed out, rubbed out, or even Tippexed. If you really wanted to emphasise ease of variability or erasability, you could employ a blackboard as the "paper" - people will understand that it still represents a writing surface capable of displaying a "form" (a structure for information), but unlike a surface of wood pulp, a blackboard is more specifically designed for reuse. And to say "many students have never seen a paper form" is an utterly risible assertion. I certainly hope you do not reflect the standard of an educator. – Steve Aug 19 '20 at 12:51
• A worksheet is one kind of form, isn't it? So if nothing else, as long as the students aren't toddlers who are just starting school, then the form analogy could be converted into a worksheet analogy: The variable "names" are the problem questions, and their values are the students' answers. (There are also the "name" and "date" variables.) You can change your answer (the variable's value) as much as you want while you're working on your worksheet, but you're (supposed to be) always overwriting your old answer. Some functions could include grading and putting the grade into the gradebook. – Sarah Aug 29 '20 at 23:05

You can also use @Steve paper form idea for a second use, namely the importance of naming. I have often given students a paper form with boxes of different lengths, labeled A, B, C, etc. I then explain that their ability to fill in the form correctly (with information about themselves) will account for 50% of their final grade. Of course, they immediately ask "What goes in each box?"

Instead of answering directly, I show them a sample form already filled in. I use values that look like first/last name, but use names which could easily be either a first name or last name (e.g. Thomas/Kelly). Between the names there is a short box containing a single letter A. Middle initial, right! The idea is to demonstrate that even given data values, one is never certain what that value is. The name must convey to the reader what the value is. At the end, I show a form with appropriate labels for each box. For the short box, the label is "Grade I hope to get".

This can be augmented by taking a short piece of code (I like to use quadratic equation), and obfuscate with names that look like Dr Seuss made up words. I ask them to explain what the code does. It is difficult when the variable names have no meaning in connection to the problem. I explain that their own code will become equally unfathomable unless they choose good names. Furthermore, asking the instructor for help solving a problem will be difficult if the instructor can not read/understand their code.

public class Seuss { // with apologies to Dr Seuss and John Lennon

public static double snuvs(String s) { return Double.parseDouble(s); }

public static void main(String[] jlbboo) {

int lorax=0, quimney=2;
double beft, obsk, kweet, zong, lerkim, skirtz;

beft   = -snuvs(jlbboo[1]);
kweet  =  snuvs(jlbboo[quimney]);
obsk   =  snuvs(jlbboo[lorax]);
zong   =  obsk * quimney;
lerkim =  beft * beft;
skirtz =  lerkim - zong * kweet / 0.5;
beft  /=  zong;

if (skirtz < lorax)
System.out.println("Imagine the two numbers\nIt's easy if you try");
else if (skirtz == lorax)
System.out.println(beft);
else {
skirtz = Math.pow(skirtz, 1.0/quimney)/zong;
System.out.println((beft+skirtz) + "," + (beft - skirtz));
}
}
}

• That's a cute example – maybe I'll use it 😄. But the importance of good name choices is really a second level question. The primary one is that — at least in imperative programming — the variable concept is fundamentally inconsistent with the standard math concept with the same name and look. – Rusi Aug 15 '20 at 8:54
• @Rusi, it's probably more accurate to say the word "variable" is overloaded with many similar meanings. A better word for the administrative/computer concept would be "field". On a computer as on paper, you are not declaring a variable, you're declaring a field. Mathematics (i.e. non computer science) tends to lack rigour concerning storage (where values are put, how that place is referred to, etc.), whereas administration has always been fundamentally concerned about it, so mathematicians tend not to have either the vocabulary nor any explicit understanding of the concept. – Steve Aug 15 '20 at 10:42
• @Steve I guess we agree on the facts though not the emphasis: if one emphasizes storage enough one gets assembly language! Many modern languages eg python, java, de-emphasise the management/administration of storage (by pushing under a garbage collector) but still end up with a leaky abstraction because of the need to clarify reference semantics. Only functional languages provide a variable abstraction layer hard(ened) enough that the programmer cannot figure out storage questions. And that's precisely why they are considered "clean"! – Rusi Aug 15 '20 at 10:53
• @Rusi, you're talking about memory management. "Garbage collection" is what paper archivists do when they send old records to pulp after 6 years. I'm talking about the more fundamental concept that every single value handled, including intermediary values, must be stored in a pre-defined field in the first place. Clerks are completely used to the concept, because they work constantly with forms. Mathematicians don't tend to work with forms - they work freestyle on blackboards, defining the storage fields on the hoof (usually according to a schema existing only in their own minds). (1/2) – Steve Aug 15 '20 at 11:17
• Functional languages, like all computer languages, are still thoroughly explicit about how values are stored. You cannot calculate a value without specifying explicitly into what field the result goes. On a blackboard, you can just perform a calculation using space freely and as desired - you don't have to give every field a name or otherwise structure the storage of values up-front in any particular way. This is why some have said that computer science is the engineering of mathematics. (2/2) – Steve Aug 15 '20 at 11:28

Using labels as a metaphor over boxes has merit and has been examined.

This post looks at it from an early learner standpoint: https://www.felienne.com/archives/6063

There is a good post on this here that is more technical. https://hellokangning.github.io/post/variables-are-labels-not-boxes/

When looking at the strength of the metaphors to their "real life" examples:

• labels apply to one thing, boxes can contain many things
• if you have a named box, you already applied a label to a box that may contain a thing
• a thing can have many labels applied (aliasing) whereas a thing can only be in a single box
• applying a new label does not remove an old label, putting something in a new box should take it out if the old box (it doesn't here though)
• Good references. I think though, that the problem of metaphors is that we think we can keep them. We can't. We shouldn't try. It is like a key that unlocks a door: after we are in, the key is useless. The door is now unlocked and we will never have to pass through it again. If we belabor the metaphor and cling to it, we are preventing the students from moving forward. Students have misconceptions because they have conceptions that they tried to take in from someone else's understanding. This will not work. Everyone is an agent and has to create their own understanding. We only point the way. – Scott Rowe Aug 22 '20 at 21:26
• @ScottRowe good point that metaphors eventually need to be tossed aside. For the purposes of this question, I think the label metaphor more accurately represents how variables "work" and that would give it more longevity. – TooManyCooks Aug 27 '20 at 21:43
• I suppose people can have varying opinions, but perhaps a more literal explanation of how computers work is better than a metaphor. It is a machine, like a car. Applying humanistic metaphors for all but the briefest introduction is bound to go wrong. I started a car with a pencil once, and a broomstick another time. It had nothing to do with whether the car wanted to start or not. It had to do with me understanding the cause of the fault, and how a ready at hand item could temporarily fix it. A society where people don't understand electricity, electronics and radio is doomed. – Scott Rowe Aug 30 '20 at 13:07

I think it is a mistake to do this since it ties the "concept" of a variable, which is a language dependent item to an implementation on a certain kind of hardware. Yes, variables are often implemented in such a way, but the concept is independent of any implementation.

A computer language creates a self-referential and self-consistent virtual world. It is better that we use metaphors from such a world than from implementations. Also, if students eventually study OO languages or even pointers, they will often get confused by these sorts of descriptions. See the caveat at the end, however.

A better metaphor, in my view, is that a variable is a relationship between a "name" and a "value". The name might be simple (x) or complex (a[i]), but it is still just a name and it "represents" the value. It doesn't "hold" the value, but represents it.

Let me try to explain the reason I find this metaphor superior. First, students, in their real lives, understand "names" as representations of things.

At a given point in time/space a given name can only "represent" or "stand for" a single thing, though that "thing" might be complex. Buffy is my name here and it represents me. Other places and times Buffy represents other things (dare I say vampire slayers?). So, a variable isn't fixed for all time but can refer to different things at different times. Scopes in programs are a kind of place. The same name can refer to different things in different places. People already understand these things.

Also note that "I", the real world thing, am not "Buffy". "Buffy" is not "me", but something that refers to me.

In an OO language, a reference variable is also just a name that refers to an object. It isn't the object, but a reference. If students already think of names as references then this is much easier to grasp.

In a language like C, a pointer variable can also be described this way. Take a declaration like:

int *ip

The ip and *ip are just different names for different things. ip refers to one thing and *ip to something else. In fact ip is, itself, a reference to the other thing.

Summary:

• Names are simple or constructed.

• Names refer to things.

• A name can refer to one thing at a time, but it can be changed at another time

• A name can refer to one thing in a give place (at a time) but can reference a different thing (simultaneously) in a different place.

This is exactly, I think, how names behave in the real world. It is also how variables behave when implemented on current kinds of hardware.

Caveat. If you are teaching implementations, specifically, such as in a compiler course, the hotel room idea is fairly valid, though most hotel rooms can hold more than one thing at a time. So, even there, the metaphor has unfortunate possible misleading consequences.

• As comments aren't intended for a discussion like this, I've moved your comments to chat and you're welcome to continue the discussion there. – thesecretmaster Aug 23 '20 at 14:49
• It is interesting you say "how names behave in the real world". Well, how humans work with names. But names are not a thing in the sense that a computer variable is a thing. It is really important to distinguish our concepts from what we are having thoughts about. Being comfortable with a way of thinking is of little help if it differs from what is actually happening, or if actual activity is required. I can think all I want about riding a bicycle, but not falling over is entirely different, and studying Physics will not help. The computer responds to what you do, not what you are thinking. – Scott Rowe May 2 at 1:56

I don't think you can say any analogy is "right" or "wrong" across the board. Whether an analogy makes sense or not depends on the context of both the teacher and the student, and I don't think we can tell you whether any particular analogy is "right" for your context.

I personally avoid analogies when I'm talking about using and creating variables. I explain that a variable is a name that holds a value. I don't think that an analogy would make that more clear, at least for my context.

That being said, one of the most effective resources I've seen for explaining Java variables are these two articles:

These articles use an analogy of variables being coffee cups with names written on them. In this analogy, Object references are remote controls that you put inside a cup, and it's one of my go-to references when explaining that Java is pass-by-value.

• I would say that a variable is a location in memory that holds a value. The name is just a convenience for humans. Lightswitches turn lights on an off, whether they have labels or not. But just writing a label under a lightswitch doesn't do anything. I had students with that misconception, they thought that the name was wired in to the behavior of the language. Names make our job easier, they are no help to the computer at all. We have to cooperate to get the solution. It is like hiring a translator. – Scott Rowe May 2 at 2:00

I have to teach basic computer skills, applications and using online platforms in advance of programming this year.

Since I will do some Spreadsheet work, I plan to initially use a cell to demonstrate an example of a variable...

• It has a (cell) reference (which can be changed to something with semantic meaning)
• It has a value (including Null/None/"nothing"
• this value has a "type" or "class"
• There can be sub-classes (number is parent to int and decimal etc) and different langs define these differently
• Another cell can reference that variable by it's reference
• As @TooManyCooks indicates, it is possible to demonstrate aliasing with a named cell (something I had not thought of)

We can reference a cell with a formula in it- this formula may represent a function which "takes in" something (perhaps) and "returns" something... We can link sheets, so a variable can refer to something that "lives" in another tab, or file, or image or web address... thus variables refer to other "things" or "objects" which can be... anything (in theory).

These ideas relate to what @Steve and @Buffy have already identified.

I've been thinking that some concepts of functional programming are present in Spreadsheets, and some OOP concepts are present in Database... since I have to teach both in advance of programming this year, perhaps these concepts can be conveyed in advance.

Another useful tool may be the visualizer as this will let you "see" it in action using a range of languages.

Again you can see the variable has a type, reference, value etc.

Hope that this might prove useful or prompt other ideas in addition to all of the fine suggestions already provided.

• Spreadsheets are the most used computer application in the world, last I heard. Certainly seems like an important 'megaphor' in computing. – Scott Rowe Aug 31 '20 at 10:12