Poly Couples (was: How to motivate use of OO?)
- From: "topmind" <topmind@xxxxxxxxxxxxxxxx>
- Date: 5 Jul 2006 20:45:51 -0700
Stefan Ram wrote:
nassegris@xxxxxxxxx writes:
So now I'm asking you people, what should I tell him?
the Refactor "Replace Conditional with Polymorphism".
(...) is the heart of why ObjectOrientedProgramming is
better.
http://c2.com/cgi/wiki?ReplaceConditionalWithPolymorphism
I assume your friend is using Microsoft® Windows, where files
are represented by areas within the Microsoft® Windows
Explorer. To print a PDF-File, he chooses the message »print«
from the context menu of the file area. To print a bitmap-File,
he chooses the message »print« from the context menu of the
bitmap-file.
He does not have to find out which program (method) is needed
to print a PDF-file or a bitmap file, respectively. He just
sends the same message »print« to any printable file object,
regardless of its type. A conditional was replaced with
polymorphism. I assume, he prefers it this way. Then, he knows
why it is better.
Fine. Oop is great for device drivers (like print drivers above),
animals, and shapes. But can we pleaaaaaase get something that reflects
what real people see in the real workplace? The vast majority of
software I write does not fit a device driver pattern. For example,
some use "employee types" with polymorphic methods for each sub-type as
a polymorphic biz example. In reality, you cannot divide employees into
a *clean* hierarchical taxonomy. And even if you did, new legal changes
could render the hierarchy useless, splitting existing categories, etc.
Lawmakers do not respect tree-ness. They don't care if new legislation
busts your nice little tree.
Thus, I've found it best to use a database to describe employees using
various attributes and flags. The code then processes these attributes
and flags without worrying too much about which taxonomy they are
attached to. The code is NOT shaped like a noun taxonomy. This is key
to FLEXIBILITY.
This way if the classification system for employees changes, as long as
the flags or attribs are the same, the code still carries out the
instruction. If the "noun" that triggers a given condition moves, such
as being tied to the department that you work in instead of an
individual person, then you only have to change the IF statement
expression a bit. Polymorphism would require *moving* the code because
the "responsibility" changes. Moving is a bigger change than altering
in-place. (Sure, you could get a fancy dancy OOP code processor to do
it for you, but one could make a CASE/IF statement processor also. I
too can build crutches.)
And the granularity of poly is often not fine enough. How do you
override HALF of a method? (without duplicating some behavior). IF
statements are simply more bendable and flexible to handle real-world
changes. You can refine them without moving lots of code around.
Polymorphism artificially couples your code to specific nouns and
specific noun taxonomies. If one is going to hammer on "coupling", then
you cannot pick and choose which coupling you keap and which you reject
without justification. If *all* coupling is bad, then poly is coupling
to nouns and noun taxonomies and is therefore bad.
-T-
.
- Follow-Ups:
- Re: Poly Couples
- From: Sasa
- Re: Poly Couples
- From: Bruno Desthuilliers
- Re: Poly Couples (was: How to motivate use of OO?)
- From: Alvin Ryder
- Re: Poly Couples (was: How to motivate use of OO?)
- From: Robert Martin
- Re: Poly Couples
- From: Bruno Desthuilliers
- Re: Poly Couples
- References:
- How to motivate use of OO?
- From: nassegris
- How to motivate use of OO?
- Prev by Date: Re: How to motivate use of OO?
- Next by Date: Re: How to motivate use of OO?
- Previous by thread: Re: How to motivate use of OO?
- Next by thread: Re: Poly Couples
- Index(es):
Relevant Pages
|