29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

24. <strong>Smalltalk</strong> Style Guidelines<br />

24.1 <strong>Introduction</strong><br />

Good programming style in <strong>an</strong>y l<strong>an</strong>guage helps promote the readability, underst<strong><strong>an</strong>d</strong>ability <strong><strong>an</strong>d</strong> clarity of<br />

your code. In m<strong>an</strong>y l<strong>an</strong>guages there are established st<strong><strong>an</strong>d</strong>ards to which m<strong>an</strong>y people adhere (sometimes<br />

without realizing it). For example, the way in which a C or Pascal program is indented is such a<br />

st<strong><strong>an</strong>d</strong>ard. However, style guidelines which have evolved for these proc edural l<strong>an</strong>guages do not cover<br />

m<strong>an</strong>y of the issues which are import<strong>an</strong>t in <strong>Smalltalk</strong>. These are issues relating to classes, inst<strong>an</strong>ces <strong><strong>an</strong>d</strong><br />

methods. As non-object oriented l<strong>an</strong>guages do not have these concepts, they do not have st<strong><strong>an</strong>d</strong>ards for<br />

dealing with them.<br />

Of course you should not forget all the pearls of wisdom <strong><strong>an</strong>d</strong> programming practices that you have<br />

learnt using other l<strong>an</strong>guages. Just as in <strong>an</strong>y other l<strong>an</strong>guage there are a number of acknowledged bad<br />

practices (which are not specific to <strong>Smalltalk</strong>), for example, the use of global variables! In this chapter<br />

(as in the whole of the book) we will assume therefore that you do not need to have the basic concepts<br />

explained, instead we will try to concentrate on those style issues which are specific to <strong>Smalltalk</strong>.<br />

The re mainder of the chapter is structured in the following m<strong>an</strong>ner: Section two considers style<br />

issues associated with variables including temporary variables, inst<strong>an</strong>ce variables as well as class, class<br />

inst<strong>an</strong>ce <strong><strong>an</strong>d</strong> pool variables. Section three provides some gu id<strong>an</strong>ce on the use of classes within<br />

<strong>Smalltalk</strong>. Section four considers methods, their naming, use <strong><strong>an</strong>d</strong> code placement within methods.<br />

Section five discusses how to name message protocols for both inst<strong>an</strong>ce <strong><strong>an</strong>d</strong> class methods.<br />

24.2 Variables<br />

24.2.1 Naming variables<br />

In <strong>Smalltalk</strong> variable names such as t1, i, j, or temp should rarely be used. Instead variable names<br />

should either be descriptive (known as sem<strong>an</strong>tic variables) or should indicate the type of object which<br />

the variable will hold (known as typed variables). Which app roach should be chosen depends on both<br />

personal style <strong><strong>an</strong>d</strong> the situation in which the variables are being used. Inst<strong>an</strong>ce <strong><strong>an</strong>d</strong> class variables, for<br />

example, tend to have sem<strong>an</strong>tic-oriented names, while parameters to methods often have typed names.<br />

The sem<strong>an</strong>tic approach has the adv<strong>an</strong>tage that less needs to be assumed about what the variable will<br />

be used for. Since subclasses c<strong>an</strong> inherit inst<strong>an</strong>ce <strong><strong>an</strong>d</strong> class variables, the point at which they are defined<br />

<strong><strong>an</strong>d</strong> the point at which they are used may be very remote. Thu s <strong>an</strong>y contextual me<strong>an</strong>ing <strong><strong>an</strong>d</strong><br />

commentary, provided with their definition, will have been lost. Examples, of sem<strong>an</strong>tic variable names<br />

include:<br />

score<br />

currentWorkingMemory<br />

TotalPopulationSize<br />

The typed approach is often adopted for parameter names as it is ea sier to see what type of object is<br />

required as <strong>an</strong> argument to a method. For example:<br />

add: <strong>an</strong><strong>Object</strong> ifAbsent: aBlock<br />

at: <strong>an</strong>Index put: <strong>an</strong><strong>Object</strong><br />

Although some methods mix the use of the two, for example: at: key put: <strong>an</strong><strong>Object</strong> .<br />

195

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!