This is probably a strange question, and will show how little OO work I've done. But, I've been thinking about it lately so here goes nothin':

I get the idea behind encapsulation, you provide a public interface of methods to get and set data so the user doesn't mess around under the hood.

But, what about in the module itself? Say I've a Perl module that takes in some data, stores it as an internal representation, has some manipulation methods, and finally outputs the data. Should those manipulation methods be aware of the underlaying representation, or should they too use getters and setters?

Specific example: I'm currently working on a module at $work to help with a task simliar to what I mention above (data in, maybe manipulation, data out in new format). The object itself is a blessed hash reference, and the main data gets stored in a 2-d array, a reference to which is in the hash.

Say I've a method that needs to find a cell in that matrix and modify it. Should it use $self->getcell($x, $y) or $self->{'dataref'}[$x][$y]?

On one hand, using the getters/setters provides the benefits of encapsulation inside the class. I'd be able to seamlessly change the data structure with little work. And were there things I wanted to track about each get and set, I could track them right in the getters/setters, knowing everything went through them.

On the other hand, I've got many, MANY operations to do, and adding a few million subroutine calls slows things down significantly (but not insufferably). This is the argument I've always allowed to sway me, and until I began playing around with this idea I always let my modules directly access the underlaying data structures.

This may very well be a dumb question in addition to a strange one, but I'm still curious as to what the prevailing wisdom is on this.


In reply to Does one encapsulate a class from itself? by amarquis

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.