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:
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 forall } def
/locatable /x /y def
/scalable /width /height defIn 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 >> defNot 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:
This will allow us to make sensible statements such as:
Box locatable is ifWhile 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.