Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Temporary variables, which ar e local to a method, often have a mixture of sem<strong>an</strong>tic <strong><strong>an</strong>d</strong> typed<br />
names. Larger methods often have sem<strong>an</strong>tic local variable names due to the additional complexity they<br />
represent.<br />
Table 24.1: Variable naming conventions<br />
Variable type Convention Variable type Convention<br />
Global variable Capitalized Temporary variables Lower case<br />
Class variable Capitalized Class inst<strong>an</strong>ce variables Lower case<br />
Class names Capitalized Inst<strong>an</strong>ce variables Lower case<br />
Pool variables Capitalized Method parameters Lower case<br />
In addition, if the variable name is made up of more th<strong>an</strong> one word, the words should not be<br />
separated by ‘-’ or ‘_’, but by the capitalization of the first letter of all but the first word. For example:<br />
theDateToday<br />
employeeRecord<br />
objectInList<br />
Whether the first word in the variable is capitalized or not depends on the type of variable being<br />
used. Table 24.1 summarizes the conventions.<br />
24.2.2 The role of variables<br />
Inst<strong>an</strong>ce variables , class variables <strong><strong>an</strong>d</strong> class inst<strong>an</strong>ce variables all play particular roles within a<br />
<strong>Smalltalk</strong> system:<br />
Inst<strong>an</strong>ce variables should only be used for holding local data or for holding references to other objects.<br />
In the latter cas e, these other objects should be involved in some form of collaboration with the<br />
object (otherwise why does the object have a reference to them).<br />
Class variables should only be used as ‘const<strong>an</strong>t’ values which are referenced by all inst<strong>an</strong>ces of a<br />
class. They should never be used as a form of limited global variable. Such a use is frequently <strong>an</strong><br />
indication that a solution has not been designed with the proper amount of care <strong><strong>an</strong>d</strong> attention. A<br />
very nice use of class variable c<strong>an</strong> be found in the database classes of VisualWorks.<br />
Class inst<strong>an</strong>ce variables are often ignored by most <strong>Smalltalk</strong> programmers because they are not sure<br />
what to do with them. This is partly due to the lack of their use in much of the VisualWorks<br />
image <strong><strong>an</strong>d</strong> partly due to the rarity of occasions when their use is justified. A class inst<strong>an</strong>ce<br />
variable, is a local variable for the class. They are only accessible by the class itself <strong><strong>an</strong>d</strong> each<br />
class will hold its own copy of the data.<br />
Class inst<strong>an</strong>ce variables should only be used when either:<br />
• classes need to hold some local data which inst<strong>an</strong>ces should not have access to (otherwise use<br />
a class variable)<br />
• each class in a particular class hierarchy needs to hold its own copy of some data whi ch<br />
should not be shared with other classes.<br />
If you wish to see <strong>an</strong> example of their use look up UILookPolicy in the VisualWorks image.<br />
24.2.3 The role of pool variables<br />
Pool variables are global variables whose scope is limited to those classes which have (explicitly) joined<br />
the pool. They should not be used as a cheap way of sharing information amongst a disparate set of<br />
classes (for example as a me<strong>an</strong>s of providing a global variable without the stigma of a truly global<br />
variable). In general their use is really on ly justifiable within <strong>an</strong> application framework (a set of<br />
cooperating objects of different classes). Such frameworks often require some common shared values.<br />
For example, the text style to use in <strong>an</strong> application, the name of a preference file or the settings obtained<br />
from such a file. Pool variables c<strong>an</strong> then be used to record this information once, but allow it to be used<br />
by all the objects within the framework.<br />
196