Pattern: Instance Variable Name


Context

A good instance variable name will result in readable code, not only in methods of the receiver, but also in senders of the corresponding Accessor Methods.

Solution

Instance variable names should describe, using natural language, the attribute which they represent. The name does not, in general, need to contain type information. Documentation about the instance variables including their type and purpose, should be located in their Accessor Methods and the class comment.

The name should begin with a lowercase letter, and if a compound word is used, then subsequent words should begin with an uppercase letter. Underscores are not conventionally used to separate words.

If an instance variable represents a collection of objects, don't append 'List' or 'Collection' to its name. Instead, make the name plural.

As with Class Names, the name should not be so generic that it is meaningless.

Consequences

Accessor Methods will often have the same name as the instance variables they access. This may affect your choice of name.

Known Uses

The Rectangle class has two instance variables origin and corner. These are both instances of Point, but it is not necessary to include this information in their names.

Object subclass: #Rectangle
        instanceVariableNames: 'origin corner '
        classVariableNames: ''
        poolDictionaries: ''

Related Patterns