Tuesday, January 29, 2008

Programming Languages are Languages too

One of the reasons that people keep inventing new programming languages is that humans are good at using language and computers are not. So was we improve computers and add more complexity, programmers endeavor to make using these new features simpler and less painful. As a result, computer languages are moving in a general direction towards more natural human language. There will likely always be differences, but programming languages and human languages are strikingly similar if you understand some of the widely used syntax.

Here's an example. When you see something like this
z = x * y;
print(z);
it means that you want the computer to "store the value of x times y in the varibale z, then display z on the screen." As you can see, some programming syntax is borrowed from math. This example includes arithmetic and a function. Functions can also be though of as verbs, with variables as the nouns. In object oriented programming, variables can be nouns which are capable of performing actions. If you had a digital carrier pigeon, and you wanted to tell it to carry a letter to your grandmother's house, you might say something like:
myPidgeon.payload = myLetter;
myPideon.flyTo(grandma.house);
In human language, there are always multiple ways to say the same thing, and the same applies in programming. The programmer might just as easily design the program to give grandma the letter like this:
myLetter.setRecipient(grandma);
myPideon.deliver(myLetter);
Now for some fun. What do the following code snippets mean?
  1. if (jack.getWorkPercent() == 100.0 &&
    jack.getPlayPercent() == 0.0) {
    jack.dullBoyFlag = true;
    }
  2. Pie aPie = new Pie();
    Song aSong = new Song(sixpence);
    aSong.sing();
    fill(pocket, rye);
    aPie.add(new Blackbird()[24]);
  3. Mouse mice[3];
    for (i in range(3)) {
    mice[i] = new BlindMouse();
    }
    observe(run(mice));
  4. party = new Party(jack, jill);
    party.setTarget(water);
    party.equip(pail);
    party.ascend(hill);
Post a Comment