[comp.lang.eiffel] reusability of code in object oriented design

day@grand.UUCP (Dave Yost) (12/22/88)

From: "Andrew K. Wright" <akwright@watmath.waterloo.edu>
Date: Thu, 24 Nov 88 13:48:48 EST

In most object oriented languages, when a new class is defined,
its operations are implicitly reusable by a subclass.  That is,
without the user going to any effort, any code (operations) written
are reusable.  In fact, object oriented languages in which the user
can indicate that code, or a class, is NOT reusable are few.

I have the feeling that in order to create a class with a fair
chance of being reused, the user must be intending to create
a reusable class.  In C++, this is, in some ways, required by the
language: the "virtual" keyword must be used by the class designer
if a derived class is to redefine parent class operations and behave
properly.  This is not true of Eiffel (each operation is implicitly
virtual).

In order to gather some feedback about what other object-oriented
designers have encountered, I will challenge the notion that
classes can be designed without the intent of reuse, and effectively
reused later.  I contend that only through freak accidents are
classes reused, unless they were intended for reuse.  Thus
C++ classes containing "virtual", and Eiffel classes containing
"deferred" are definitely not eligible, because they were clearly
intended for reuse.  Classes which had to be modified to achieve reuse
do not count either.  What percentage of your classes which you
did not originally intend to reuse do you actually derive from?

Andrew K. Wright      akwright@watmath.waterloo.edu
CS Dept., University of Waterloo, Ont., Canada.

bertrand@eiffel.UUCP (Bertrand Meyer) (03/23/89)

This is in reference to an old message, <473@grand.UUCP> from
Andrew K. Wright <akwright@watmath.waterloo.edu>, dated
24 Nov 88, actually sent to the mailing list before the newsgroup
existed. The questions raised by that message were interesting;
however I don't remember seeing a response so far.

Because the message is old it is appropriate to quote it at length:

 > (...)
 > I have the feeling that in order to create a class with a fair
 > chance of being reused, the user must be intending to create
 > a reusable class.  In C++, this is, in some ways, required by the
 > language: the "virtual" keyword must be used by the class designer
 > if a derived class is to redefine parent class operations and behave
 > properly.  This is not true of Eiffel (each operation is implicitly
 > virtual).

 > In order to gather some feedback about what other object-oriented
 > designers have encountered, I will challenge the notion that
 > classes can be designed without the intent of reuse, and effectively
 > reused later.  I contend that only through freak accidents are
 > classes reused, unless they were intended for reuse.  Thus
 > C++ classes containing "virtual", and Eiffel classes containing
 > "deferred" are definitely not eligible, because they were clearly
 > intended for reuse.  Classes which had to be modified to achieve reuse
 > do not count either.  What percentage of your classes which you
 > did not originally intend to reuse do you actually derive from?

    I would be hard pressed to give a precise figure. 
I think Prof. Wright's basic contention is correct: you don't get
reusability without designing for it - unless you are lucky and a
class turns out to be reusable in a context for which it was not
initially planned. But what our experience shows is that as you go along
you are lucky more and more often: in other words, in more and more
frequent cases you find yourself applying classes to unforeseen uses.

    This is seldom just luck, of course, but rather experience
gained from previous designs, particularly lessons learned from cases
in which an attempt was made to reuse a class and the class turned out
to be too specific. Little by little, you learn how to avoid such a
situation, and how to design classes that will stand a better chance of
being applicable to new uses.  So in our experience the picture is
less dark than what Prof.  Wright's message would seem to imply.

    In fact one could almost contend that no reuse occurs
other than through what he calls ``freak accidents'':
if you design a class ``with the intent of reuse'', it means you have
a certain set of applications in mind. But using the class for
these applications is not reuse: it is just use! Actual reuse only occurs
when you discover that the class is applicable to a case that was
*not* explicitly envisioned in the original design - a ``freak accident'',
or what I have called luck above.

    Also, reusability is not an all-or-nothing property but a relative
concept. The only fully reusable devices are Turing machines and their
equivalents. Everything else is specific. The reusability of a class
is the number of potential applications to which it can be applied;
the bigger this number, the more reusable the class is.
Therefore if a precise answer to Prof. Wright's final question was
required I think the question should first be broadened. In practice
it is not just important to ask ``Were you able to reuse class C
exactly as it stood'' but also, in cases when the answer to this
initial question is no, ``How extensive a set of changes did you have
to perform?''. We don't want to dismiss a class with 50 routines as
non-reusable just because we had to adapt or add one routine for some
new application.

-- Bertrand Meyer
bertrand@eiffel.com