Re: Some thoughts about OO programming

From: MSCHAEF.COM (mschaef_at_io.com)
Date: 10/07/04


Date: Thu, 07 Oct 2004 08:46:14 -0500

In article <90a4dba897d8c925e9c1cf15c0214625@localhost.talkaboutprogramming.com>,
TGOS <spamcop@_nospam_tgos.net> wrote:
>I like OO programming... in general, but the concept has several flaws.

What concept doesn't have flaws?

>I
>tried to fix these by simply creating an own OO language just as a proof
>of concept, that it's possible to create an OO language without all these
>flaws.

Can you post more information on what you've done so far? I think it'd
probably make for interesting reading.

>Animal -> Bird -> Penguin
>Animal -> Bird -> Ostrich
>
>Assume Anmial has a method canFly().
>
>animal.canFly() is abstract

I'd rather put a method 'fly()' in Bird, to avoid the problem you mention.
Asking animals if they can fly is the logically the same as switching on a
type code which is exactly what polymorphism is supposed to help fix.

>OO Programming means: You either can't ever add methods to an object or
>all sub-classes must be under your control. The second other people build
>their work on top of your work, you break their work by modifying any of
>your classes.

Yeah, in general that's called the fragile base class problem. It's one of
the reasons people tend to like interface inheritance, etc. rather than
implementation inheritance. With that, your library provider might give
you an interface to code against:

interface IAnimal
{
 // ...whatever...
 bool canFly();
}

A later version of the library might also offer

interface IAnimal2 : public IAnimal
{
 bool canSwim();
}

Then, objects can implement either one or the other. If the library can
tell that an object doesn't implement IAnimal2 it can then proceed knowing
that the object doesn't know if it can swim or not.

Another reason this is beneficial is that the fragile base class problem
extends to things like class layout. That is, if I give you an object
library and you compile against it, the code the compiler emits for yor
code is going to be full of assumptions like The _foo field of the Bar
class is at offset 48 from the beginning of the object. If I add a field
to a base class in my library, that can change the offset of _foo and
require all code that links against my library to be recompiled.

>Going by OO logic, all manipulations to an image are supposed to be
>methods of Image.

That's not necessarily true. It's perfectly possible to define tool
objects that can manipulate images. Rather than saying this:

  Image img = new Image("foo.bmp");

  img.GaussianBlur(12);

You might say this:

  Image img = new Image("foo.bmp");
  GaussianBlurFilter filt = new GaussianBlurFilter(12);

  filt.apply(img);

If you want to draw on an image, you might ask it for a canvas object that
provides a set of drawing operations. There are a bunch of different ways
to avoid creating so many methods in an object. There are also a bunch of
reasons to avoid it, many of which you address in your post.

>fracImage = new FractalImage((Image) geogrImage);
>
>and then I want to store it as JPEG:
>
>jpgImage = new JPEGImage((Image) fracImage);
>
>And inside the whole picture data is always copied around... surely, this
>will result in fast application...

Not necessarily. It's pretty easy to use techniques like copy-on-write
to minimize this. You might end up with new instances, but they'll share
most of the same representation until there's a good reason for them not
to.

-Mike

-- 
http://www.mschaef.com


Relevant Pages

  • Re: Stylistic question about inheritance
    ... > might want it so that type queries such as isinstance ... Are there other reasons to create a base class when I ...
    (comp.lang.python)
  • Stylistic question about inheritance
    ... We might imagine various kinds of expressions, ... If I were solving such a problem in C++, I would define a base class for all ... then derive the various kinds of expression classes from that ... there are reasons to have a base class anyway. ...
    (comp.lang.python)
  • Shared AND Overridable <-- Why not?!
    ... Is it just one of those 'it not BEST programming practices = stick up the ... stupid languages like java? ... the sort of reasons that just make you hate a programming ... so that i can have one base class called GlobalDB that must be ...
    (microsoft.public.dotnet.languages.vb)
  • Re: from a tutorial
    ... what would be the reasons for using the ... That's why the base class' constructor has to be called. ... the compiler will add the call to the base class' 'New' method. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: BUG: compiler allows for creation of objects without destructor compiled
    ... are MS compiler guys going to fix it? ... > not be generated because a base class destructor is inaccessible ... > Since there's an easy workaround it's ...
    (microsoft.public.dotnet.languages.vc)