<p>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.</p>
<p>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: &quot;can there be multiple guests in a room?&quot; Hooray! Thinking! This was the objective.</p>
<p>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...</p>
<p>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.</p>
<p>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.</p>
<p>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 <a href="https://www.goodreads.com/author/quotes/14525.Ram_Dass?page=5" rel="noreferrer">&quot;continue writing the figure '1' &quot;</a> and you have another Ramanujan on your hands, but it is unlikely.</p>
<p>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.</p>
<p>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 <em>actually work</em>, 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.</p>
<p>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.</p>

CC BY-SA 4.0
2020-08-18T13:13:48.437Z
If the staff lives in the hotel, then you have a stored-program computer, with a Von Neumann architecture...
Scott Rowe
CC BY-SA 4.0
2020-08-18T13:33:26.057Z
*"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
CC BY-SA 4.0
2020-08-18T13:42:02.517Z
@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
CC BY-SA 4.0
2020-08-18T13:48:39.840Z
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
CC BY-SA 4.0
2020-08-18T13:57:43.000Z
You genuflect to my answer after a number of paragraphs, then immediately contradict it again. What is special about arrays or lists? I have one of those *every time I go shopping*, and the vast majority of paper forms incorporate some array elements. A simple paper receipt will have multiple rows, with multiple fields, to capture a description, item price, quantity, and line total, each for many items. A bank statement is another example of a structured paper form with arrays. As for bank accounts, you are explaining nothing unless you explain double-entry accounting.
Steve
CC BY-SA 4.0
2020-08-18T13:58:55.160Z
@steve I guess you don't own a computer then. No need for one.
Scott Rowe
CC BY-SA 4.0
2020-08-18T14:03:25.870Z
And the question is not to what extent computers differ from paper, the question was how to explain the *coding* concept of a "variable". The human perfectly well understands that with paper forms, they are the operator. They ought to understand that the computer will be the operator with code. The question is that if the computer is told to establish and use a "variable", then what is the equivalent of that operation for a human operator using pen and paper? The answer is that it is the equivalent of drawing another box on the form, into which values can be written or retrieved from later.
Steve
CC BY-SA 4.0
2020-08-18T14:09:50.693Z
+1 from me. In 1989 [Dijkstra](https://www.cs.utexas.edu/users/EWD/transcriptions/EWD10xx/EWD1036.html) 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
Rushi
CC BY-SA 4.0
2020-08-18T14:30:54.440Z
*"The question is," said Humpty Dumpty, "which is to be master -- that's all."* You know the question, just as I did. When I was seven.... And no one I knew had seen a computer, and I wanted to know *How could such a thing possibly work?!?*
Scott Rowe
CC BY-SA 4.0
2020-08-18T14:35:09.180Z
@rusi Thank you, but Dijkstra didn't go far enough, he just bunted. Buffy will tell you that programming is not math, it is something much deeper, related to *naming things*, which no other thing in the universe does, so far as we know. "*Anyone who is not shocked by Quantum Mechanics has not understood it.*" Equally so for computers, and there are billions of un-surprised people out there. And our job is to shock them. Piaget said, "*Nothing new is learned until existing systems have failed to maintain equilibrium.*" The quote of the 20th century. Let's get started!
Scott Rowe
CC BY-SA 4.0
2020-08-19T13:13:03.603Z
@Rusi, that's an interesting view from Dijkstra - he appears to be talking in total ignorance of the large bureaucracies that grew up before the computer era. For example, the banks (who were one of the first frontiers for computerisation in the 1960s) already had complex and well-engineered systems of accountancy and administration, which were not "analogue" but were akin to "pressing a typewriter key" (as Dijkstra puts it). Concepts like flowcharts and similar predated computing. (1/2)
Steve
CC BY-SA 4.0
2020-08-19T13:14:21.483Z
He goes on to say *"the program is an abstract symbol manipulator, which can be turned into a concrete one by supplying a computer to it."*. The question then is, why does the computer have to be a machine, why can it not be a human? In fact "computers" were originally human, and they used paper to manipulate symbols haha! There's nothing conceptually radical about them at all - they are simply *fast* at doing what they do. (2/2)
Steve
CC BY-SA 4.0
2020-08-18T12:57:22.377Z
2020-08-18T13:04:13.587Z
2020-08-18T13:04:13.587Z
Scott Rowe
<p>Why complicate matters with contrived analogies like hotel rooms and guests?</p>
<p>It is much more straightforward to take the analogy of a simple paper &quot;form&quot; (which absolutely everyone in contact with the Western world has used and understands), and understand that there are spaces or &quot;boxes&quot; 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 &quot;value&quot;.</p>
<p>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.</p>
<p>This analogy of a paper bureaucracy fully extends to all other computer concepts.</p>
<p>A function call, for example, is the summoning of another form from the shelf. We transfer certain &quot;input&quot; 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 &quot;return&quot; values which are transferred back to the existing form, and the &quot;new&quot; form is then thrown in the bin.</p>
<p>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.</p>

CC BY-SA 4.0
2020-08-19T11:06:05.433Z
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
CC BY-SA 4.0
2020-08-19T12:51:35.863Z
@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
CC BY-SA 4.0
2020-08-29T23:05:02.377Z
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
CC BY-SA 4.0
2020-08-14T13:35:46.490Z
2020-08-14T13:35:46.490Z
Steve
<p>Using labels as a metaphor over boxes has merit and has been examined.</p>
<p>This post looks at it from an early learner standpoint:
<a href="https://www.felienne.com/archives/6063" rel="nofollow noreferrer">https://www.felienne.com/archives/6063</a></p>
<p>There is a good post on this here that is more technical.
<a href="https://hellokangning.github.io/post/variables-are-labels-not-boxes/" rel="nofollow noreferrer">https://hellokangning.github.io/post/variables-are-labels-not-boxes/</a></p>
<p>When looking at the strength of the metaphors to their &quot;real life&quot; examples:</p>
<ul>
<li>labels apply to one thing, boxes can contain many things</li>
<li>if you have a named box, you already applied a label to a box that may contain a thing</li>
<li>a thing can have many labels applied (aliasing) whereas a thing can only be in a single box</li>
<li>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)</li>
</ul>

CC BY-SA 4.0
2020-08-22T21:26:02.530Z
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
CC BY-SA 4.0
2020-08-27T21:43:36.450Z
@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
CC BY-SA 4.0
2020-08-30T13:07:49.987Z
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
CC BY-SA 4.0
2020-08-22T15:11:46.017Z
2020-08-22T15:11:46.017Z
TooManyCooks
<p>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 &quot;What goes in each box?&quot;</p>
<p>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 <strong>name</strong> 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 &quot;Grade I hope to get&quot;.</p>
<p>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.</p>
<pre class="lang-java prettyprint-override"><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 &lt; lorax)
      System.out.println(&quot;Imagine the two numbers\nIt's easy if you try&quot;);
    else if (skirtz == lorax)
      System.out.println(beft);
    else {
      skirtz = Math.pow(skirtz, 1.0/quimney)/zong;
      System.out.println((beft+skirtz) + &quot;,&quot; + (beft - skirtz));
    }
  }
}
</code></pre>

CC BY-SA 4.0
2020-08-15T08:54:44.107Z
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.
Rushi
CC BY-SA 4.0
2020-08-15T10:42:42.717Z
@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
CC BY-SA 4.0
2020-08-15T10:53:53.413Z
@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"!
Rushi
CC BY-SA 4.0
2020-08-15T11:17:12.597Z
@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
CC BY-SA 4.0
2020-08-15T11:28:26.527Z
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
CC BY-SA 4.0
2020-08-16T15:37:45.177Z
Sorry, but I don't see how obfuscating names answers the OPs question at all.
Buffy
CC BY-SA 4.0
2020-08-17T16:59:28.467Z
@Buffy, this post was not to answer the question directly, since you and other have done a much better job that I could, but to address a second topic that one might discuss when describing what a variable is. Namely the importance of good naming. I wish my answer came after the actual answers.
Fritz Sieker
CC BY-SA 4.0
2020-08-17T17:01:37.060Z
Non answers usually get deleted. This isn't a random discussion site. If you want to ask the associated question yourself and provide your own answer, that is acceptable.
Buffy
CC BY-SA 4.0
2020-08-14T22:02:07.367Z
2020-08-16T14:55:07.377Z
2020-08-16T14:55:07.377Z
Fritz Sieker
<p>I don't think you can say any analogy is &quot;right&quot; or &quot;wrong&quot; 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 &quot;right&quot; for your context.</p>
<p>I personally avoid analogies when I'm talking about <a href="https://happycoding.io/tutorials/processing/using-variables" rel="nofollow noreferrer">using</a> and <a href="https://happycoding.io/tutorials/processing/creating-variables" rel="nofollow noreferrer">creating</a> 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.</p>
<p>That being said, one of the most effective resources I've seen for explaining Java variables are these two articles:</p>
<ul>
<li><a href="https://javaranch.com/campfire/StoryCups.jsp" rel="nofollow noreferrer">Cup Size -- a story about variables</a></li>
<li><a href="https://javaranch.com/campfire/StoryPassBy.jsp" rel="nofollow noreferrer">Pass-by-Value Please (Cup Size continued)</a></li>
</ul>
<p>These articles use an analogy of variables being coffee cups with names written on them. In this analogy, <code>Object</code> 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.</p>

CC BY-SA 4.0
2021-05-02T02:00:39.133Z
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
CC BY-SA 4.0
2020-09-01T00:42:29.930Z
2020-09-01T00:42:29.930Z
Kevin Workman
<p>I think it is a mistake to do this since it ties the &quot;concept&quot; 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.</p>
<p>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.</p>
<p>A better metaphor, in my view, is that a variable is a relationship between a &quot;name&quot; and a &quot;value&quot;. The name might be simple (x) or complex (a[i]), but it is still just a name and it &quot;represents&quot; the value. It doesn't &quot;hold&quot; the value, but represents it.</p>
<p>Let me try to explain the reason I find this metaphor superior. First, students, in their real lives, understand &quot;names&quot; as representations of things.</p>
<p>At a given point in time/space a given name can only &quot;represent&quot; or &quot;stand for&quot; a single thing, though that &quot;thing&quot; 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.</p>
<p>Also note that &quot;I&quot;, the real world thing, am not &quot;Buffy&quot;. &quot;Buffy&quot; is not &quot;me&quot;, but something that <em>refers</em> to me.</p>
<p>In an OO language, a reference variable is also just a name that <em>refers</em> 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.</p>
<p>In a language like C, a pointer variable can also be described this way. Take a declaration like:</p>
<blockquote>
<p>int  *ip</p>
</blockquote>
<p>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.</p>
<p>Summary:</p>
<ul>
<li><p>Names are simple or constructed.</p>
</li>
<li><p>Names refer to things.</p>
</li>
<li><p>A name can refer to one thing at a time, but it can be changed at another time</p>
</li>
<li><p>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.</p>
</li>
</ul>
<p>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.</p>
<hr />
<p>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.</p>

CC BY-SA 4.0
2020-08-23T14:49:56.250Z
As comments aren't intended for a discussion like this, I've [moved your comments to chat](https://chat.stackexchange.com/rooms/112147/discussion-on-answer-by-buffy-concept-of-variable) and you're welcome to continue the discussion there.
thesecretmaster
CC BY-SA 4.0
2021-05-02T01:56:10.430Z
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
CC BY-SA 4.0
2020-08-10T20:18:43.343Z
2020-08-11T22:16:13.170Z
2020-08-11T22:16:13.170Z
Buffy
<p>I have to teach basic computer skills, applications and using online platforms in advance of programming this year.</p>
<p>Since I will do some Spreadsheet work, I plan to initially use a cell to demonstrate an example of a variable...</p>
<ul>
<li>It has a (cell) reference (which can be changed to something with semantic meaning)</li>
<li>It has a value (including Null/None/&quot;nothing&quot;</li>
<li>this value has a &quot;type&quot; or &quot;class&quot;</li>
<li>There can be sub-classes (number is parent to int and decimal etc) and different langs define these differently</li>
<li>Another cell can reference that variable by it's reference</li>
<li>As @TooManyCooks indicates, it is possible to demonstrate aliasing with a named cell (something I had not thought of)</li>
</ul>
<p>We can reference a cell with a formula in it- this formula may represent a function which &quot;takes in&quot; something (perhaps) and &quot;returns&quot; something...
We can link sheets, so a variable can refer to something that &quot;lives&quot; in another tab, or file, or image or web address... thus variables refer to other &quot;things&quot; or &quot;objects&quot; which can be... anything (in theory).</p>
<p>These ideas relate to what @Steve and @Buffy have already identified.</p>
<p>I've been thinking that <strong>some</strong> concepts of functional programming are present in Spreadsheets, and <strong>some</strong> 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.</p>
<p>Another useful tool may be the <a href="http://www.pythontutor.com/" rel="nofollow noreferrer">visualizer</a> as this will let you &quot;see&quot; it in action using a range of languages.</p>
<p><a href="https://i.sstatic.net/v01Kq.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/v01Kq.png" alt="Multi-language visualizer" /></a></p>
<p>Again you can see the variable has a type, reference, value etc.</p>
<p><a href="https://i.sstatic.net/pezkE.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/pezkE.png" alt="Variables have a value, type etc." /></a></p>
<p>Hope that this might prove useful or prompt other ideas in addition to all of the fine suggestions already provided.</p>

CC BY-SA 4.0
2020-08-31T10:12:42.253Z
Spreadsheets are the most used computer application in the world, last I heard. Certainly seems like an important 'megaphor' in computing.
Scott Rowe
CC BY-SA 4.0
2020-08-27T14:18:49.333Z
2020-09-04T09:48:13.010Z
2020-09-04T09:48:13.010Z
srattigan
<p>For a beginner
Is it right to use the concept of hotel room and passenger as an analogy to explain the concept of variable?</p>

<programming><variables><data-types>
Concept of variable
# Concept of variable

(Asked by: username_0 on 2020-08-10)

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

**Tags:** programming, variables, data-types


---

## 7 answers

### Answer by username_1 on 2020-08-18. Score: 5 | Accepted answer

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' "](https://www.goodreads.com/author/quotes/14525.Ram_Dass?page=5) 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.

> **Comments:**
> **username_1**: If the staff lives in the hotel, then you have a stored-program computer, with a Von Neumann architecture...  
> **username_2**: *"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"*?  
> **username_1**: @username_2 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.  
> **username_1**: 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.  
> **username_2**: You genuflect to my answer after a number of paragraphs, then immediately contradict it again. What is special about arrays or lists? I have one of those *every time I go shopping*, and the vast majority of paper forms incorporate some array elements. A simple paper receipt will have multiple rows, with multiple fields, to capture a description, item price, quantity, and line total, each for many items. A bank statement is another example of a structured paper form with arrays. As for bank accounts, you are explaining nothing unless you explain double-entry accounting.  
> **username_1**: @steve I guess you don't own a computer then. No need for one.  
> **username_2**: And the question is not to what extent computers differ from paper, the question was how to explain the *coding* concept of a "variable". The human perfectly well understands that with paper forms, they are the operator. They ought to understand that the computer will be the operator with code. The question is that if the computer is told to establish and use a "variable", then what is the equivalent of that operation for a human operator using pen and paper? The answer is that it is the equivalent of drawing another box on the form, into which values can be written or retrieved from later.  
> **username_3**: +1 from me. In 1989 [Dijkstra](https://www.cs.utexas.edu/users/EWD/transcriptions/EWD10xx/EWD1036.html) 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  
> **username_1**: *"The question is," said Humpty Dumpty, "which is to be master -- that's all."* You know the question, just as I did. When I was seven.... And no one I knew had seen a computer, and I wanted to know *How could such a thing possibly work?!?*  
> **username_1**: @rusi Thank you, but Dijkstra didn't go far enough, he just bunted. username_7 will tell you that programming is not math, it is something much deeper, related to *naming things*, which no other thing in the universe does, so far as we know. "*Anyone who is not shocked by Quantum Mechanics has not understood it.*" Equally so for computers, and there are billions of un-surprised people out there. And our job is to shock them. Piaget said, "*Nothing new is learned until existing systems have failed to maintain equilibrium.*" The quote of the 20th century. Let's get started!  
> **username_2**: @Rusi, that's an interesting view from Dijkstra - he appears to be talking in total ignorance of the large bureaucracies that grew up before the computer era. For example, the banks (who were one of the first frontiers for computerisation in the 1960s) already had complex and well-engineered systems of accountancy and administration, which were not "analogue" but were akin to "pressing a typewriter key" (as Dijkstra puts it). Concepts like flowcharts and similar predated computing. (1/2)  
> **username_2**: He goes on to say *"the program is an abstract symbol manipulator, which can be turned into a concrete one by supplying a computer to it."*. The question then is, why does the computer have to be a machine, why can it not be a human? In fact "computers" were originally human, and they used paper to manipulate symbols haha! There's nothing conceptually radical about them at all - they are simply *fast* at doing what they do. (2/2)  

---

### Answer by username_2 on 2020-08-14. Score: 7

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.

> **Comments:**
> **username_1**: 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.  
> **username_2**: @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.  
> **username_4**: 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.  

---

### Answer by username_5 on 2020-08-22. Score: 4

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)

> **Comments:**
> **username_1**: 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.  
> **username_5**: @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.  
> **username_1**: 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.  

---

### Answer by username_6 on 2020-08-14. Score: 4

You can also use @username_2 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));
    }
  }
}

> **Comments:**
> **username_3**: 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.  
> **username_2**: @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.  
> **username_3**: @username_2 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"!  
> **username_2**: @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)  
> **username_2**: 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)  
> **username_7**: Sorry, but I don't see how obfuscating names answers the OPs question at all.  
> **username_6**: @username_7, this post was not to answer the question directly, since you and other have done a much better job that I could, but to address a second topic that one might discuss when describing what a variable is. Namely the importance of good naming. I wish my answer came after the actual answers.  
> **username_7**: Non answers usually get deleted. This isn't a random discussion site. If you want to ask the associated question yourself and provide your own answer, that is acceptable.  

---

### Answer by username_8 on 2020-09-01. Score: 2

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](https://happycoding.io/tutorials/processing/using-variables) and [creating](https://happycoding.io/tutorials/processing/creating-variables) 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:

* [Cup Size -- a story about variables](https://javaranch.com/campfire/StoryCups.jsp)
* [Pass-by-Value Please (Cup Size continued)](https://javaranch.com/campfire/StoryPassBy.jsp)

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.

> **Comments:**
> **username_1**: 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.  

---

### Answer by username_7 on 2020-08-10. Score: 2

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. username_7 is my name here and it represents me. Other places and times username_7 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 "username_7". "username_7" 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.

> **Comments:**
> **username_9**: As comments aren't intended for a discussion like this, I've [moved your comments to chat](https://chat.stackexchange.com/rooms/112147/discussion-on-answer-by-buffy-concept-of-variable) and you're welcome to continue the discussion there.  
> **username_1**: 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.  

---

### Answer by username_10 on 2020-08-27. Score: 2

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 @username_5 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 @username_2 and @username_7 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](http://www.pythontutor.com/) as this will let you "see" it in action using a range of languages.

[![Multi-language visualizer](https://i.sstatic.net/v01Kq.png)](https://i.sstatic.net/v01Kq.png)

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

[![Variables have a value, type etc.](https://i.sstatic.net/pezkE.png)](https://i.sstatic.net/pezkE.png)

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

> **Comments:**
> **username_1**: Spreadsheets are the most used computer application in the world, last I heard. Certainly seems like an important 'megaphor' in computing.  

---

