(This is a reply to The Accessor Heresy. I'm doing it as its own meditation because that thread is getting full, plus I feel that this deserves a bit more attention.)

There's a lot of talk about accessors in Perl's OO where there isn't in the OO for other languages1, and for good reason. But, it's not the reason most people think. When you're introduced to OO through Perl, you write accessors. But, that's not because you need to in order to write OO - it's to reduce the possibility of typos when dealing with hash-based objects.

When designing a class, one should be considering how the client will want to use it. In OO, interface is king. Let's take a circle, for example. Disregarding the thorny nest that is a Shapes class hierarchy, we know we want our circle to be able to take a value when it's being built. (We'll get to what that value is in a sec.) We also want to be able to ask it what its radius and area both are. So far, so good. So, we have the following:

Now, when building our circle, we may want to specify the radius or the area. So, instead of a single new(), we may need two constructors - new_from_radius() and new_from_area().

So far, so good. We have a circle object that meets every one of our requirements. Now, let's say we receive a requirement that says "We need to be able to change the circle's size." Ok ... the first idea that comes to mind is "Let's expose the attributes we're using and let the client change them as needed." And, that's the worst idea. The better idea is to provide methods that allow the client to adjust the size. So, maybe something like:

"Hah! You're using accessors now!" Actually, I'm not. An accessor exposes an attribute. I haven't ever mentioned implementation. All I've discussed is the interface, which can be satisfied by any number of implementations. As far as we know, the implementation could be a center point and any point on the circle itself. Or, it could be any three points on the circle. Neither of those implementations have a "radius" or an "area" attribute.

  1. I'm referring to real OO languages, like Smalltalk and Ruby. I'm going to bypass C++ and Java because they're mostly OO, not really OO.

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

In reply to Perl OO and accessors by dragonchild

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.