in reply to Re^5: Method Chaining and Accessors
in thread Method Chaining and Accessors

Personally, I think that get_foo is the cleaner of the two,
You have an odd notion of what's "clean", though of course you do have a point that any change in standard practice has potential to cause confusion.

The thing with me is I can't remember any case where I've just guessed what accessor I should be using, because there isn't any way to intuit the name of the attribute itself -- it always comes out of the documentation, half of the time it comes straight out of the SYNOPSIS.

Since $self->{attribute} is wrong it should look wrong.
Well, different things should look different, right? Which is why I think "get_*" and "set_*" methods look too similar.

Replies are listed 'Best First'.
Re^7: Method Chaining and Accessors
by f00li5h (Chaplain) on Apr 12, 2007 at 17:07 UTC

    ... At this point i'm not sure if i'm repeating myself or not ...

    q{ You have an odd notion of what's "clean" }. This is true, I learnt perl by reading merlyn's (et al) llama and TheDamian's Perl Best Practices, so I suspect that my view may be a little tainted by what I've been told is "just the right way to do it".

    Yes, you do get stuff from the SYNOPSIS the first time, but after that, you should be able to guess the interface of the module, getters, setters and all should become fairlly obvious to guess after a short period of use.

    q{ Well, different things should look different, right? }. Yes this is true, a constructor should look like one, and a setter method should look like one. Things should look different, but if something is wrong, it should always look wrong.

    $f00li5h = f00li5h->new(); $f00li5h->{moose}; # looks wrong because my objects never work +that way $f00li5h->get_moose; # *magic* $f00li5h->set_moose( 8 ); # *magic* $doom = Doom->new(); $doom->get_moose; # looks wrong - your objects don't work like + that $doom->{moose} # wrong-o $doom->moose; # *joy* this is the ticket $doom->set_moose( 21 ); # looks right to people who often use doom's + objects
    I suppose that i've just somewhat countered my own point, but get_* and set_* will always look right on instances of my class, and always look wrong on instances of yours (as long as the instances are named uniformly - ie you don't call an instance of Doom something like $f00li5h)

    I'd say that their looking similar makes for a uniform interface... also:

    $f00li5h->get_foo; $f00li5h->set_foo('new value');
    don't look too similar due to the argument to set_*

    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
      $f00li5h->{moose}; # looks wrong because my objects never work that way
      But... isn't that a "surprise"? Doesn't everyone expect perl objects to work that way? How can you deviate from the standard without creating mass confusion?
        Doesn't everyone expect perl objects to work that way?

        Mine don't. Mine work as I documented them. If you use them in ways apart from that documentation, things will break and you get to keep all of the pieces.

        The same rule applies for most other modules.

        q{ But... isn't that a "surprise"? }. This is a fair point, but it's only a surprise if the user of my class expects a hash based implementation (the docs will soon correct this faulty guess ^_^).

        In the case where he looks into my hash, he has decided to play with the underlying implementation details of my class and that's none of his buisness. It means I can't change how my classes work without breaking his client code, it also ties his code far tighter to my class and it's implementation, so he wont be able to later (painlessly) change to f00li5h::XS to gain the speed improvements it offers, or f00li5h::POE::Plugin to gain the cool poeness. Also, I may not be able to maintain a hash based interface when writing these new versions, so they may just never happen.

        At very minimum, letting people play with your hash innards is using variables in your API, and that's frowned upon.

        Although tricks with tie can allow my code to look like it's based on a hash, when it's not (kyle's Make an inside-out object look hash-based using overload.), but that's really best for porting, not for long term use...

        @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;