in reply to Re: Re: OO Getters/Setters
in thread OO Getters/Setters

With the direct access, I have no control over the field.

In Perl you do, via tied scalars in the internal attributes. I don't think this method is common, but it can be done.

With the accessor, I have complete control over the field.

If you're doing something more complex than setting or getting an attribute (no validation, etc.), then it's not a true accessor or mutator.

----
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

Replies are listed 'Best First'.
Re: Re: Re: Re: OO Getters/Setters
by dreadpiratepeter (Priest) on Dec 31, 2003 at 15:29 UTC
    If you're doing something more complex than setting or getting an attribute (no validation, etc.), then it's not a true accessor or mutator
    Since when? I have always understood an accessor to be a method that presents an attribute of the the object to the consumer and a mutator to be a method that allows the user to change an attribute of an object in a controlled way. By your reasoning, the following code should work, and any code in the mutator to prevent it disqualifies the method as a mutator:
    my $year = $obj->birthyear(); $year = "Elmer J. Fudd Millionaire"; $obj->birthyear($year);


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

      By your reasoning, the following code should work, and any code in the mutator to prevent it disqualifies the method as a mutator

      That's correct. Accessors/mutators, by definition, provide direct access to the underlieing attributes. Since Perl is a dynamically typed language, that means doing any sort of validation on the data makes it something other than an accessor/mutator. If you're in a statically typed language, then the language does some validation for you, and it would thus still be an accessor/mutator. The important point is that you're not doing validation in your own code.

      So what if it isn't an accessor/mutator? More often than not, that's a good thing. That isn't to say you have a well-designed object, but you're closer to it than if you provide direct access.

      ----
      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

        Accessors/mutators, by definition, provide direct access to the underlieing attributes. Since Perl is a dynamically typed language, that means doing any sort of validation on the data makes it something other than an accessor/mutator.

        Pedantry such as this seems useless to me. If the object's interface says the only way to access an attribute is by using method foo, then for all intents and purposes, foo is an accessor. Believing anything else pierces the veil of encapsulation and defeats one of the main purposes of object orientation.

        Ah, but now I'm into the fray... Quick! What's a "block structured language" and why is C not one of them? For bonus points, how does the fact that C is not block structured affect your ability to write useful applications?

        I'm not entirely sure where you learned object orientation, but it seems you need a new lesson in it. Object Orientation is a methodology a way of writing a program. Which means that its principles are language agnostic. Perl in my opinion is one of the worst languages to write oop programs in because it breeds hideous techniques such as yours. Attribute encapsulation is a cornerstone of OOP. The simple fact of the matter, is that accessors/mutators are methods to retrieve bits of object data or set bits of object data. What they do and how they do it is irrelevant. From my point of view, there are very few times when you would want to expose member variables (a.k.a attributes) to the public. Indeed, I've never encountered a situation which required this. I find the maintainability of encapsulation a Godsend. This allows me to trivially change the data structure without concern. You spoke of tied scalars etc, but by that argument you invalidated your previous argument over performance. You will never convince me that using a tied scalar is more efficient, easier to use, and easier to maintain than accessor/mutators. I'm sure you are a fine procedural programmer, but I would recommend you study up on your OOP before you go giving advice. May I suggest Object Oriented Perl.

        On a further note, there are times when pedantry is needed, this was not one of them. When misused pedantry is counter-productive and infuriating. It's like the force, use it well.