<p>This question reminds me of when a student failed to understand why a third variable was needed to exchange the value of two variables (i.e. to swap A and B,  C=A, A=B, B=C). I managed to illustrate this to them by placing an item (mobile phone) in each of their hands, and asking them to swap the items between the hands. On attempting to do this, they immediately realised they needed a third location (the table) to achieve this.</p>
<p>You may find your list-swapping activity works better if you <em>place the pencils in students' hands</em>. This will help reinforce the idea that each pencil needs a location (hand) to be stored in at all times. You'll obviously need more than one student to make a list that's larger than two pencils, and pandemic-related distancing may be an obstacle.</p>
<p>I'd suggest trying this activity with two or three students sat in a row, and using an odd number of pencils. This will ensure that there is only one hand available for moving pencils about at any time, without appearing to be an arbitrary rule that you've introduced.</p>

CC BY-SA 4.0
2022-01-07T17:43:38.717Z
Neat! Hand not the mover (verb) but the place (noun) 
Rushi
CC BY-SA 4.0
2022-01-07T15:39:12.500Z
2022-01-09T13:33:59.570Z
2022-01-09T13:33:59.570Z
Kaz
<blockquote>
<p>They took both their hands, picked up the second and fourth pencils (each in one hand), and swapped them! That works in reality... but isn't right in computer science.</p>
</blockquote>
<p>It depends on your paradigm.  You are teaching the swap algorithm you know, but haven't explained to the students (or at least in the question) what things are.   Here is a perfectly valid way to look at the operation, using two temp variables, 'Left Hand' and 'Right Hand':</p>
<pre><code>Second Item -&gt; Left Hand
Fourth Item -&gt; Right Hand
Left Hand -&gt; Fourth spot
Right Hand -&gt; Second spot
</code></pre>
<p>In fact many students will do it this way (with two temp variables) when they attempt to do swapping for the first time, I'm pretty sure I did.  Some languages have swap operations built-in so you don't require a temporary variable.</p>
<p>In this particular case I think it would make more sense to say that you can only move one pencil at a time rather than you have only one hand.  After all I could pick up the pencil in spot two and place it in slot four while picking up the pencil there at the same time with one hand.</p>
<p>The analogy breaks down in other ways, for instance normally you make a copy of the item in the temp variable and it exists in two places until you overwrite it in the array.  I think using things like colored blocks here and making copies into variables would be a better way to visualize what is actually happening too...</p>

CC BY-SA 4.0
2022-01-05T18:47:02.550Z
2022-01-05T18:47:02.550Z
Jason Goemaat
<p>Analogies &amp; metaphors are imperfect, so let's share the expectation that they are just illustrative.</p>
<p>Rules of what is possible (and not) are very important, and we can compare the rules of pencils moved with hands and what computers can do — but we need to define both, in part so the rules for the metaphor can be compared with the those of a computer.</p>
<p>These rules can be reasonably terse and simple to state.</p>
<p>The computer can copy a value from one storage location to another storage location.  (Computers generally copy data rather than moving it.)</p>
<p>Your hand can simulate action of making a copy, and whether the hand itself represent another storage location that can be utilized while doing something else is a matter of definition and explanation of rules.</p>
<p>Since we're talking about information rather than physical items, I might prefer a metaphor of a spreadsheet: swapping two cells in a spreadsheet generally requires another free cell; a pencil and piece of paper with some boxes holding symbols might also work, you need to stipulate whether human memory is outside of the metaphor (i.e. not be used for storage).</p>
<p>Programming languages support constructs &amp; expressions that require additional implicit storage, and language implementations introduced temporaries for these purposes: Python's <code>a,b=b,a</code> is such a construct, while any language's general purpose expressions is another: you can't really compute <code>a*b+c*d</code> without holding the result of one of the multiplications somewhere (on paper, in your head, in storage...), while doing the other.</p>

CC BY-SA 4.0
2022-01-06T03:45:22.300Z
"a * b + c * d" : lovely! This insight is at the heart of functional programming : elide useless names. The more technical name for that is *combinatory logic*. (The proper name "Haskell" is the name of its second founder)
Rushi
CC BY-SA 4.0
2022-01-07T02:11:53.787Z
@Rusi Are there any Functional Machines? Computers that execute your high level idea of the calculation and not some lower level translation? I would like to see such a machine. I would also like to get to work using a map instead of a car, but you can't have everything. (Where would you put it?) Brains really do seem to implement a Neural Network, but computers not so much. Some day perhaps.
Scott Rowe
CC BY-SA 4.0
2022-01-07T02:51:57.657Z
@Scott: [Lisp machine](https://en.m.wikipedia.org/wiki/Lisp_machine), [Scheme88](https://dspace.mit.edu/handle/1721.1/6042). In fact [Data flow architectures](https://en.m.wikipedia.org/wiki/Dataflow_architecture) were actually made for improving efficiency of von Neumann languages eg [SISAL](https://en.m.wikipedia.org/wiki/SISAL). In fact among theoretical models, lambda calculus is more realiable than Turing machines because the latter has an infinite sub part -- the tape. How the F does an engineer make an infinite object?! Nothing infinite about the lambda calculus. Evolution...... is slow
Rushi
CC BY-SA 4.0
2022-01-07T03:23:15.937Z
@ScottRowe All that FP does is generalize "a * b + c * d" from numbers to arbitrary memory data structures. Heck even python Java C# etc -- the whole current mainstream gang -- will allow you to handle complex data structures, *manage* ing the memory with gc etc. Only difference is the modern crop [mess up memory semantics](https://cseducators.stackexchange.com/a/6567/8837). FPLs don't. Java, C#, JS are identical modulo syntax and minor details.
Rushi
CC BY-SA 4.0
2022-01-07T03:42:27.650Z
Of course **IF** you're prepared to go outside the mainstream there are heroic exceptions. Eg Rust which strives for C-level efficiency, memory-footprint. Plus almost haskellish purity. And is even highly portable (Compare to C's autoconf!!!). **IF**... Are you?? (In 1987 [I pioneered](https://dl.acm.org/doi/10.1145/126459.126471) and initiated C in edu. It was as new and uncharted then as it's passe now. Today I would use rust for that space (if I were younger... If...))"
Rushi
CC BY-SA 4.0
2022-01-07T11:28:55.313Z
@Rusi I learned C in college in 1986, I think. My point was that since real machines can be explained as being something like a simple RISC / registers CPU, maybe education should start by making that model clear? It's nice to have a common reference for understanding new things. After people know the "5 operations" model, we can add to it.
Scott Rowe
CC BY-SA 4.0
2022-01-07T13:05:13.093Z
@ScottRowe "Can be explained" = "Best explained"??
Rushi
CC BY-SA 4.0
2022-01-07T17:09:37.703Z
@Rusi The Question that got me started learning about computers when I was 7, was when I wondered "*How could such a thing possibly work?*" If you don't answer that, you have said nothing. Virtus Dormitiva and all.
Scott Rowe
CC BY-SA 4.0
2022-01-06T01:21:58.080Z
2022-01-06T01:21:58.080Z
Erik Eidt
<p>I do the swap example pretending to have two largish (2-hands-to-lift) boxes side-by-side on a desk (with only enough room for two boxes). After we lift the first we have to look for a place to put it, which feels like creating the temp variable.</p>
<p>But I don't think swap is an especially good example with lists. It's easier to explain swapping just any two variables, x and y. Lists are about using indexes. In Scratch we don't get as much fun with loops, but can do 2 step find-the-index-first stuff like &quot;turn the first 9 into a 6&quot; or &quot;put 12's before and after the first 3&quot;.</p>
<p>As far as &quot;computer rules&quot; I like a lighter touch. Swap is one of many clever tricks which happen to work, for now. Motivate the problem with how switching x and y starting with &quot;set x to y&quot; permanently erases x. Then explain how that 3-step dance is actually the standard swap trick. We could have had a built-in swap command but everyone was all &quot;no, everyone knows the trick and it's just one extra step&quot;.</p>
<p>Esp. in Scratch you're hoping they like it and move onto a real language. At that point you're going to explain how &quot;Scratch rules&quot; like pre-making variables aren't computer rules at all. But we learned lots of concepts which mostly transfer over with minor changes. In the end: switching values of two variables is a thing; everyone calls it a swap; and it's good to know the trick just-in-case there's no built-in way.</p>

CC BY-SA 4.0
2022-01-05T20:43:38.693Z
well... to be fair you *do* need to "pre-make" variables in most languages...
somebody
CC BY-SA 4.0
2022-01-05T23:27:51.240Z
@somebody My understanding is the Scratch language has no declarations -- variables are created manually through the GUI. I assume a Scratch user learning a text-based language would be surprised at something like *int x=4*. But I've only seen Scratch a few times, is there a better example?
Owen Reynolds
CC BY-SA 4.0
2022-01-05T23:47:32.063Z
well. while that's true i'd argue that scratch variables are all global, and global variable declarations in other languages are little different from that of scratch - you still "create variables manually". to look at it another way, in scratch, not just variables but *everything* is created "through the GUI", no? (equivalently, scratch has *no (official) textual syntax* at all). but either way the point is, imo "pre-making variables" (and scratch in general) is little different from the so-called "computer rules"
somebody
CC BY-SA 4.0
2022-01-05T23:48:28.723Z
tl;dr imo how variables are created in scratch are mostly a side effect of being a graphical (?) language rather than it being fundamentally different from other lanugages
somebody
CC BY-SA 4.0
2022-01-05T17:51:04.250Z
2022-01-05T17:51:04.250Z
Owen Reynolds
<p>In thinking about this, and especially <a href="https://cseducators.stackexchange.com/a/7211/1293">the answer of Graham</a>, I think that what we really do in the standard three step process <em>is</em> a two handed swap without thinking of it. So, it is a failure of the metaphor, actually. So, here is a reframing of the standard process:</p>
<p>Pick up one stick with the left hand (i.e. temp is associated with the left hand). THEN, move the other stick with the right hand (the assignment). THEN put the stick in the left hand into the proper slot.</p>
<p>Or:</p>
<p>Second Item -&gt; Left Hand
Fourth Item -&gt; Second spot // using Right Hand
Left Hand -&gt; Fourth spot</p>
<p>All you need to explain is that (with a single processor) only one thing can be done at a time and so the swap-with-two-hands has to be sequenced in some way rather than happening simultaneously.</p>
<p>This is to avoid issues in the more general case where you want to swap(m,n) where m and n could be the same. Both hands can't pick up the same stick simultaneously if it has to be sequenced. The bright kids will want to complain that you can't pick up a stick from an empty slot, but the small variation isn't too hard to explain if you think of it beforehand. The really astute ones will notice that assignment is copying, so &quot;picking up one stick&quot; in the program doesn't really leave that slot empty, as it does with physical objects.</p>
<p>(ASIDE: there is a way to avoid that physical/virtual disconnect by using a reference (rather than copy) metaphor of variables, but it is likely not where you want to go with youngsters unless you do it ubiquitously. In essence, a variable is a reference to a value not a box that &quot;holds&quot; the value.)</p>
<p>It also avoids discussions of race conditions with multiple processors.</p>

CC BY-SA 4.0
2022-01-06T16:19:27.320Z
Why does "single processor" keep getting brought up?  How would you sequence operations by 2 cores to get a swap done correctly?  The relevant thing here is a *scalar* CPU, as opposed to modern superscalar CPU cores that literally can execute two independent load instructions in the same clock cycle.  (e.g. AMD since K8, Intel since Sandybridge: https://www.realworldtech.com/sandy-bridge/7/).  Intel since Ice Lake can even execute two store-data uops in the same clock cycle, although commit from store-buffer to L1d cache can only commit two stores in 1 cycle if they're to the same line.
Peter Cordes
CC BY-SA 4.0
2022-01-06T16:27:05.180Z
As you say, a normal swap is 2 loads then 2 stores (assuming an optimizing compiler for an AoT compiled language for a register machine), whether we name one or both temporaries.  (Or in Python, neither).  For (much) more advanced students of CPU architecture, this might be a good illustration of hazards like write-after-read when finding parallelism between operations: you have to finish picking up an object (loading into regs) before a store of the other object writes that location.
Peter Cordes
CC BY-SA 4.0
2022-01-06T17:10:17.373Z
@PeterCordes *"Why does "single processor" keep getting brought up?"* Not because using multiple processors or multiple threads would enable a different solution, but because if you are explaining to a student why they have to solve the problem with one hand then the analogy is justified by the fact that their program can "only do one thing at a time".
kaya3
CC BY-SA 4.0
2022-01-06T18:17:38.983Z
@kaya3: Thanks, you're right, I was thinking in terms of finding instruction-level parallelism within instructions for a sequential model of execution, not the need for there to be a sequence in the first place.  I think I'd find it less weird to say the code or program is single-threaded, rather than to say there's only one processor to execute it on.  As you rightly pointed out, the issue here is the execution model of the programming language, not the CPU details.
Peter Cordes
CC BY-SA 4.0
2022-01-07T02:19:53.797Z
@PeterCordes Because if you ask a centipede how it knows which leg to move next, it gets in a hopeless tangle. (That's just a metaphor, of course.)
Scott Rowe
CC BY-SA 4.0
2022-01-05T14:36:04.753Z
2022-01-05T14:36:04.753Z
Buffy
<p>Ben's answer is great, but allow me to suggest that you don't need to &quot;respond&quot; to the two-handed approach at all: just say when you're explaining the problem in the first place that they can only use one hand and they can only hold one item at a time.</p>
<p>Students are less likely to need a justification this way, because you haven't changed the rules to forbid a solution after they've come up with it themselves. If a justification is required, the question is not <em>&quot;why can't I use two hands?&quot;</em>, but rather <em>&quot;why is it useful to know how to do this with one hand?&quot;</em>. And the answer to that is that the solution using one hand can be translated directly into code, whereas the solution with two hands can't be.</p>

CC BY-SA 4.0
2022-01-05T13:36:37.247Z
Except you perfectly well can translate the solution with two hands into code.  Not picking up both simultaneously, sure - at least on most systems - but one at a time works fine.
Graham
CC BY-SA 4.0
2022-01-05T20:49:23.400Z
except you *cannot* just let it slide without explanation - it's *extremely* important to make sure the student is understanding it how you think they are. so you'd at the very least have to explain (and get the student to agree) that they are describing a 4 step process `left = a; right = b; a = right; b = left` rather than a 1 step process `a, b = b, a`. the problem with this is that it runs the same risk of the student asking why the process must be split into those steps.
somebody
CC BY-SA 4.0
2022-01-05T22:51:01.507Z
@Graham By "the solution with two hands" I refer to the one in the OP where a student picks up both items with both hands at the same time. If you interpret hands as variables rather than as the computer's action of moving (or really, copying) a value from one variable to another, then there are other two-handed solutions, but "the" two-handed solution still doesn't work. As for "on some systems", we are talking about whatever language the students are learning, not the capabilities of all programming languages.
kaya3
CC BY-SA 4.0
2022-01-07T02:29:51.750Z
Maybe someday we will build computers that can do many things at once. Quantum, massively parallel, whatever. Then we will not be able to teach anyone how to program them, but we won't have to.
Scott Rowe
CC BY-SA 4.0
2022-01-07T06:03:50.490Z
@ScottRowe I don't know what future you're imagining where nobody is a computer programmer, but I want no part of it - I like programming!
kaya3
CC BY-SA 4.0
2022-01-07T11:34:13.840Z
@kaya3 I liked using a darkroom, but Kodak didn't think it was worth producing film, paper and chemicals just for me. So schools don't teach it anymore. Can you sail a boat or ride a horse or repair a Zenith Trans-Oceanic without a schematic? I could, but opportunities are few now. Starting a car with a pencil not likely possible now.
Scott Rowe
CC BY-SA 4.0
2022-01-07T11:36:41.970Z
I liked your answer by the way, I just wanted to say why the one-hand explanation is necessary. But clearly it won't be for long. Lord help us then!
Scott Rowe
CC BY-SA 4.0
2022-01-05T01:19:45.787Z
2022-01-05T01:28:07.967Z
2022-01-05T01:28:07.967Z
kaya3
<p><a href="https://cseducators.stackexchange.com/a/7211/8837">Graham</a> pointed out that the assumtions of the question are wrong.  And <a href="https://cseducators.stackexchange.com/questions/7205/how-to-respond-to-the-two-handed-approach-to-swapping#comment18833_7212">Peter Cordes</a> elaborated a bit in the comments.</p>
<p>Considering a target <a href="https://en.m.wikipedia.org/wiki/Very_long_instruction_word" rel="nofollow noreferrer">VLIW</a> (Very Long Instruction Word)
architecture can help sharpen this argument further.</p>
<p>The basic idea of VLIW is that a Very Long Instruction &quot;horizontally&quot; packs a number of instructions per single VL-instruction, all of which are executed in parallel in one machine cycle. The requirement is that the multiple instructions in a VL-instruction need to access disjoint memory/registers.</p>
<p>In terms of more popular architectures, VLIW is:</p>
<ul>
<li>Like pipelined superscalar architectures except that the compiler rather than the hardware does parallelization scheduling</li>
<li>Like RISC in that the complexity is shifted to the compiler</li>
</ul>
<p>So</p>
<pre><code>x = 1
y = 2
</code></pre>
<p>can be packed as one VL-instruction</p>
<pre><code> x = 1 // y = 2
</code></pre>
<p>Whereas</p>
<pre><code>x = 1
y = x + 1
</code></pre>
<p>can't because x overlaps.</p>
<p>(Using the notation that multiple VL instructions are on multiple lines and &quot;subinstructions&quot; packed into a VL instruction are on one line &quot;//&quot; separated).</p>
<p>With that background we can write swap as</p>
<pre><code>lh = x // rh = y
x = rh // y = lh
</code></pre>
<p>Whereas your preferred solution</p>
<pre><code>t = x 
x = y
y = t
</code></pre>
<p>is interlocked at each stage and so is hopelessly sequential.</p>
<h2>tl;dr</h2>
<p>The VLIW oriented swap is 33% faster — 2 vs 3 — than the standard imperative one. When it is naively sequentialized it's 25% slower — 4 vs 3.</p>
<h2>Note</h2>
<p>The two-hand analogy is right in being more efficient than with one hand. However it's not so right in this that it seems to be able to do it in <em>one</em> single 2-handed swoop. Whereas (for VLIW at least) it's <em>two</em> 2-handed swoops. Our hands have enough muscle intelligence to criss-cross without colliding whereas data-buses dynamically hop-skipping over each other is..  uh... still futuristic sci-fi.<br />
IOW analogies/metaphors may help but don't go all the way.</p>

CC BY-SA 4.0
2022-01-13T01:40:39.433Z
Premature optimization is... Oh, you already heard that one. Could we venture to teach beginning students something without having to teach them everything? I had a great welding instructor, he could cover one topic or technique each class night. I still think of him as one of the best teachers I had, and still smile when I recall him whistling during class as he thought about going out with his wife afterwards.
Scott Rowe
CC BY-SA 4.0
2022-01-13T02:54:53.760Z
 @ScottRowe. In upholding the "Premature..." proverb you'll find me more extreme left (or is it right?) than anyone. The point of this post was.... O well... May the blessing of [Fermat](https://en.m.wikipedia.org/wiki/Fermat%27s_Last_Theorem) be on these comment columns...
Rushi
CC BY-SA 4.0
2022-01-09T03:41:59.147Z
2022-01-13T10:12:57.183Z
2022-01-13T10:12:57.183Z
Rushi
<p>Don't overcomplicate things.  Your first instinct, that the computer only has one hand, was the correct one.</p>
<p>This is a regular problem that new teachers have, and it sometimes takes a few years of frustration to burn out the idea that everything you say must be formally <em>true</em>.  If we say something that is <em>untrue</em>, the reasoning goes, the students will remember it and use that wrong idea to badly misunderstand things later.</p>
<p>Let me give you a slightly different mental model of learning, and you'll find all of the support you need in the <em>absolutely fantastic</em> book &quot;How People Learn II&quot;, which is a big volume that describes everything we really know from research about how learning actually works in the brain.</p>
<p>We (1) build mental models by (2) connecting new information (3) to things that we already know.  Furthermore, (4) abstraction is the brain-process of finding the similarity in (5) similar mental models.  (6) In order to build upon prior knowledge, (7) it must already be fairly well understood.  If it's not, and you simply don't have the prerequisite knowledge, you are unlikely to build any mental model at all.</p>
<p>(That should make some sense; if it's not already fairly concrete within your mind, it's too nebulous to glom new ideas onto.)</p>
<p>So, with that as a model of learning, there are a few implications:</p>
<ol>
<li>We need to help students build small, clear mental models.  They can then use these in the future to build further, richer models.  This lends itself very nicely to the KISS principle.  Don't try to introduce too many variations to the idea at once.  Leave the complicating for the next step, or you won't obtain any model at all.</li>
<li>Before there is already a fairly clear mental model, learning isn't really taking place, so little of what is said will be directly remembered.  Corollary to this is that the danger of them being confused by some overly literal interpretation of what you said while explaining how to swap in Scratch once they encounter threading later down the line is completely negligible.  They won't remember much of the explanation until they can build a mental model, so they won't remember those kinds of details. In addition, there are simply too many cognitive models that have to be built between swap in Scratch and multithreading for one to hamper the other.</li>
</ol>
<p>Don't let perfect be the enemy of progress.  Keep it simple and focused on the small concept they are learning <em>right now</em>, and be comfortable that that will support them best as they grow and develop and complexify ideas later.</p>

CC BY-SA 4.0
2022-01-04T20:29:01.607Z
Let me add that to justify using the simple (sequential, explicit temp) model, ask the students to think about all the ways the two handed model could go wrong. Especially how the simple model doesn't have those defects.
Buffy
CC BY-SA 4.0
2022-01-06T20:42:47.957Z
It would also be fine to add the caveat that "right now in our example, the computer only has one hand." This would be less technically incorrect without distracting the student too much, I think.
2rs2ts
CC BY-SA 4.0
2022-01-06T22:32:39.097Z
@2rs2ts Sure! But realize that you do that basically for your own benefit. It won't help the student, and *might*, by admitting that more complex layers exist, hinder them in their current struggle. But I don't want to sound too negative; I play those sorts of word games every day, and could scarcely stop it if I tried.  After all, it bothers *me* to say it wrong.  I just have to be cautious about how much additional complexity I allow into a model that the student *already isn't getting* (ie they're already at the edge of their cognitive limits). In those moments, when in doubt, leave it out.
Ben I.
CC BY-SA 4.0
2022-01-07T02:04:34.243Z
One name for the "small, clear mental model" you mentioned is **Notional Machine**, as someone else commented. This idea is why I often say that we have to teach people *how computers actually work*. I say: computers can only do 5 things - fetch, store, arithmetic, compare and jump. With that in mind, students are more likely to understand programming than if we start with something other than a Notional Machine.
Scott Rowe
CC BY-SA 4.0
2022-01-04T20:22:41.430Z
2022-01-05T05:59:03.663Z
2022-01-05T05:59:03.663Z
Ben I.
<h3>Re-evaluate what you think is &quot;right&quot;, because you aren't</h3>
<blockquote>
<p>but isn't right in computer science</p>
</blockquote>
<p>This is simply incorrect.  Your student can create two variables called <code>LeftHand</code> and <code>RightHand</code>.  Copy value &quot;red&quot; from position 1 to <code>LeftHand</code>, copy value &quot;green&quot; from position 4 to <code>RightHand</code>, copy values back to positions 4 and 1 respectively.  Job done.</p>
<p>Is this optimal?  No - but you didn't ask for optimal, you asked for a solution which works.  Your student gave you a working solution, and you've rejected it because of your own preconceptions of how many variables they &quot;should&quot; use, or how many steps it &quot;should&quot; take.  Neither of those preconceptions are correct.  There's no time or RAM pressures here, so the student is perfectly free to do it their way. (Edit following your comments elsewhere: Nor do they have to consider concurrency where someone else could move the pencils in between.)</p>
<p>And not only are they free to do it this way - if they find it easier to understand then they <em><strong>should</strong></em>.  My background is in safety-related software.  Coding standards in safety-related software prioritise readability, maintainability and ease of understanding by other coders, ahead of pure speed, because the most intractable bugs almost always come from people trying to be too clever.  If you keep the concepts simple, you don't make mistakes and your code is more likely to work first time.</p>
<p>In short, you are committing a cardinal mistake in software engineering, which is premature optimisation (which as every software engineer should know <a href="https://wiki.c2.com/?PrematureOptimization" rel="noreferrer">is the root of all evil</a>).  Your student is not making that mistake, and that currently makes them a <em><strong>better</strong></em> software engineer than you!</p>
<p>Certainly after they've got a two-handed solution, you can have them do a one-handed version with a single slot.  You can then show them that it takes fewer steps and one less variable.  The answer to &quot;why can't we...?&quot; is simply &quot;you can if you want, but look, it's more work&quot;.  Most people would rather do less work, so it's a win all round.</p>

CC BY-SA 4.0
2022-01-07T22:58:24.017Z
Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/132991/discussion-on-answer-by-graham-how-to-respond-to-the-two-handed-approach-to-swap).
Ben I.
CC BY-SA 4.0
2022-01-09T05:55:20.763Z
I've added an answer that shows your LeftHand, RightHand version as actually better — for one extant architecture — than the OP's : https://cseducators.stackexchange.com/a/7223/8837
Rushi
CC BY-SA 4.0
2022-01-13T21:27:56.680Z
The gcc compiler happily turns the "standard one temp variable"  in C into a 2 registers "both hands" versions. Probably because it is more efficient (read-write, cache...)
Michel Billaud
CC BY-SA 4.0
2022-01-18T04:09:43.603Z
@Michelbillaud That's really worth an answer! Hard data that the less efficient notionally is the more efficient actually. Just (1) input program (2) output cut down to relevant parts (3) gcc flags if any (4) gcc version just in case
Rushi
CC BY-SA 4.0
2022-01-21T09:30:31.370Z
Well compiling this with gcc -03 -S

`void swap(int *a, int *b) { int c = *a; *a = *b; *b = c;  }`

produces this assembler code `movl (%rdi), %eax ; movl (%rsi), %edx ; movl %edx, (%rdi) ; movl %eax, (%rsi) ; ret`
Michel Billaud
CC BY-SA 4.0
2022-01-05T10:55:48.853Z
2022-01-05T14:11:38.333Z
2022-01-05T14:11:38.333Z
Graham
<p>Your problem is that you tried to let your student &quot;program&quot; without establishing a language. Thus they were free to write their own &quot;language&quot; in which you can do two things at once because you have two hands. If you'd asked them to rotate 3 objects, they would have picked up the third pencil in their mouth or so.</p>
<p>You could have asked them &quot;suppose you're a crane driver&quot; then, because they obviously have only one crane, they would have solved the problem &quot;correctly&quot;.</p>
<p>May this also be a lesson to you to think very hard about everything you ask your students. Don't improvise. Or just learn for next time.</p>

CC BY-SA 4.0
2022-01-04T23:27:01.803Z
I do like your crane idea, and in hindsight, I should have done that... however, how do you come up with these ideas for alternate ways to phrase problems to get students to think about it in a certain way or come to a specific answer?
cocomac
CC BY-SA 4.0
2022-01-05T01:41:46.167Z
I actually ran into a very similar problem, but it was about communcating processes. After a student uses the two-handed solution, I now tell them, "you have only one communication channel, so put your left hand in your pocket, and use your right hand". So I had to learn the hard way, like you.
Victor Eijkhout
CC BY-SA 4.0
2022-01-05T11:23:39.220Z
@cocomac -- apart from the obvious canned answer of "practice will do it" that, although true, doesn't help you up front :-), mostly just by going the opposite direction. To think about it first, like "I don't want them to use both hands, so I need to come up with some setup that makes it one hand only, so I need an interface between two-handed people and one-handed manipulator, let me see".
Gábor
CC BY-SA 4.0
2022-01-05T18:31:09.450Z
This is a good answer, but it has an issue. Some cranes have the ability to "double pick", and so does a hand. This means the student could pick up both pencils with the same hand at the same time, then either swap them with some movement of their fingers (FIFO) or place the 2nd pencil before placing the 1st pencil they picked up (FILO). As this answer says, this could be solved by specifying the language or methods allowed for movement of the pencils.
computercarguy
CC BY-SA 4.0
2022-01-05T20:05:17.117Z
@computercarguy And I'm sure there are people who can juggle with one hand, so they could still do the swap without a temporary variable. Analogies are always imprecise.
Victor Eijkhout
CC BY-SA 4.0
2022-01-05T20:23:18.423Z
@VictorEijkhout, juggling usually means you can handle more than one object with one hand. I've juggled for over 35 years and can do 3 balls in one hand, for example. So yes, the analogy is imprecise, but that doesn't mean it can't still work. I said there was an issue, not a problem. I should have been more precise that it wasn't a critical reason not to use the analogy. This issue with the analogy can easily be avoided by specifying the language, as you say, by not allowing double-picking. We just need to know how to specify the language to avoid these issues, that's all.
computercarguy
CC BY-SA 4.0
2022-01-07T02:26:08.563Z
Notional Machine is the operative concept here. If you don't define it for your students, how can they hope to understand anything?
Scott Rowe
CC BY-SA 4.0
2022-01-04T23:12:44.703Z
2022-01-05T11:35:41.853Z
2022-01-05T11:35:41.853Z
Victor Eijkhout
<p>I was introducing lists to someone young (in <a href="https://en.wikipedia.org/wiki/Scratch_%28programming_language%29" rel="nofollow noreferrer">Scratch</a>). Creating a list is boring, so I opted to introduce something that you can do with lists, but requires some thinking: swapping. I introduced it by putting out four pencils in a row, like this. I asked them to swap the second and fourth items in the array (in reality).</p>
<pre><code>| | | |
</code></pre>
<p>They took both their hands, picked up the second and fourth pencils (each in one hand), and swapped them! That works in reality... but isn't right in computer science. So, I told them, try doing it with only one hand. Predictably, they couldn't. I introduced a temporary spot, where they could put one pencil. I was hoping they would do something like this:</p>
<pre><code>Second Item -&gt; Temp spot
Fourth Item -&gt; Second spot
Temp spot -&gt; Fourth spot
</code></pre>
<p>They figured it out. But they asked me something like this: &quot;Why can't I use two hands?&quot;. I wasn't sure how to answer that. The obvious answer is... <em>the computer only has one hand</em> (this was <a href="https://scratch.mit.edu/" rel="nofollow noreferrer">Scratch</a>, so multithreading didn't exist). But... I'm hesitant to say that for two reasons: (1) in Python, the &quot;two-hands&quot; approach works (<code>a, b = b, a</code>, see <a href="https://stackoverflow.com/a/24844556/16886597">this Stack Overflow question</a>) and (2) multithreading exists, and there are some (obscure) ways to swap two variables without a temporary variable. So, how should I answer the question &quot;why can't I use two hands&quot;?</p>
<p>Remember, they are just learning lists, so explaining something like multithreading would be too difficult.</p>
<p>What's a good way to answer that question, without confusing them?</p>

CC BY-SA 4.0
2022-01-07T22:58:20.953Z
Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/132990/discussion-on-question-by-cocomac-how-to-respond-to-the-two-handed-approach-to-s).
Ben I.
<teaching-analogy><computational-thinking><arrays><primary-school><concurrency>
How to respond to the two-handed approach to swapping
# How to respond to the two-handed approach to swapping

(Asked by: username_0 on 2022-01-04)

I was introducing lists to someone young (in Scratch). Creating a list is boring, so I opted to introduce something that you can do with lists, but requires some thinking: swapping. I introduced it by putting out four pencils in a row, like this. I asked them to swap the second and fourth items in the array (in reality).
| | | |

They took both their hands, picked up the second and fourth pencils (each in one hand), and swapped them! That works in reality... but isn't right in computer science. So, I told them, try doing it with only one hand. Predictably, they couldn't. I introduced a temporary spot, where they could put one pencil. I was hoping they would do something like this:
Second Item -> Temp spot
Fourth Item -> Second spot
Temp spot -> Fourth spot

They figured it out. But they asked me something like this: "Why can't I use two hands?". I wasn't sure how to answer that. The obvious answer is... the computer only has one hand (this was Scratch, so multithreading didn't exist). But... I'm hesitant to say that for two reasons: (1) in Python, the "two-hands" approach works (a, b = b, a, see this Stack Overflow question) and (2) multithreading exists, and there are some (obscure) ways to swap two variables without a temporary variable. So, how should I answer the question "why can't I use two hands"?
Remember, they are just learning lists, so explaining something like multithreading would be too difficult.
What's a good way to answer that question, without confusing them?

**Tags:** teaching-analogy, computational-thinking, arrays, primary-school, concurrency

> **Comments:**
> **username_1**: Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/132990/discussion-on-question-by-username_0-how-to-respond-to-the-two-handed-approach-to-s).  

---

## 10 answers

### Answer by username_1 on 2022-01-04. Score: 27 | Accepted answer

Don't overcomplicate things. Your first instinct, that the computer only has one hand, was the correct one.

This is a regular problem that new teachers have, and it sometimes takes a few years of frustration to burn out the idea that everything you say must be formally *true*. If we say something that is *untrue*, the reasoning goes, the students will remember it and use that wrong idea to badly misunderstand things later.

Let me give you a slightly different mental model of learning, and you'll find all of the support you need in the *absolutely fantastic* book "How People Learn II", which is a big volume that describes everything we really know from research about how learning actually works in the brain.

We (1) build mental models by (2) connecting new information (3) to things that we already know. Furthermore, (4) abstraction is the brain-process of finding the similarity in (5) similar mental models. (6) In order to build upon prior knowledge, (7) it must already be fairly well understood. If it's not, and you simply don't have the prerequisite knowledge, you are unlikely to build any mental model at all.

(That should make some sense; if it's not already fairly concrete within your mind, it's too nebulous to glom new ideas onto.)

So, with that as a model of learning, there are a few implications:

1. We need to help students build small, clear mental models. They can then use these in the future to build further, richer models. This lends itself very nicely to the KISS principle. Don't try to introduce too many variations to the idea at once. Leave the complicating for the next step, or you won't obtain any model at all.
2. Before there is already a fairly clear mental model, learning isn't really taking place, so little of what is said will be directly remembered. Corollary to this is that the danger of them being confused by some overly literal interpretation of what you said while explaining how to swap in Scratch once they encounter threading later down the line is completely negligible. They won't remember much of the explanation until they can build a mental model, so they won't remember those kinds of details. In addition, there are simply too many cognitive models that have to be built between swap in Scratch and multithreading for one to hamper the other.

Don't let perfect be the enemy of progress. Keep it simple and focused on the small concept they are learning *right now*, and be comfortable that that will support them best as they grow and develop and complexify ideas later.

> **Comments:**
> **username_9**: Let me add that to justify using the simple (sequential, explicit temp) model, ask the students to think about all the ways the two handed model could go wrong. Especially how the simple model doesn't have those defects.  
> **username_13**: It would also be fine to add the caveat that "right now in our example, the computer only has one hand." This would be less technically incorrect without distracting the student too much, I think.  
> **username_1**: @username_13 Sure! But realize that you do that basically for your own benefit. It won't help the student, and *might*, by admitting that more complex layers exist, hinder them in their current struggle. But I don't want to sound too negative; I play those sorts of word games every day, and could scarcely stop it if I tried.  After all, it bothers *me* to say it wrong.  I just have to be cautious about how much additional complexity I allow into a model that the student *already isn't getting* (ie they're already at the edge of their cognitive limits). In those moments, when in doubt, leave it out.  
> **username_6**: One name for the "small, clear mental model" you mentioned is **Notional Machine**, as someone else commented. This idea is why I often say that we have to teach people *how computers actually work*. I say: computers can only do 5 things - fetch, store, arithmetic, compare and jump. With that in mind, students are more likely to understand programming than if we start with something other than a Notional Machine.  

---

### Answer by username_12 on 2022-01-05. Score: 22

### Re-evaluate what you think is "right", because you aren't

> but isn't right in computer science

This is simply incorrect. Your student can create two variables called `LeftHand` and `RightHand`. Copy value "red" from position 1 to `LeftHand`, copy value "green" from position 4 to `RightHand`, copy values back to positions 4 and 1 respectively. Job done.

Is this optimal? No - but you didn't ask for optimal, you asked for a solution which works. Your student gave you a working solution, and you've rejected it because of your own preconceptions of how many variables they "should" use, or how many steps it "should" take. Neither of those preconceptions are correct. There's no time or RAM pressures here, so the student is perfectly free to do it their way. (Edit following your comments elsewhere: Nor do they have to consider concurrency where someone else could move the pencils in between.)

And not only are they free to do it this way - if they find it easier to understand then they ***should***. My background is in safety-related software. Coding standards in safety-related software prioritise readability, maintainability and ease of understanding by other coders, ahead of pure speed, because the most intractable bugs almost always come from people trying to be too clever. If you keep the concepts simple, you don't make mistakes and your code is more likely to work first time.

In short, you are committing a cardinal mistake in software engineering, which is premature optimisation (which as every software engineer should know [is the root of all evil](https://wiki.c2.com/?PrematureOptimization)). Your student is not making that mistake, and that currently makes them a ***better*** software engineer than you!

Certainly after they've got a two-handed solution, you can have them do a one-handed version with a single slot. You can then show them that it takes fewer steps and one less variable. The answer to "why can't we...?" is simply "you can if you want, but look, it's more work". Most people would rather do less work, so it's a win all round.

> **Comments:**
> **username_1**: Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/132991/discussion-on-answer-by-graham-how-to-respond-to-the-two-handed-approach-to-swap).  
> **username_3**: I've added an answer that shows your LeftHand, RightHand version as actually better — for one extant architecture — than the OP's : https://cseducators.stackexchange.com/a/7223/8837  
> **username_14**: The gcc compiler happily turns the "standard one temp variable"  in C into a 2 registers "both hands" versions. Probably because it is more efficient (read-write, cache...)  
> **username_3**: @Michelbillaud That's really worth an answer! Hard data that the less efficient notionally is the more efficient actually. Just (1) input program (2) output cut down to relevant parts (3) gcc flags if any (4) gcc version just in case  
> **username_14**: Well compiling this with gcc -03 -S  `void swap(int *a, int *b) { int c = *a; *a = *b; *b = c;  }`  produces this assembler code `movl (%rdi), %eax ; movl (%rsi), %edx ; movl %edx, (%rdi) ; movl %eax, (%rsi) ; ret`  

---

### Answer by username_15 on 2022-01-04. Score: 17

Your problem is that you tried to let your student "program" without establishing a language. Thus they were free to write their own "language" in which you can do two things at once because you have two hands. If you'd asked them to rotate 3 objects, they would have picked up the third pencil in their mouth or so.

You could have asked them "suppose you're a crane driver" then, because they obviously have only one crane, they would have solved the problem "correctly".

May this also be a lesson to you to think very hard about everything you ask your students. Don't improvise. Or just learn for next time.

> **Comments:**
> **username_0**: I do like your crane idea, and in hindsight, I should have done that... however, how do you come up with these ideas for alternate ways to phrase problems to get students to think about it in a certain way or come to a specific answer?  
> **username_15**: I actually ran into a very similar problem, but it was about communcating processes. After a student uses the two-handed solution, I now tell them, "you have only one communication channel, so put your left hand in your pocket, and use your right hand". So I had to learn the hard way, like you.  
> **username_16**: @username_0 -- apart from the obvious canned answer of "practice will do it" that, although true, doesn't help you up front :-), mostly just by going the opposite direction. To think about it first, like "I don't want them to use both hands, so I need to come up with some setup that makes it one hand only, so I need an interface between two-handed people and one-handed manipulator, let me see".  
> **username_17**: This is a good answer, but it has an issue. Some cranes have the ability to "double pick", and so does a hand. This means the student could pick up both pencils with the same hand at the same time, then either swap them with some movement of their fingers (FIFO) or place the 2nd pencil before placing the 1st pencil they picked up (FILO). As this answer says, this could be solved by specifying the language or methods allowed for movement of the pencils.  
> **username_15**: @username_17 And I'm sure there are people who can juggle with one hand, so they could still do the swap without a temporary variable. Analogies are always imprecise.  
> **username_17**: @VictorEijkhout, juggling usually means you can handle more than one object with one hand. I've juggled for over 35 years and can do 3 balls in one hand, for example. So yes, the analogy is imprecise, but that doesn't mean it can't still work. I said there was an issue, not a problem. I should have been more precise that it wasn't a critical reason not to use the analogy. This issue with the analogy can easily be avoided by specifying the language, as you say, by not allowing double-picking. We just need to know how to specify the language to avoid these issues, that's all.  
> **username_6**: Notional Machine is the operative concept here. If you don't define it for your students, how can they hope to understand anything?  

---

### Answer by username_2 on 2022-01-07. Score: 5

This question reminds me of when a student failed to understand why a third variable was needed to exchange the value of two variables (i.e. to swap A and B, C=A, A=B, B=C). I managed to illustrate this to them by placing an item (mobile phone) in each of their hands, and asking them to swap the items between the hands. On attempting to do this, they immediately realised they needed a third location (the table) to achieve this.

You may find your list-swapping activity works better if you *place the pencils in students' hands*. This will help reinforce the idea that each pencil needs a location (hand) to be stored in at all times. You'll obviously need more than one student to make a list that's larger than two pencils, and pandemic-related distancing may be an obstacle.

I'd suggest trying this activity with two or three students sat in a row, and using an odd number of pencils. This will ensure that there is only one hand available for moving pencils about at any time, without appearing to be an arbitrary rule that you've introduced.

> **Comments:**
> **username_3**: Neat! Hand not the mover (verb) but the place (noun)   

---

### Answer by username_4 on 2022-01-05. Score: 5

They took both their hands, picked up the second and fourth pencils (each in one hand), and swapped them! That works in reality... but isn't right in computer science.

It depends on your paradigm.  You are teaching the swap algorithm you know, but haven't explained to the students (or at least in the question) what things are.   Here is a perfectly valid way to look at the operation, using two temp variables, 'Left Hand' and 'Right Hand':
Second Item -> Left Hand
Fourth Item -> Right Hand
Left Hand -> Fourth spot
Right Hand -> Second spot

In fact many students will do it this way (with two temp variables) when they attempt to do swapping for the first time, I'm pretty sure I did.  Some languages have swap operations built-in so you don't require a temporary variable.
In this particular case I think it would make more sense to say that you can only move one pencil at a time rather than you have only one hand.  After all I could pick up the pencil in spot two and place it in slot four while picking up the pencil there at the same time with one hand.
The analogy breaks down in other ways, for instance normally you make a copy of the item in the temp variable and it exists in two places until you overwrite it in the array.  I think using things like colored blocks here and making copies into variables would be a better way to visualize what is actually happening too...


---

### Answer by username_5 on 2022-01-06. Score: 3

Analogies & metaphors are imperfect, so let's share the expectation that they are just illustrative.

Rules of what is possible (and not) are very important, and we can compare the rules of pencils moved with hands and what computers can do — but we need to define both, in part so the rules for the metaphor can be compared with the those of a computer.

These rules can be reasonably terse and simple to state.

The computer can copy a value from one storage location to another storage location.  (Computers generally copy data rather than moving it.)

Your hand can simulate action of making a copy, and whether the hand itself represent another storage location that can be utilized while doing something else is a matter of definition and explanation of rules.

Since we're talking about information rather than physical items, I might prefer a metaphor of a spreadsheet: swapping two cells in a spreadsheet generally requires another free cell; a pencil and piece of paper with some boxes holding symbols might also work, you need to stipulate whether human memory is outside of the metaphor (i.e. not be used for storage).

Programming languages support constructs & expressions that require additional implicit storage, and language implementations introduced temporaries for these purposes: Python's `a,b=b,a` is such a construct, while any language's general purpose expressions is another: you can't really compute `a*b+c*d` without holding the result of one of the multiplications somewhere (on paper, in your head, in storage...), while doing the other.

> **Comments:**
> **username_3**: "a * b + c * d" : lovely! This insight is at the heart of functional programming : elide useless names. The more technical name for that is *combinatory logic*. (The proper name "Haskell" is the name of its second founder)  
> **username_6**: @Rusi Are there any Functional Machines? Computers that execute your high level idea of the calculation and not some lower level translation? I would like to see such a machine. I would also like to get to work using a map instead of a car, but you can't have everything. (Where would you put it?) Brains really do seem to implement a Neural Network, but computers not so much. Some day perhaps.  
> **username_3**: @Scott: [Lisp machine](https://en.m.wikipedia.org/wiki/Lisp_machine), [Scheme88](https://dspace.mit.edu/handle/1721.1/6042). In fact [Data flow architectures](https://en.m.wikipedia.org/wiki/Dataflow_architecture) were actually made for improving efficiency of von Neumann languages eg [SISAL](https://en.m.wikipedia.org/wiki/SISAL). In fact among theoretical models, lambda calculus is more realiable than Turing machines because the latter has an infinite sub part -- the tape. How the F does an engineer make an infinite object?! Nothing infinite about the lambda calculus. Evolution...... is slow  
> **username_3**: @ScottRowe All that FP does is generalize "a * b + c * d" from numbers to arbitrary memory data structures. Heck even python Java C# etc -- the whole current mainstream gang -- will allow you to handle complex data structures, *manage* ing the memory with gc etc. Only difference is the modern crop [mess up memory semantics](https://cseducators.stackexchange.com/a/6567/8837). FPLs don't. Java, C#, JS are identical modulo syntax and minor details.  
> **username_3**: Of course **IF** you're prepared to go outside the mainstream there are heroic exceptions. Eg Rust which strives for C-level efficiency, memory-footprint. Plus almost haskellish purity. And is even highly portable (Compare to C's autoconf!!!). **IF**... Are you?? (In 1987 [I pioneered](https://dl.acm.org/doi/10.1145/126459.126471) and initiated C in edu. It was as new and uncharted then as it's passe now. Today I would use rust for that space (if I were younger... If...))"  
> **username_6**: @Rusi I learned C in college in 1986, I think. My point was that since real machines can be explained as being something like a simple RISC / registers CPU, maybe education should start by making that model clear? It's nice to have a common reference for understanding new things. After people know the "5 operations" model, we can add to it.  
> **username_3**: @ScottRowe "Can be explained" = "Best explained"??  
> **username_6**: @Rusi The Question that got me started learning about computers when I was 7, was when I wondered "*How could such a thing possibly work?*" If you don't answer that, you have said nothing. Virtus Dormitiva and all.  

---

### Answer by username_7 on 2022-01-05. Score: 3

I do the swap example pretending to have two largish (2-hands-to-lift) boxes side-by-side on a desk (with only enough room for two boxes). After we lift the first we have to look for a place to put it, which feels like creating the temp variable.

But I don't think swap is an especially good example with lists. It's easier to explain swapping just any two variables, x and y. Lists are about using indexes. In Scratch we don't get as much fun with loops, but can do 2 step find-the-index-first stuff like "turn the first 9 into a 6" or "put 12's before and after the first 3".

As far as "computer rules" I like a lighter touch. Swap is one of many clever tricks which happen to work, for now. Motivate the problem with how switching x and y starting with "set x to y" permanently erases x. Then explain how that 3-step dance is actually the standard swap trick. We could have had a built-in swap command but everyone was all "no, everyone knows the trick and it's just one extra step".

Esp. in Scratch you're hoping they like it and move onto a real language. At that point you're going to explain how "Scratch rules" like pre-making variables aren't computer rules at all. But we learned lots of concepts which mostly transfer over with minor changes. In the end: switching values of two variables is a thing; everyone calls it a swap; and it's good to know the trick just-in-case there's no built-in way.

> **Comments:**
> **username_8**: well... to be fair you *do* need to "pre-make" variables in most languages...  
> **username_7**: @username_8 My understanding is the Scratch language has no declarations -- variables are created manually through the GUI. I assume a Scratch user learning a text-based language would be surprised at something like *int x=4*. But I've only seen Scratch a few times, is there a better example?  
> **username_8**: well. while that's true i'd argue that scratch variables are all global, and global variable declarations in other languages are little different from that of scratch - you still "create variables manually". to look at it another way, in scratch, not just variables but *everything* is created "through the GUI", no? (equivalently, scratch has *no (official) textual syntax* at all). but either way the point is, imo "pre-making variables" (and scratch in general) is little different from the so-called "computer rules"  
> **username_8**: tl;dr imo how variables are created in scratch are mostly a side effect of being a graphical (?) language rather than it being fundamentally different from other lanugages  

---

### Answer by username_9 on 2022-01-05. Score: 3

In thinking about this, and especially [the answer of username_12](https://cseducators.stackexchange.com/a/7211/1293), I think that what we really do in the standard three step process *is* a two handed swap without thinking of it. So, it is a failure of the metaphor, actually. So, here is a reframing of the standard process:

Pick up one stick with the left hand (i.e. temp is associated with the left hand). THEN, move the other stick with the right hand (the assignment). THEN put the stick in the left hand into the proper slot.

Or:

Second Item -> Left Hand
Fourth Item -> Second spot // using Right Hand
Left Hand -> Fourth spot

All you need to explain is that (with a single processor) only one thing can be done at a time and so the swap-with-two-hands has to be sequenced in some way rather than happening simultaneously.

This is to avoid issues in the more general case where you want to swap(m,n) where m and n could be the same. Both hands can't pick up the same stick simultaneously if it has to be sequenced. The bright kids will want to complain that you can't pick up a stick from an empty slot, but the small variation isn't too hard to explain if you think of it beforehand. The really astute ones will notice that assignment is copying, so "picking up one stick" in the program doesn't really leave that slot empty, as it does with physical objects.

(ASIDE: there is a way to avoid that physical/virtual disconnect by using a reference (rather than copy) metaphor of variables, but it is likely not where you want to go with youngsters unless you do it ubiquitously. In essence, a variable is a reference to a value not a box that "holds" the value.)

It also avoids discussions of race conditions with multiple processors.

> **Comments:**
> **username_10**: Why does "single processor" keep getting brought up?  How would you sequence operations by 2 cores to get a swap done correctly?  The relevant thing here is a *scalar* CPU, as opposed to modern superscalar CPU cores that literally can execute two independent load instructions in the same clock cycle.  (e.g. AMD since K8, Intel since Sandybridge: https://www.realworldtech.com/sandy-bridge/7/).  Intel since Ice Lake can even execute two store-data uops in the same clock cycle, although commit from store-buffer to L1d cache can only commit two stores in 1 cycle if they're to the same line.  
> **username_10**: As you say, a normal swap is 2 loads then 2 stores (assuming an optimizing compiler for an AoT compiled language for a register machine), whether we name one or both temporaries.  (Or in Python, neither).  For (much) more advanced students of CPU architecture, this might be a good illustration of hazards like write-after-read when finding parallelism between operations: you have to finish picking up an object (loading into regs) before a store of the other object writes that location.  
> **username_11**: @PeterCordes *"Why does "single processor" keep getting brought up?"* Not because using multiple processors or multiple threads would enable a different solution, but because if you are explaining to a student why they have to solve the problem with one hand then the analogy is justified by the fact that their program can "only do one thing at a time".  
> **username_10**: @username_11: Thanks, you're right, I was thinking in terms of finding instruction-level parallelism within instructions for a sequential model of execution, not the need for there to be a sequence in the first place.  I think I'd find it less weird to say the code or program is single-threaded, rather than to say there's only one processor to execute it on.  As you rightly pointed out, the issue here is the execution model of the programming language, not the CPU details.  
> **username_6**: @PeterCordes Because if you ask a centipede how it knows which leg to move next, it gets in a hopeless tangle. (That's just a metaphor, of course.)  

---

### Answer by username_11 on 2022-01-05. Score: 2

Ben's answer is great, but allow me to suggest that you don't need to "respond" to the two-handed approach at all: just say when you're explaining the problem in the first place that they can only use one hand and they can only hold one item at a time.

Students are less likely to need a justification this way, because you haven't changed the rules to forbid a solution after they've come up with it themselves. If a justification is required, the question is not *"why can't I use two hands?"*, but rather *"why is it useful to know how to do this with one hand?"*. And the answer to that is that the solution using one hand can be translated directly into code, whereas the solution with two hands can't be.

> **Comments:**
> **username_12**: Except you perfectly well can translate the solution with two hands into code.  Not picking up both simultaneously, sure - at least on most systems - but one at a time works fine.  
> **username_8**: except you *cannot* just let it slide without explanation - it's *extremely* important to make sure the student is understanding it how you think they are. so you'd at the very least have to explain (and get the student to agree) that they are describing a 4 step process `left = a; right = b; a = right; b = left` rather than a 1 step process `a, b = b, a`. the problem with this is that it runs the same risk of the student asking why the process must be split into those steps.  
> **username_11**: @username_12 By "the solution with two hands" I refer to the one in the OP where a student picks up both items with both hands at the same time. If you interpret hands as variables rather than as the computer's action of moving (or really, copying) a value from one variable to another, then there are other two-handed solutions, but "the" two-handed solution still doesn't work. As for "on some systems", we are talking about whatever language the students are learning, not the capabilities of all programming languages.  
> **username_6**: Maybe someday we will build computers that can do many things at once. Quantum, massively parallel, whatever. Then we will not be able to teach anyone how to program them, but we won't have to.  
> **username_11**: @ScottRowe I don't know what future you're imagining where nobody is a computer programmer, but I want no part of it - I like programming!  
> **username_6**: @username_11 I liked using a darkroom, but Kodak didn't think it was worth producing film, paper and chemicals just for me. So schools don't teach it anymore. Can you sail a boat or ride a horse or repair a Zenith Trans-Oceanic without a schematic? I could, but opportunities are few now. Starting a car with a pencil not likely possible now.  
> **username_6**: I liked your answer by the way, I just wanted to say why the one-hand explanation is necessary. But clearly it won't be for long. Lord help us then!  

---

### Answer by username_3 on 2022-01-09. Score: 2

username_12 pointed out that the assumtions of the question are wrong.  And username_10 elaborated a bit in the comments.
Considering a target VLIW (Very Long Instruction Word)
architecture can help sharpen this argument further.
The basic idea of VLIW is that a Very Long Instruction "horizontally" packs a number of instructions per single VL-instruction, all of which are executed in parallel in one machine cycle. The requirement is that the multiple instructions in a VL-instruction need to access disjoint memory/registers.
In terms of more popular architectures, VLIW is:

Like pipelined superscalar architectures except that the compiler rather than the hardware does parallelization scheduling
Like RISC in that the complexity is shifted to the compiler

So
x = 1
y = 2

can be packed as one VL-instruction
 x = 1 // y = 2

Whereas
x = 1
y = x + 1

can't because x overlaps.
(Using the notation that multiple VL instructions are on multiple lines and "subinstructions" packed into a VL instruction are on one line "//" separated).
With that background we can write swap as
lh = x // rh = y
x = rh // y = lh

Whereas your preferred solution
t = x 
x = y
y = t

is interlocked at each stage and so is hopelessly sequential.
tl;dr
The VLIW oriented swap is 33% faster — 2 vs 3 — than the standard imperative one. When it is naively sequentialized it's 25% slower — 4 vs 3.
Note
The two-hand analogy is right in being more efficient than with one hand. However it's not so right in this that it seems to be able to do it in one single 2-handed swoop. Whereas (for VLIW at least) it's two 2-handed swoops. Our hands have enough muscle intelligence to criss-cross without colliding whereas data-buses dynamically hop-skipping over each other is..  uh... still futuristic sci-fi.
IOW analogies/metaphors may help but don't go all the way.

> **Comments:**
> **username_6**: Premature optimization is... Oh, you already heard that one. Could we venture to teach beginning students something without having to teach them everything? I had a great welding instructor, he could cover one topic or technique each class night. I still think of him as one of the best teachers I had, and still smile when I recall him whistling during class as he thought about going out with his wife afterwards.  
> **username_3**:  @ScottRowe. In upholding the "Premature..." proverb you'll find me more extreme left (or is it right?) than anyone. The point of this post was.... O well... May the blessing of [Fermat](https://en.m.wikipedia.org/wiki/Fermat%27s_Last_Theorem) be on these comment columns...  

---

