in reply to "Accessors break encapsulation"?

Whether or not an accessor violates encapsulation depends upon the class. While there's a good argument that you should often just send messages to an object and let it decide how to respond rather than pull bits of data out of it, that's for objects that I see as modelling a process or need. A perfect example is this oft-repeated mistake:

if ($obj->error) { $obj->log_error; }

With code like that, the programmer has to remember to check for an error every time she might need to log one. More stuff to remember means more bugs. Instead, push the $obj->error call into the &log_error method and let the object ignore the message if there are no errors (though even exposing the &log_error method may not be good.)

However, sometimes a class can merely be viewed as a data type where you have fine-grained control over the domain of values the data can represent. I mean, I could create an integer variable which is only supposed to hold prime numbers. I could also create a Prime class which does the same thing but actually restricts me to prime numbers. In the latter case, my code may arguably be more correct but there's also nothing wrong with asking what the prime number is.

As for whether or not you should only test your published API, people disagree about that, also. On the "purity" side, there are those who argue that only the published API should be tested as that's all you are promising. Plus, testing the internals means you're more likely to break tests as you refactor, add, or delete code. However, I do like to test the internals, though I will often keeps those tests separate. The reason I do this is the API is often like the tip of an iceberg. There's a whole lotta code underneath. While you definitely want to test the API (and probably test that first), if you have a lot of code that this doesn't test directly, you may find a bug but have a much harder time tracking it down. Internals testing means you quickly find what's really broken, though this can mean more maintenance down the road.

Cheers,
Ovid

New address of my CGI Course.

Replies are listed 'Best First'.
Re^2: "Accessors (don't always) break encapsulation"
by mpeters (Chaplain) on Jul 19, 2005 at 17:35 UTC
    if ($obj->error) { $obj->log_error; }
    I assume that you are referring to some of the logging modules out there that do this (eg, Log::Log4perl). In the simple case I agree with you (and it's actually what most of them do behind the scenes anyway). But the problem occurs when you have a computationally expensive error string. For instance:
    use Data::Dumper; $obj->log_error(Dumper($some_very_large_structure));
    By using the first approach, this expensive call could be avoided if the object just gave you an accessor so that you could make the decision yourself if the logging was necessary.

    -- More people are killed every year by pigs than by sharks, which shows you how good we are at evaluating risk. -- Bruce Schneier

      Actually, I wasn't referring to those modules at all. Ignoring potential issue with premature optimization -- issues that I feel are more significant in Perl as it's rather slow and we don't have lazy evaluation -- if a logging module is there to allow the user to log stuff, then of course we need to figure out whether or not it's appropriate to call the method. In the example I used, it was a hypothetical example regarding something the programmer shouldn't have to worry about. In your example, you present an external API that the programmer should decide whether or not to call because that's the entire rationale behind the module.

      Sorry if I wasn't clear.

      Cheers,
      Ovid

      New address of my CGI Course.

        We sure do have lazy evaluation!

        $obj->log_error( promise { Dumper($some_very_large_structure) } ); package Promises; use overload( '""' => sub { $_[0]->() } ); sub promise (&) { return bless $_[0], __PACKAGE__ }
      I encountered almost exactly this situation. Here was my solution to keep code simple and avoid the excessive hit.
      $obj->log_on_error{sub {Dumper($some_very_large_structure)});
      The standard logging modules may not provide this functionality though.