On Tue, 18 May 2004, Tim Bates wrote:

> > The worse breakage I have seen in a Object Oriented System was in a
> > language called Actor, where for optimization reasons they had forced
> > the internal representation of their graphics type into pairs of two byte
> > integers. This turned a very large, very useful generic 2D geometry
> > library into something totally useless to me. ie. Never gratiutously
> > break polymorphism, even if you personally can't think of another use
> > for this code, somebody else can.
>
> I'm sorry, I don't quite follow you; can you clarify a little? How did
> changing the internal representation break things? How does this relate
> to polymorphism or Duck Typing?

The tighter you constrain the interface, the less reusable it is. In the
example I mentioned, the programmers had ruined the polymorphic behaviour
of the library by internally squashing everything into two byte int's. If
they hadn't done that, polymorphism would have allowed the library to work
on _anything_ that inherited from Number. Including the floating point
types I needed.

Duck typing is an even freer form of polymorphism and in the case of the
example I mentioned, with Duck Typing, the geometry library would work on
anything that responded to +,-,*,/,==,<, including Vectors and Matrices!

I'm hinting at a prime rule of polymorphic / duck typed design. Don't
gratuitously constrain the interface. Reusable means you don't know what
other types may be fed to you in future, and to be reusable you must allow
it.

> > For example, suppose you got a string from a SQL server that was a
> > "date". But since parsing the string is difficult and expensive to do,
> > you just don't. You stuff it into the date= method and lo and behold,
> > it just so turns out that for this execution path you _never_ actually
> > invoke :year, :month, :day, it just travels through your system, and
> > gets converted to a string with .to_s and written back to SQL.
>
> This is rather risky; you're assuming less than what the documentation
> says, and hoping that nobody else assumes any more than you do. That's
> just asking for someone to come along and add something that does use
> one of those methods, and suddenly things come crashing down around you.

True, YAGNI. And when and if it hits you, you also have the option of
catching the exception, performing the conversion and retrying.

> On the other hand, however, if you know what you're doing then this is
> one of the advantages of Duck Typing, and part of the beauty of Ruby -
> if you *know* that those methods will never get called, the language
> never forces you to convert the date into that format if you don't want to.

The longer I program in Ruby, the less I like the many other languages I
have used.



John Carter                             Phone : (64)(3) 358 6639
Tait Electronics                        Fax   : (64)(3) 359 4632
PO Box 1645 Christchurch                Email : john.carter / tait.co.nz
New Zealand

The universe is absolutely plastered with the dashed lines exactly one
space long.