next up previous contents
Next: 4.3 Instance-specific Dispatch Up: 4 Style Guide Previous: 4.1 Environment organization   Contents

Subsections

4.2 Naming Methods

One of the primary benefits and peculiarities of Smalltalk's style of method syntax is that it provides an opportunity to name one's protocols using something resembling a phrase. Usually, it is recommended to re-use protocols whenever describing similar behaviors, as an aid to the user's memory in matching functionality to a name to call. Here are some general practices that Slate uses which have been common in Smalltalk practice for years.

4.2.1 Attributes

Attributes are perhaps the simplest to name of all, in that they are generally nouns or noun phrases of some sort, whether used as direct slots or methods which calculate a property dynamically.

4.2.2 Queries

Methods which test for some fact or property about a single object are generally given a ``whether''-style phrase. For example, myCar isRed answers whether one's car is red. Slate offers an additional idiom over this particular style, in that myCar color is: Red is also possible, since is: looks at both the subject and the object of the query.

4.2.3 Creating

While the method clone is the core of building new objects in Slate, rather than instantiating a class, there is still the need to provide an idiom for delivering optional attributes to one's new objects. Generally, these methods should start with new- as a prefix to help the reader and code user to know that the original object will not be modified, and that the result is a new, separate individual. These methods are usually keyword methods, with each of the keywords describing each option, whether literally naming an attribute, or simulating a grammatical phrase using prepositions.

4.2.4 Performing Actions

The most interesting protocols are akin to commands, where one addresses the objects in question with a phrase that suggests performing some action. This should usually have one key verb for each major component of the action (there is usually just one action per method, but select:thenCollect:, for example, performs two), and prepositions or conjunctions to relate the verbs and nouns.

4.2.5 Binary Operators

These are perhaps the most controversial of any programming language's protocols. In the Smalltalk family of syntax, there are no precedence orderings between operators of different names, so the issues with those do not arise. However, it is very tempting for the library author to re-use mathematical symbols for her own domain, to allow her users to have a convenient abbreviation for common operations. While this benefits the writer of code which uses her library, there are domains and situations that punish the reader of the code that results.

For example, mathematical addition and multiplication symbols, ``+'' and ``*'', are generally associative and commutative. That is, repeated calls to these should be able to re-order their arguments arbitrarily and acheive the same result. For example, $3+4+5=4+3+5=5+4+3$. However, string concatenation (as an example) is not commutative; we cannot re-order the arguments and expect the same result, i.e. "gold"+"fish"="goldfish" , whereas "fish"+"gold"="fishgold" . Because concatenation is associative, however, we can re-use the punctuation style of the semi-colon ``;'' and achieve intuitive results. This general style of reasoning should be applied wherever this type of operator name re-use could arise.


next up previous contents
Next: 4.3 Instance-specific Dispatch Up: 4 Style Guide Previous: 4.1 Environment organization   Contents
The Slate Project 2003-07-29