Re: How much oop is too much oop?



In article <u#Kv5PjSFHA.204@xxxxxxxxxxxxxxxxxxxx>,
dave@xxxxxxxxxxxxxxxxxxx says...

[ ... ]

> I suspect the trouble may be that the text books tend to give
> examples based on a single object hierarchy. When you have dozens
> of different objects collaborating in a task, then forbidding one
> from asking another about itself and what it can do is extremely
> counter-productive.

Rather the contrary -- it's often a good thing to do. Allowing such
questions to be asked almost always leads directly to _requiring_
that they be asked to use them correctly. This means two things: that
each class has to acquire knowledge about the other classes to use
them correctly (breaking encapsulation) and that the compiler can't
do much to enforce the restrictions about what objects can be used in
what ways.

> To continue my trivial example you may have a "plane surface object"
> which wants to tile itself with a given shape. It is extremely
> convenient if it can ask the shape whether it is one which can be
> used for this purpose (like a square or regular hexagon, but not a
> regular pentagon). Why hamper things by forbidding the question?

This displays both of the shortcomings outlined above. To work
correctly, the plane class has to know allows a shape to be tiled (or
prevents it from being tiled), and the compiler can't do anything to
enforce the correct use of shapes, preventing attempts at tiling with
shapes that can't be tiled.

If tiling is important in your application, you can make that
explicit:

class shape {};

class tileable_shape : public shape {};

struct plane {

tile(tileable_shape const &s);
};

and now the compiler knows that only tileable_shapes can be used to
do tiling, so it'll enforce this restriction. Likewise, the plane
class doesn't need to "know" anything about what makes a shape
tileable or not -- it just knows that tileable_shapes are tileable,
and others aren't.

One of the basic ideas of the type system is to explicitly encode HOW
types can be used, so when/if you find yourself "asking" an object
whether it can be used in a particular way/for a particular purpose,
what you're doing is creating your own type system -- but you're
fighting the compiler instead of using it.

Of course, it's also possible to go overboard with this -- attempting
to encode _lots_ of characteristics into the types can lead to deep,
brittle hierarchies. In some cases, this may simply reflect a messy
problem domain (e.g. natural language analysis). More often, however,
it reflects a lack of understanding of the problem, so the much
smaller group of general rules that _could_ be applied haven't been
recognized and applied.

--
Later,
Jerry.

The universe is a figment of its own imagination.
.



Relevant Pages

  • Re: PEP 3107 and stronger typing (note: probably a newbie question)
    ... You can have declarative static typing with very few type *checking* - I may be wrong here but I think one could even write a C compiler without *any* type checking. ... some dynamically typed languages have grown type-annotations for the very same reason: hinting the compiler about possible optimizations. ... Most of the errors I have to deal with are logical errors, not type errors, and given the kind of "logic" you can find in business applications, I have some doubt about what a static type system - even a _very_ smart one - could prove here, at least if we hope to deliver the application within time and budget constraints. ...
    (comp.lang.python)
  • Re: Python from Wise Guys Viewpoint
    ... > Neelakantan Krishnaswami wrote: ... > increase in expressive power for a static type system. ... polymorphic instantiations onto the compiler. ...
    (comp.lang.lisp)
  • Re: RAD vs. performance
    ... Yes, but only because some, not even all, programming languages make it ... But wouldn't you agree that writing an IDL compiler that generates code ... dynamically typed language, you could just feed the above code through ... if you "develop" a dynamic type system to provide ...
    (comp.lang.misc)
  • Re: LISPPA
    ... compiler to really be able to do much with it. ... Both languages support ... you have no clue what you are talking about. ... Milner type system" or "ML type system" and try to learn a few things. ...
    (comp.lang.lisp)

Loading