|The Essence Of Programming|
|Written by Mike James|
|Monday, 29 October 2012|
Page 1 of 3
We often state that program is just a list of instructions, but this does not do justice to the complexity of this apparently simple idea. A the idea of a program exists outside of any particular programming language, but we only get to see and experience it when it has been expressed. This makes it hard to separate the fundamental ideas from language features.
You might think that programming was invented along with the computer but no – it goes back much further and in fact doesn't depend upon the idea of the digital computer at all.
After all a program is just a list of instructions and this idea is as old as writing itself.
Who knows perhaps written communication was invented just so that we could program and communicate programs but whatever the truth programming goes back well before Babbage as an idea.
Let’s take a look at the basic idea of a program without any preconceptions.
On Being Sequential
Given that a program is merely a list of instructions you might think that all the fuss we make about it is a little unjustified. However this is to underrate the complexity of this apparently simple idea.
Lists of instructions do start out being simple.
All you have to do is write down what is to be done at each step.
For example, the instructions that tell you how to build a model aeroplane are a program. You start at step one, obey it and then move on to step two. When step two is complete you move on to step three and so on.
The language that the program is written in is usually a little more precise than standard English but it is nothing special – in most cases it could usually do with being more precise but that’s a problem of another sort.
The instructions are the program, the model builder is the computer and you could even think of the actual model itself as the data that the program manipulates.
A human computer
Before you deride such a simple idea as being well too simple you need to realize that there are people who can't follow lists of instructions - they just don't get the sequential idea. They are baffled by flat pack furniture assembly sheets and don't see what is to be done. A natural programmer takes the instructions and follows them one after another realizing that its the only route to completing the task.
A programmer accepts the law of sequential instructions and does not allow for anything outside of the program.
Surely great programs like Windows and Linux cannot be just a list of instructions of the sort that you find in a model aeroplane kit?!
Sequential instructions get you so far but not all the way. There are some important additional ideas required to get to the full concept of a program.
Programming starts to get more complicated when two key ideas are added to the basic list of instructions – conditionals and loops.
It is arguable that both of these are the product of an even more basic development of the “list of instructions” – the ability to self reference. When a list of instructions includes instructions that refer to the list itself something magical happens and both conditionals and loops are just examples of self reference - but let's keep things simple.
A conditional is an instruction like:
if you want to hang the model from the ceiling stick part Z to the body.
Do you want to hang it from the ceiling?
All very simple and you can see that in general this sort of conditional is something like:
Not only is this reasonably understandable natural language this is also very close to what you will find in nearly all programming languages.
For example, in Visual Basic you would write:
In Python not you would write the same thing only rather more tersely as:
The point is that all programming languages have this sort of conditional and only the minor details vary.
The next question is how complicated can a conditional be?
Do we need to invent all sorts of different types of conditional instruction to build programs from?
The answer is no!
It turns out that this simple IF..THEN is all you need to write any program that can be thought up.
It is a sort of atomic building block from which all other types of conditional can be constructed.
For example, a slightly more complicated conditional is:
In this case we either do instruction A or instruction B depending on the condition being true or false.
if you want to hang the model from the ceiling stick part Z to the body otherwise stick part Z to the wing.
You can see this is just:
and you should also be able to see that this can be written as:
Thus an IF..THEN..ELSE can be converted into two IF..THENs. Notice that this conversion is actually trickier than you might expect and in practice you have to take great care not to say something you didn’t mean to say.
As “saying something you didn’t mean to say” in a program is otherwise known as a bug, most programming languages do provide the IF..THEN..ELSE form of the conditional and a few other forms that also commonly used. But do remember that these are needed in practice but not in theory.
In theory the only conditional you need is the IF..THEN form. You can see why this is so if you think for a moment of a conditional that makes a choice between doing different things and how this could be achieved by using multiple IF..THEN instructions.
The IF..THEN provides a switch into an optional set of instructions. Using multiple switched you can select between as many different optional lists as you care to work with.
|Last Updated ( Monday, 29 October 2012 )|