Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Some argue that the behavior or subclasses (or subtypes) should not change . . .

I dunno. HTML::Template::Dumper significantly changes the behavior of HTML::Template. If people want to argue that this breaks Liskov, that's fine, but it doesn't stop HTML::Template::Dumper from being useful.

$object->as_string

One important benifit is that YAML/Data::Dumper only show the state of the object's internal data structure, but not lexicals used for storing private data (see below). It might be private, but if it's for debugging or human-readability, it might still be important to print it out. So a mere Dump($obj); is often not enough.

Other observations:

1) One thing I often see is people storing things in the object's structure that should be private, or storing things in lexicals that should be accessible to subclasses. As a general rule, if you want a subclass to get at it, it should be in the blessed reference of the object. Or provide accessors/mutators that are only available to subclasses (not public, unless your design explicitly calls for it). If the data needs to be truely private, put it in lexicals declared at the top of the package.

2) If you have a factory method, do not hardcode the name of the class. Instead, put it in a subroutine, like this:

package My::Foo; sub BAR_CLASS { 'My::Bar' } sub bar_factory { my $my_class = shift; my $bar_class = $class->BAR_CLASS; bless { }, $bar_class; }

This better supports subclasses, which would otherwise need to override the entire bar_factory method (which could be extremely complex). Now they only need to say sub BAR_CLASS { 'My::Bar::Subclass' } to change exact output of the factory method. (It's the responsibility of the subclass to make sure My::Bar::Subclass implements the same methods as My::Bar).

3) Calling all your own methods should be done in $obj->method( @args ) form. Not method( $obj, @args ) or (worse) method( @args ). Both the bad examples break inheirtance, and the second one isn't a method at all. I could see an argument made for the method( $obj, @args ) form if performance is a concern and you don't care about subclasses or if it's a private method, but I see no excuse for the second form.

4) Document protected methods. Unlike some other languages, Perl doesn't have any very good ways to specify what methods should only be accessible to subclasses (though it has a multitude of hacked solutions). Ultimately, you need to document your protected methods, with warnings of fire coming down from heaven if somebody on the outside uses them. And make sure fire won't come down on ligitimate subclasses.

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

: () { :|:& };:

Note: All code is untested, unless otherwise stated


In reply to Re: Often Overlooked OO Programming Guidelines by hardburn
in thread Often Overlooked OO Programming Guidelines by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (8)
    As of 2014-08-22 07:19 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (148 votes), past polls