Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by Joost (Canon)
on Mar 16, 2007 at 15:09 UTC ( [id://605166]=note: print w/replies, xml ) Need Help??


in reply to Re^2: 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.
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.

  • Comment on Re^3: RFR: Inside-out classes - a Class::InsideOut primer

Replies are listed 'Best First'.
Re^4: RFR: Inside-out classes - a Class::InsideOut primer
by xdg (Monsignor) on Mar 16, 2007 at 15:30 UTC
    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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-18 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found