What a programmer does for a living
What a programmer does for a living is best illustrated by writing a program yourself.
So, today, you'll write your first program!
"But I don't know how to!"
Don't worry, you'll manage, it is not all that hard. The program is going to be in a language called "English".
If you're reading this, chances are that you already know English, so that will make it a bit easier than if you had to learn a new language for this. It saves us some time and in the end it will not make the program substantially different from what it would have been otherwise.
The program will be for a computer called 'the human being', and the goal is to get the program to produce a cup of coffee to be placed at a small table in the doorway between the kitchen and the hallway.
For the sake of simplicity we'll assume that all the required ingredients are present on the other side of the table, and that there is a person in the kitchen that understands English but hasn't a clue on how to make coffee.
When you write a program what you are essentially doing is to take a problem that is 'large', and you break it up in to small steps. Some of these steps can be broken down further, and we can give these steps their own names. In English we'd call such a series of 'sub steps' a procedure, so lets call them procedures here. We're making coffee in the old fashioned way, because this kitchen does not have any modern gear such as a coffee maker. For your own kitchen, you may make adjustments to the program as required. The best test setup is a person that you hand a set of written instructions on some pieces of paper, and you then watch the proceedings from the hallway.
We can tell the person that will make the coffee that by convention the program starts on a page labeled main, if they can read they'll be able to find it.
The basic steps involved in making coffee can be summed up like this:
define main:
prepare ingredients
boil water
filtration
presentation
cleanup
end
(order fixed, thanks Klaus-Werner)
Which we write down on the the first piece of paper.
It would make some sense for each of those steps of the 'main' page to be a procedure on a page with the name of the procedure, the above five steps (+ the end) would produce a cup of coffee on the table if they were somehow magically defined. Because they're not we will need to supply the definitions ourselves. Let's do so for one of them, preferably one that we find obvious or trivial, that gets one of the procedures out of the way. We don't need to 'define' the procedures in the order in which they'll be executed, after all execution comes after we're done writing the program, and the person reading the pages has no idea in what order we wrote them, and so it doesn't matter.
The final step, 'presentation' is the one that is easiest to do, and it looks like this:
define presentation:
pour coffee
pick up cup
place cup on hall-way table
There, that's done. So at least one of the steps is now defined. Or is it? What if the person does not know how to pour coffee? Well, we can easily take care of that:
define pour coffee:
pick up coffee pot
aim spout over cup
tilt spout towards cup
as long as coffee is pouring and fill level of cup is not reached:
wait for a little while
tilt spout away from cup
place coffee pot on kitchen sink
That should do it! Next up, the preparation phase:
define preparation:
find coffee and place on kitchen sink
find water kettle and place on kitchen sink
find coffeepot and place on kitchen sink
find coffee cup and place on kitchen sink
find filterbags and place on kitchen sink
find coffee filter and place on kitchen sink
place a filterbag in the coffee filter
put 6 small spoons of coffee in the filterbag
place the coffee filter on top of the coffee pot
That's all the preparation steps that need to be done. For your kitchen you may have to adapt a few of them.
Boiling water is the next step to be defined, how hard could it be? Another piece of paper:
define boil water:
pick up kettle
open the lid
hold kettle under the cold water tap
open the tap
as long as kettle is not full enough:
wait for a little while
close the tap
place the kettle on the stove
as long as there is no steam coming out of the kettlespout:
wait for a little while
that's a pretty complicated procedure, let's see if it does what it should do by following our own instructions. Pick up the kettle, open the lid, hold the kettle under the cold water tap, open the tap, wait until it is full enough, close the tap, place the kettle on the stove, and wait until steam comes out of the kettlespout.
2 days later, we realize there is something wrong with the procedure for boiling water. It contains a fault, a programmer would say it is buggy. Can you spot the mistake?
Right, we forgot to light the stove. As the next to last step we add 'light the stove', the procedure now looks like this:
define boil water:
pick up kettle
open the lid
hold kettle under the cold water tap
open the tap
as long as kettle is not full enough:
wait for a little while
close the tap
place the kettle on the stove
light the stove
as long as there is no steam coming out of the kettlespout:
wait for a little while
We try it again, and this time we're rewarded with a kettle full of boiling water at the end of the procedure.
Next up is filtration:
define: filtration
as long as the coffee pot isn't full
pick up the kettle
hold the kettle with the spout over the coffee filter
tilt kettle until boiling water streams out
as long as not all the coffee is below the water level
wait for a while
tilt kettle back
place kettle back on the stove
There is a subtle bug in that procedure as well, it's not a disaster but it will look funny when you follow the instructions perfectly. Can you spot the mistake?
And, finally, the last stop, cleaning up:
define cleanup:
switch off the stove
throw the used coffee filter in to the garbage bag
rinse the filter
put the filter back where you found it
pour out the remaining water from the kettle in to the sink
place the kettle back where you found it
pour out the remaining coffee in to the sink (*)
rinse the coffee pot
put the coffee pot back where you found it
clean the spoon
put the spoon back where you found it
And with that the coffee making program is complete. That wasn't so hard now, was it?
All the components of what a programmer does for a living are represented here. We think about a problem to be solved, we break it down in to steps that we understand, we define those steps (or their sub-steps, for harder problems), we test them to see if they're working as expected. If they don't work as expected we analyze the problem, we fix it and we test again. Finally, when we're done we deliver our 'product', a series of precise instructions that enable someone (or something!) that understands the language that we wrote in something they do not need to understand but which give the impression that they do understand, and which for all intents and purposes of everyday life is the same thing.
Programming is not 'hard', it is just very tedious to have to specify each and every little thing and to expect the party that will execute the program to be very literal minded, quite capable of doing exactly what we wrote, but not what we meant.
I would encourage everybody (even the programmers!) to play the game and actually do this, take some task in the household and write a little program for it, hand it to your s.o. and see what happens during the 'execution' phase, it's good fun! If this makes you curious about 'real' programming (no more real than this, just in a computer language instead of English) have a look at hackety-hack.
No comments:
Post a Comment