|
Posted by Thomas Gagne on 05/17/07 16:48
Toby A Inkster wrote:
> gosha bine wrote:
>
>
>> In your everyday natural language, how often do you use verbs without
>> nouns and nouns without verbs?
>>
>
> Go away! (is an example)
>
> It's not so much a case of using nouns alone, or verbs alone; but OOP
> promotes nouns as "masters" and verbs as "slaves". A verb cannot be
> invoked without first referencing some noun.
>
That's one way of thinking about it. Or we could say methods require a
scope (they must), even if that scope is implicit it is still a scope.
The scope is some kind of object whether its HERE or THERE, it's got to
be somewhere. No matter where you go, there you are.
> As a practical example, consider this code in PHP:
> <snip>
> This can be used like so:
>
> dump_obj($database);
> dump_obj($user);
> dump_obj($template);
>
It could. Regardless whether it is a good example or not it is an
example of tight coupling, which is usually a clue that a method is
poorly scoped -- it lives somewhere it probably shouldn't.
> Now, probably the best pure-OO method for doing this would be to give each
> object you're likely to want to dump a "dump()" method.
I don't know by what measurement this would be considered the "best
pure-OO method." I can think of at least two other ways that are
acceptably OO that don't require redundant code. This example, if its
the "best pure-OO method" PHP allows, isn't a pattern as much as it's a
kludge. And because it's a kludge it carries all the usual baggage
kludges carry with them--including redundant code.
> , so you'd have:
>
> $database->dump();
> $user->dump();
> $template->dump();
>
> However, that means that you need to re-implement the dump() method for
> each class your write. You *may* be able to use inheritance here, but
> because most OO languages (Java and PHP included) don't support
> multiple-inheritance, if your classes are already inheriting from
> some other parent class, then inheritance is probably not possible.
>
Since "pure OO" has already been brought up, then we should agree that
all classes inherit from a single base class, commonly called Object.
It would be consistent with the best OO approaches to implement the
method once in Object so all its subclass' instances could use it. It
provides the added benefit of allowing subclasses whose structure
prohibits dump() from working properly, to override its behavior so that
it does work predictably, which is something a locally scoped dump()
class, as you've described, would not be able to do.
> So you create the ObjectDumper class.
I'll skip the straw man "dumb" example, but the solution proposed with
the static method is an attempt to give the method a global scope.
That's all. If the syntax is a bother than it's not the fault of OO,
just of PHP's syntax. But even that is a mere inconvenience because a
better design is sexier than bad syntax is ugly.
>
> <snip>
>
> Some OO purists will contest that the above is somehow "better", but when
> asked why, they'll generally fall back on "because it's object-oriented"
> as if that were an end in itself.
>
No. Relatively better scoping is > relatively worse scoping. Some
people know this intuitively but have trouble describing it.
One approach you didn't describe by itself is also satisfactorily OO, if
you ignore its coupling--a locally scoped (instance) method. If an
object (or more precisely one of its methods) needs to do something
fairly unique to itself (good cohesion) but invasive to other objects
(bad coupling) it could implement that method inside its own scope, just
as it does any of its other methods.
> OO should not be treated as an end in itself. OO is often, some might even
> say usually, a good way of modelling complex projects, and classes provide
> a good way of organising code, minimising naming collisions and increasing
> code legibility.
>
> However, it doesn't always make sense.
This complaint is often repeated by people either attacking OO or
apologizing for it. Often (though not in this posting) OO is compared
to a hammer--then everything else is a nail. Well, if we consider
programming is a nail then perhaps some people are just using the wrong
end of the hammer?
> <snip>
>
> Java forces you down the object-oriented route every time.
I wish it did. We'd probably see better Java code.
> PHP (and PHP is
> by no means alone in this matter) allows you to use object-oriented methods
> when appropriate, and procedural programming when it makes sense.
>
This is a similar argument C++ proponents make. That by being
multi-paradigm (as the description above seems to say about PHP) it
allows the programmer more flexibility. Another way of looking at it
would be to say C++ is jack of all trades and master of none. Or
without analogy, that a multi-paradigm language is adequate for
everything but excellent at nothing.
--
Visit <http://blogs.instreamfinancial.com/anything.php>
to read my rants on technology and the finance industry.
Navigation:
[Reply to this message]
|