P01: programming concepts

What’s hard about programming?

If you have never written a computer program before, you will find computer programming to be difficult. Programming a computer requires:

  1. figuring out what computation solves the problem you want to solve

  2. specifying what steps to follow to perform that computation, and

  3. explaining those steps to a computer.

When you write your first simple programs, 1 will be solved for you, 2 will be easier, and 3 will seem quite hard.
As you get better at programming, and the programs you write become more complicated, 3 will become fairly easy, while 2 will prove to be quite difficult. Eventually, most of the difficulty will end up in 1.

Learning to program

  • Programming means giving a computer instructions about what steps to follow to perform some calculation.

  • Computers have no build in knowledge of the world and they are very ‘literal’. They do exactly what we tell them; no more, no less. No matter how silly or simple the instructions sound.

  • The most common cause of a computer doing something wrong is us telling it to do the wrong thing.

  • Learning to program means learning to think like a computer (i.e., that you know nothing about the state of the world or the nature of the problem to be solved except what you are explcitly told by a programmer using code). Programming about figuring out what calculation to do to solve the problem you are interested in, and then explaining those steps to the idiot computer.

Example:

You want to instruct me to send a new post to the class with the message “hi” using the campuswire website. The trick is that (1) you have to write all the instructions out ahead of time, and (2) I only understand a limited set of commands:

  • move mouse {left, right, up, down} (distance in cm, on my screen)

  • click mouse

  • press (keyboard button)

Some observations about what makes this hard:

  • cannot observe intermediate state.

  • my representation differs from yours, and you have to give me instructions in my representation.

  • tedious level of precision required because I don’t understand more sophisticated commands.

Computer programming presents the same challenges: we need to write out very detailed instructions, using a very limited, literal, set of commands. We need to write the instructions out in advance. The computer’s representation of the world differs from our own. How the state changes in response to instructions may differ from your expectations.

Another example - the directions on your shampoo bottle probably say something like “Apply, Rise, Repeat”. If you told a computer to that, the world would rapidly run out of shampoo…