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.

• Correct it This allows the user to correct the fault.<br />

5.5.2 Types of run time error<br />

When encountering a new l<strong>an</strong>guage for the first time, it is quite common to get extremely frustrated<br />

while you attempt to determine why a piece of code is causing a run time error. This often turns out to<br />

be caused by a trivial fault which would have been dealt with immediately if you had been working in a<br />

l<strong>an</strong>guage with which you were familiar. This sort of problem afflicts us all <strong><strong>an</strong>d</strong> is the source of a great<br />

deal of resist<strong>an</strong>ce to ch<strong>an</strong>ge. However, the problems c<strong>an</strong> be eased somewhat if you are awa re of the<br />

types of root causes for different failures. The following list attempts to ease this sort of problem. It<br />

presents the types of run time error (other th<strong>an</strong> incorrect message sends) which c<strong>an</strong> occur:<br />

• Sending a message to <strong>an</strong> object which does not respond to that message.<br />

• Trying to create <strong>an</strong> inst<strong>an</strong>ce of Character or Boole<strong>an</strong>.<br />

• Trying to create inst<strong>an</strong>ces with inappropriate inst<strong>an</strong>ce creation messages.<br />

• Evaluating a block (inst<strong>an</strong>ce of BlockContext) with the wrong number of arguments.<br />

• Numeric errors, such as divide by zero, or square root of a negative number.<br />

• Lots of collection errors, such as trying to remove <strong>an</strong> element not in a collection, or trying to use<br />

the add: message on a collection class which does not support add: for example Array etc.<br />

• Sending a message to <strong>an</strong> object where the corresponding method should have been defined in a<br />

subclass. This c<strong>an</strong> be identified by finding the following statement in a method:<br />

self subclassResponsibility<br />

• Control messages to objects which are not Boole<strong>an</strong>s (ifTrue) or Blocks such as []<br />

(whileTrue:).<br />

5.6 Some <strong>Smalltalk</strong><br />

Let’s look at a little <strong>Smalltalk</strong>, just to get you going. You have already seen (<strong><strong>an</strong>d</strong> possibly written) some<br />

<strong>Smalltalk</strong> when you typed in the Smallta lk version of the “Hello World” program in the last chapter.<br />

Now let’s add two numbers together. First we shall do it in a non -object oriented l<strong>an</strong>guage such as<br />

Pascal. For example, we might write:<br />

int a, b, c;<br />

This says som ething like, “create three variables to hold<br />

a := 1;<br />

b := 2;<br />

integer values (call them a, b <strong><strong>an</strong>d</strong> c). Store the value 1 into<br />

c := a + b;<br />

the variable a <strong><strong>an</strong>d</strong> 2 into variable b. Add the two numbers<br />

together <strong><strong>an</strong>d</strong> save them into the third variable c”.<br />

Now let’s look at how we could write the same thing in <strong>Smalltalk</strong>.<br />

| a b c |<br />

As you c<strong>an</strong> see this looks basically the same (apart from the<br />

a := 1.<br />

b := 2.<br />

use of a full stop (also known as a period) instead of a semi -<br />

c := a + b.<br />

colon). We also apparently forget to declare the types of the<br />

variables a, b <strong><strong>an</strong>d</strong> c (<strong><strong>an</strong>d</strong> put some bars around them).<br />

However, although the affect is the same, <strong><strong>an</strong>d</strong> the look similar, the me<strong>an</strong>ing is<br />

different. In <strong>Smalltalk</strong>, this actually says:<br />

dramatically<br />

“Define three temporary variables a, b <strong><strong>an</strong>d</strong> c (we don’t care what they will hold). Assign the<br />

object 1 to variable a. Assign the object 2 to the variable b. Take the object in a, which has the<br />

value 1, <strong><strong>an</strong>d</strong> send it the message “+”, which includes the argument b, which in turn has the<br />

value 2. <strong>Object</strong> a, receives the message <strong><strong>an</strong>d</strong> performs the action requested. This action is to add<br />

the value of the argument to itself. Create a new object <strong><strong>an</strong>d</strong> return this result as the result of<br />

evaluating the message. Then save this object into the variable c.”<br />

56

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

Saved successfully!

Ooh no, something went wrong!