|
Navigation: Appendix B: Dolphin Pattern Book > Class Patterns > Instance Variable Name |
![]() ![]()
|
Context
You are creating a New Class and adding instance variables to the definition. A good instance variable name should result in readable code, not only in methods of the receiver, but also in senders of the corresponding Accessor Methods.
Also Known As
Role Suggesting Instance Variable Name
Solution
Instance variable names should describe, using natural language, the attribute which they represent. The name does not, in general, need to contain expected type information. Documentation about an instance variable (including its type and purpose) should be located in the associated Accessor Method and the class comment.
An instance variable name should use Word Capitalization where necessary and must begin with a lowercase letter. Underscores are allowed in variable names but are not conventionally used by most Smalltalkers.
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 a Class Name, the name should not be so generic that it is meaningless.
Consequences
An Accessor Method will often have the same name as the instance variable it accesses. This may therefore affect your choice of variable name.
Examples
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