in reply to Re: Method Chaining and Accessors
in thread Method Chaining and Accessors
TheDamian suggests the use of get_foo and set_foo.This is because you have some attributes for which there will be a get_foo but no set_foo so the example was that a foo sub that is both a getter and setter has no way to indicate that you can not actually set foo, because if it dies then you have to wrap all your get/setters in eval { ... } and nobody wants that.
A good point, but I think you can make the case even more simply: undef is a useful value, and sometimes you want to set something to undef. If you use undef to indicate you want to do a get rather than a set, then you're stuck. So yeah, explicit getters & setters are good, and mutators aren't worth the saving of a few lines of code.
Myself, I'm inclined to name my setters "set_*" but to avoid using the prefix "get_" on my getters. I think that reads a little more naturally and helps to distinguish between the two:
my $attribute = $self->attribute; $self->set_attribute( $attribute );
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Method Chaining and Accessors
by f00li5h (Chaplain) on Apr 06, 2007 at 02:36 UTC | |
by doom (Deacon) on Apr 11, 2007 at 19:44 UTC | |
by f00li5h (Chaplain) on Apr 12, 2007 at 02:28 UTC | |
by doom (Deacon) on Apr 12, 2007 at 16:14 UTC | |
by f00li5h (Chaplain) on Apr 12, 2007 at 17:07 UTC | |
| |
Re^3: Method Chaining and Accessors
by TGI (Parson) on Apr 06, 2007 at 20:36 UTC |