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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

24.4 Methods<br />

24.4.1 Naming methods<br />

Method names should always start with a lower case letter. If the method name is made up of more th<strong>an</strong><br />

one element then each eleme nt following the first one, should start with a capital letter. This st<strong><strong>an</strong>d</strong>ard<br />

follows that used for variable names. For example:<br />

account deposit: 100<br />

account printStatement.<br />

If the method is a keyword method containing more th<strong>an</strong> one parameter, then each of the key words<br />

should start with a lower case letter, with each subsequent word starting with a capital letter. For<br />

example:<br />

Dialog request: 'Name' initialAnswer: 'John'<br />

onC<strong>an</strong>cel: [Tr<strong>an</strong>script show: 'Error'].<br />

The naming of methods is extremely import<strong>an</strong>t. An appropriate method name will not only make the<br />

code easier to read it will also aid in reuse. Therefore method names should be selected to illustrate the<br />

method’s purpose. In addition, it is common to try to select a name which makes it possible to read <strong>an</strong><br />

expression containing the method name in a similar m<strong>an</strong>ner to reading a sentence . For example:<br />

statement deposit: 100 on: Date today.<br />

This is actually helped by the ability to spread the arguments amongst the method name.<br />

Methods which return true <strong><strong>an</strong>d</strong> false as the result of some test follow a common format throughout<br />

the <strong>Smalltalk</strong> system. These methods use a verb such as is or has concatenated with whatever is being<br />

tested, for example :<br />

isString<br />

isActive<br />

hasFood<br />

Notice that in the first case the method is testing to see if the receiver is <strong>an</strong> object of class String.<br />

It therefore uses the name of the class in the method name. In the next two cases some aspect of the<br />

receiver is being tested. For example, the second method may test to see if a process is active or not. In<br />

this case, the value being tested for is used as part of the method name.<br />

24.4.2 General comments about methods<br />

In general it is better to percolate a method up as high as possible in the inherit<strong>an</strong>ce hierarchy as long as<br />

it makes sens e. The higher the method is, the more visibility it has to classes in other br<strong>an</strong>ches of the<br />

hierarchy, <strong><strong>an</strong>d</strong> the more method level reuse you c<strong>an</strong> achieve. You should also consider:<br />

The role of a method. Think carefully about the purpose <strong><strong>an</strong>d</strong> placement of methods within a class. Just<br />

as a class should have a specific purpose, at a lower level a method should also have a single<br />

purpose. If a method is used to perform more th<strong>an</strong> one function, then it should be divided into<br />

separate methods. In general terms methods should be no longer th<strong>an</strong> one page of A4. Small but<br />

beautiful methods are desirable.<br />

Code placement within methods. Deciding how to break up the desired functionality into procedural<br />

elements c<strong>an</strong> be difficult in procedural programming l<strong>an</strong>guages. In <strong>Smalltalk</strong> it is made more<br />

difficult by considerations of object encapsulation <strong><strong>an</strong>d</strong> reuse. However, there are a number of<br />

questions which you c<strong>an</strong> bear in mind when determining whether your code is correctly placed<br />

within the methods you have defined:<br />

• If a method does not refer to <strong>an</strong>y aspect of the object (e.g. super, self, or inst<strong>an</strong>ce variables<br />

etc.) what is it doing? Should the method be there?<br />

200

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

Saved successfully!

Ooh no, something went wrong!