On word smithing

Part of what separates a good program from a mediocre one is the words the programmer selects to express the concepts contained within the code. Consider the following two examples of PostScriptâ„¢ code:

/frobinate  dup /foo get exch /bar get def

/at  dup /x get exch /y get def
Now both can be though of doing the exact same thing. Each takes a dictionary on the top of the stack and returns the values associated with two keys. Both work exactly the same way, the only difference is the choice of names used to convey meaning to other programmers. To the computer it makes no difference if the key is called foo or x. Neither have any particular import to the computer. Similarly, frobinate is as good a word as any, when dealing with meaningless qualities like foo and bar.

The Semantic Gap

This example illustrates how the observer's preconceptions influence the meanings of the words chosen. Structurally, these two statements were identical, performing the same operations with the same types of values in the exact same order. Semantically, however, the second example more clearly expresses the programmer's intentions and the likely usage of these routines. For example, we could define a class of dictionaries that all respond to our procedure at as all dictionaries which posses the keys x and y:
/is  true exch { 2 index exch known and forall } def
/locatable /x /y def
/scalable /width /height def
In this code segment we define a routine is which will test to see if each of the keys in an array passed on the top of the stack exist within the dictionary which is next on the stack. So say we define a dictionary Box which is both locatable and scalable:
/Box << /x 100 /y 200 /width 30 /height 40 >> def
Not only will these routines work for our dictionary Box it will work with any dictionary which has these keys. For example, all locatable elements could be moved:

/to  2 index exch /y exch put /x exch put def

This will allow us to make sensible statements such as:
Box locatable is  400 300 to if
While it may read like Ciceronian Latin, it clearly mimics the semantics of the English sentence:

If the box is locatable, move the box to coordinate (400,300).
Syntactically there are are differences between what we would like to say, and what we can easily say in PostScriptâ„¢, but semantically the gap can be kept incredibly narrow. What makes all the difference is our choice of words, and the manner in which we structure our idioms. These same techniques can be applied to other languages as well, bringing what you wish to say in English into harmony with what the language designers allowed.

A Different View of Classes

In the previous example, I demonstrated what could be considered object oriented style programming in PostScriptâ„¢. Rather than using a traditional concept of class, I used a concept similar to traits as found in Self and Smalltalk. If you are familiar with protocols in Objective-C or interfaces in Java, those concepts are similar as well. This style of classiness is particularly useful for languages that heavily use associative datatypes: dictionaries, hashes, assoc-lists, in order to represent objects. By checking to see if an entity has the correct properties at runtime, and applying logic based on the results of the test, you can effectively manage polymorphism in ways where traditional class hierarchies quickly break down.

Javascript programmers man make particularly strong use of this approach, as all objects in Javascript are Self style key-value slot based objects. Rather than manipulating a long prototype chain, inheritance can be done with a direct slot copy methodology. And since protocol chain inheritance can quickly become opaque, through use of anonymous functions, implementing the is function in Javascript as a property of the Object.prototype object can make all of your objects classy.