Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: RFR: Inside-out classes - a Class::InsideOut primer

by radiantmatrix (Parson)
on Mar 16, 2007 at 14:23 UTC ( #605160=note: print w/ replies, xml ) Need Help??


in reply to Re: RFR: Inside-out classes - a Class::InsideOut primer
in thread RFR: Inside-out classes - a Class::InsideOut primer

I disagree about the main reason for wanting them. They may have been created to solve inheritance issues, but based on how often I'm asked about how to create private and read-only attributes, that seems to be the primary reason many people would want inside-out objects.

I'm reluctant to address the more esoteric concerns (like subclassing issues) in a primer -- the goal of a primer is to get someone started using a particular skill or technique, and it should not assume too advanced a prerequisite.

Besides that, your characterization that I'm "mostly concerned with" the private/read-only capabilities is ridiculous: I mention that issue once in the introduction, and refer to it obliquely once more in discussing class design. However, that latter is necessary because Class::InsideOut does require the developer to consider whether one's attributes are public, private, or read-only.

You are, of course, entitled to your opinions, but that doesn't make mine wrong.

<radiant.matrix>
Ramblings and references
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet


Comment on Re^2: RFR: Inside-out classes - a Class::InsideOut primer
Re^3: RFR: Inside-out classes - a Class::InsideOut primer
by xdg (Monsignor) on Mar 16, 2007 at 15:02 UTC
    Class::InsideOut does require the developer to consider whether one's attributes are public, private, or read-only

    Well, perhaps it's clearer to say that Class::InsideOut emphasizes the decision through a choice of syntax.

    Class::InsideOut tries to require as few things as possible:

    1. Objects: All new objects must be passed to (or created by) the register() function
    2. Properties: All data structures for object properties must be passed to the property() function.
    3. Direct Access: The key to directly access the property for an object in a property hash must be generated using refaddr() (from Scalar::Util) or the shorter alias id().

    The public(), private() and readonly() functions just call property() with appropriate options.

    I agree that showing it in practice rather than explaining all this is more appropriate for a primer

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re^3: RFR: Inside-out classes - a Class::InsideOut primer
by Joost (Canon) on Mar 16, 2007 at 15:09 UTC
    I disagree about the main reason for wanting them. They may have been created to solve inheritance issues, but based on how often I'm asked about how to create private and read-only attributes, that seems to be the primary reason many people would want inside-out objects.
    Oh I don't know. I would guess that people who want private and read-only attributes are just used to "strict" languages. I have hardly ever found them useful, since I'm of the opinion that you shouldn't access attributes from outside the object at all (i.e. you always want accessors or other methods to set the usefull attributes anyway), and if someone does validate the encapsulation, it's his problem. In other words, from "the outside", the actual storage mechanism of attributes is not usually relevant, since you normally never access them directly anyway.

    When I use inside-out objects attributes it's when I know I'm going to have many differing inheriting classes. Especially mixing pure-perl and perl/XS code. That's where the safety is relevant - you don't want to overwrite some super/subclasses' internal data - which can be easy to do by accident if you don't know all the internals of your superclass, and if you're subclassing an XS class you usually don't even have a hash-based implementation anyway.

      OI would guess that people who want private and read-only attributes are just used to "strict" languages.

      I would agree more if you were talking about private vs. protected properties -- i.e. access controls from other classes.

      (N.B. I avoid the term "attributes" since we also have attributes in Perl. I think it's unfortunate that some inside-out class generators talk about object "attributes" and then use attributes for methods as well.)

      I think that generating accessors (and/or mutators) for every bit of state associated with an object is a mistake because it merges implementation and interface. That is the violation -- or really, repudiation -- of the principle of encapsulation. It's not about hiding data, it's about decoupling.

      If you are the only one using your class, then it may not matter -- but if you share your code with others and if you want to promise some degree of API stability, then I think that making accessors for everything is a mistake. On the other hand, if you're comfortable declaring the difference between accessors you mean to be public and accessors you mean to be private in documentation and letting violators do so at their own risk, then I can see why this isn't a big deal for you.

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

        I agree. What I meant to say is that every property that's relevant to the classes' user should have some way of getting and maybe setting it. Basically, I regard all properties as implementation details and private and they should only be exposed (via accessors) if that's useful. I certainly didn't mean that all properties should have a direct public interface. A classes' user doesn't have to know or care if a method is just a plain accessor or something else. All that matters is that the public API allows the user to do everything relevant and hide everything that's not.

        I avoid the term "attributes" since we also have attributes in Perl. I think it's unfortunate that some inside-out class generators talk about object "attributes" and then use attributes for methods as well.

        And that is an excellent point. I've been considering a slight rewrite, and I think introducing the term "properties" and using it throughout is the straw that pushes me over. I'll be spending some time on that in the next week or so (hopefully).

        <radiant.matrix>
        Ramblings and references
        The Code that can be seen is not the true Code
        I haven't found a problem yet that can't be solved by a well-placed trebuchet
Re^3: RFR: Inside-out classes - a Class::InsideOut primer
by bart (Canon) on Mar 18, 2007 at 15:32 UTC
    I disagree about the main reason for wanting them. They may have been created to solve inheritance issues, but based on how often I'm asked about how to create private and read-only attributes, that seems to be the primary reason many people would want inside-out objects.
    Then these people are excited about their new shotgun.

    Disallowing people to access the internal gains you nothing. It just makes debugging harder, as Data::Dumper doesn't see anything inside the objects.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://605160]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (15)
As of 2014-07-22 17:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (121 votes), past polls