Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

•Re: Why get() and set() accessor methods are evil

by merlyn (Sage)
on Nov 25, 2003 at 17:02 UTC ( #309956=note: print w/replies, xml ) Need Help??


in reply to Why get() and set() accessor methods are evil

An object should have methods reflecting its external behavior. "Set your X value to 24" is rarely a nice reusable, maintainable external behavior.

People writing these kinds of objects generally haven't quite gotten in to "object thinking", and instead treat the object as a "smart record". Methods should instead be derived by understanding how the class might be used (including subclassing).

I think the Perl6 object approach where all variables are private to the class (and not even directly available to the subclasses) will make for some interesting designs, especially when thinking about reuse.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

  • Comment on •Re: Why get() and set() accessor methods are evil

Replies are listed 'Best First'.
Re: Why get() and set() accessor methods are evil
by Abigail-II (Bishop) on Nov 25, 2003 at 17:12 UTC
    I don't see how making attributes private to the class instead tucking them away into a reference makes people not treat objects as glorified structs using accessors to get to the internals.

    For me, objects are state-keepers. Methods may cause the object to change state, or they may be used to interrogate the object about its state (or both). How to object keeps its state however, is something only known the the class(es) of which the object is an instance.

    Abigail

Re: •Re: Why get() and set() accessor methods are evil
by Elian (Parson) on Nov 25, 2003 at 21:46 UTC
    Given that Larry's mandated that the Perl 6 compiler be capable of autogenerating lvalue accessor methods for attributes, I don't think you'll find that perl 6 will magically make things any better--you'll just end up with a lot of public attribute accessors.

    Granted, a.b = 12; is much nicer looking than a.set_b(12); or a.set_b = 12

      Granted, a.b = 12; is much nicer looking than a.set_b(12); or a.set_b = 12
      (Other post: If it's supposed to be an assignment it should look like an assignment, and setting a property or attribute is an assignment no matter how it's mediated under the hood.)

      FYI, in case you didn't know about it already, Attribute::Property makes a->b = 12 work in Perl 5. From its synopsis:

      my $object = SomeClass->new(digits => '123'); $object->nondigits = "abc"; $object->digits = "123"; $object->anyvalue = "abc123\n"; $object->anyvalue('archaic style still works'); my $john = Person->new(name => 'John Doe', age => 19); $john->age++; printf "%s is now %d years old", $john->name, $john->age;

      When modifying a value using an accessor method (which we dubbed 'archaic style'), things get ugly:

      Old: $john->age($john->age + 1); $john->name->(do { (my $n = $john->name) =~ s/Doe/Smith/; $n }); New: $john->age++; $john->name =~ s/Doe/Smith/;
      Having these things1 built-in in Perl 6 will make me happy.

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      1 The things that are called 'properties' in Perl 5 jargon2, but 'attributes' in Perl 6 jargon. Perl 5's 'attributes' are 'traits' in Perl 6. (IIRC)
      2 But not officially. They're often called 'attributes', but we should discourage that, because 'attributes' are already something else. Attributes are relatively new, though, so we can't blame writers of older documentation.

      ...but not as nice as a->set_b(12). :-)
      ...I know, I know arrow notaion is going away in Perl6. :-(

        You seem disappointed that the arrow notation is going away. Personally, I think this is a Good Thing. Aside from the fact that it will mean less typing for common things, it's a holdover from the old days of C. In C, a struct's members could be accessed with dot notation:

        typedef struct { double x double y; } POINT; POINT some_point; /* and later in the code */ some_point.x = 1.02;

        However, if you later had passed a pointer to a struct, you would access the struct members with arrow notation:

          some_point->x = 1.02

        Since Perl was largely designed to be familiar to C programmers, having an arrow to dereference things seemed fine. However, Perl 6 seemingly is intended to be familiar to a larger audience (in some ways), it makes sense to drop the C style arrow notation in favor of the dot notation that popular languages use.

        And just to keep things topical, by using the arrow notation, we may be encouraging C programmers to use objects like structs and that's just silly. Which is kind of the point of much of this thread :)

        Cheers,
        Ovid

        New address of my CGI Course.

        Nah. a[insert method call punctuation here]set_b(foo) is just nasty. If it's supposed to be an assignment it should look like an assignment, and setting a property or attribute is an assignment no matter how it's mediated under the hood.

        One of the few places (well, OK, the only place so far, but I won't rule out others existing) that I like lvalue methods or subs.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2021-01-17 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?