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.

made worse, because the symbol held in aVariable (which is the name of the message to send to the<br />

item) is determined by <strong>an</strong><strong>Object</strong> which is <strong>an</strong> object passed into this method. We therefore do not<br />

know:<br />

1. What the contents of aCollection will be<br />

2. Which classes must respond to the message passesTheTest<br />

3. Which inst<strong>an</strong>ces are likely to result in the ifTrue block being evaluated (this is context<br />

dependent).<br />

4. What the message to be sent within the ifTrue block will be (this is content dependent).<br />

5. Which objects must respond to the message we don’t yet know.<br />

It could be argued that this is true, but irrelev<strong>an</strong>t. However, the point is that a number of the above<br />

issues would be resolved by the co mpiler at compile time in other l<strong>an</strong>guages such as Ada or Pascal.<br />

These static compile time checks c<strong>an</strong> identify what type of object will be held in the aCollection, if the<br />

type of object receiving the message will underst<strong><strong>an</strong>d</strong> that message etc. Where as in S malltalk, it is left<br />

up to the developer to ensure that no problems are likely to occur.<br />

As you c<strong>an</strong> see from this very simple (although slightly contrived) example traditional static test<br />

generation techniques, although of use in object oriented l<strong>an</strong>guage s such as <strong>Smalltalk</strong>, do not provide<br />

the whole <strong>an</strong>swer. In the example above, the problems are due to the dynamic typing of <strong>Smalltalk</strong> <strong><strong>an</strong>d</strong><br />

the late binding <strong><strong>an</strong>d</strong> polymorphism of object oriented l<strong>an</strong>guages.<br />

22.2.2 What makes it hard?<br />

Part of the problem for object orien tation is that space of possibilities is so much greater. For example,<br />

not only are we concerned about objects sending messages requesting that other objects run specified<br />

methods correctly, we are also concerned with which class of object will receive the message <strong><strong>an</strong>d</strong> which<br />

class of object sent the message as this will therefore have <strong>an</strong> effect on which method will be used.<br />

These issues are related to the polymorphic nature of object oriented l<strong>an</strong>guages <strong><strong>an</strong>d</strong> the use of<br />

abstraction <strong><strong>an</strong>d</strong> inherit<strong>an</strong>ce as a basic system construction tool.<br />

In addition encapsulation leads to a fundamental problem of observability; as the only way to<br />

observe the state of <strong>an</strong> object is through its operations. This is fine for black box testing, but makes<br />

white box testing extremely diffic ult. This problem is compounded as traditional control flow <strong>an</strong>alysis<br />

techniques are not directly applicable as there is no definite sequential order in which methods will be<br />

invoked. That is, <strong>an</strong>other object may request that a method is executed at a point which c<strong>an</strong>not be<br />

statically determined (see example in introduction). Indeed, as the state of the object may affect what<br />

the method does, it may not even be possible to determine how the method will behave. Of course this<br />

does introduce the issue of “what is correct behavior”, however we will leave the issue of “correctness”<br />

until a later section.<br />

In traditional programming l<strong>an</strong>guages the basic unit of test is usually taken to be procedure or<br />

function. However, it should be seen from this brief discussion t hat, the object oriented equivalent i.e.<br />

the method, c<strong>an</strong>not be taken as the basic unit of test. It is affected by the state of the object, possibly by<br />

the state of the class (if the class has class variables), may interact in unforeseen ways with other<br />

methods <strong><strong>an</strong>d</strong> may rely on methods defined elsewhere in the class’s superclass hierarchy.<br />

Inherit<strong>an</strong>ce also affects testing <strong><strong>an</strong>d</strong> introduces the following question “after a ch<strong>an</strong>ge, to what extent<br />

should the code currently in the system be re -tested?”. In a traditio nal programming l<strong>an</strong>guage it is<br />

usually quite straight forward to identify what parts of the system code are affected by some ch<strong>an</strong>ge. In<br />

<strong>an</strong> object oriented system it is far less obvious.<br />

The implication of the above is that in object oriented programs, suc h as those implemented in<br />

<strong>Smalltalk</strong>, the basic unit of test must be considered to be the class. That is not to say that care is not<br />

given to exercising individual methods within a class, merely that the individual methods should not be<br />

treated in isolation.<br />

22.2.3 Why a lack of emphasis?<br />

Part of the reason for the lack of <strong>Smalltalk</strong> testing literature or tools is probably due to the background<br />

of <strong>Smalltalk</strong>. In the past it was often used as a single (or at most a few) developer’s l<strong>an</strong>guage. It was<br />

often considered to be a personal development environment or a good prototyping tool. However few<br />

183

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

Saved successfully!

Ooh no, something went wrong!