|The Trick Of The Mind - On Being Variable|
|Written by Mike James|
|Monday, 09 May 2022|
Page 1 of 3
This chapter of my new book on the nature of programming is aimed at both programmers and non-programmers. In this extract we look at the idea of a variable something that confuses the beginner and expert alike.
The Trick Of The Mind - Programming & ComputationalThought
Buy Now From Amazon
So far the emphasis has been on constructing lists of instructions and of the process of working through them. There is one more ingredient in the melting pot of computational thought that is often dismissed as minor. or at worst an irritation which should be ignored or removed as soon as possible – the idea of a variable. This is a key idea in programming and it is perhaps the one factor that demonstrates beyond all doubt that programming is something different from mathematics. Mathematicians have had something that they call a variable for centuries and yet the defining factor of said variables is that they are not particularly variable. For a true variable you need to move to programming and the strange controversy over its very existence.
The Invention of the Variable
It is often the case that things that eventually seem obvious or slight don’t seem to need inventing. This is especially true when that invention takes the form of small steps over a long time. So it is with the idea of a variable in programming.
Back in the very early days of programming there were no variables, only storage locations and addresses. You can think of this as storage boxes with labels, but be warned this isn’t a very good analogy because computer memory delivers its content when you refer to its address – storage boxes don’t. Computers had a very simple type of memory and the programmer could use this memory to store temporary results.
At first we thought of what was stored in memory as just numeric values, but very quickly it became apparent that you could store anything at all simply by using a coding scheme. For example, you can read any number as text simply by using a code A=1, B=2 and so on. So without any loss of generality we can consider a memory location as something that can store a number and retrieve it using a label.
At first the labels were numeric addresses and programmers wrote instructions like:
store 42 in memory location 123
retrieve the value stored in location 123
The numeric addresses worked fine for the computer, but the programmers had a hard time remembering what was stored where. To make this easier the idea of the variable was born. You could give a memory location a name which you could use in place of the address. This allowed programmers to write things like:
store 42 in Total
retrieve the value stored in Total
This is just a cover-up for the use of an address. What happens is that when you want to store something in Total some bookkeeping allocates a memory location to it and the address is associated with the label Total. For example, Total might be an alias for the address 123. When you next write something that retrieves the value Total is replaced by the address associated with it.
You can see that we are still working in terms of addresses and the change is purely cosmetic. However, you cannot imagine what a huge difference this change made to the understandability of programs. Suddenly you could see what sort of thing was stored and stop worrying about where.
Of course, the program as written had to be converted back to using numerical addresses before it was presented to the computer, but this was just another transformation of the human-written program to what the computer wanted – another step in compilation, i.e. another job for the compiler in addition to arithmetic expressions.
|Last Updated ( Monday, 09 May 2022 )|