[Bioperl-l] attribute setting and return value
Hilmar Lapp
hlapp@lj.gnf.org
Tue, 1 Oct 2002 10:26:56 -0700
Hi Lincoln, if you get around to do this that'd certainly be great
as more consistency is always a good thing.
But generally I think the sanest thing to do, and which I think was
also the consensus of responses, is to disclaim any assumptions
about the returned value when you set a value. I could be talked
into requiring a certain behaviour, but IMHO it's not worth the
trouble, and once people do rely on it, it is broken too easily.
-hilmar
On Tuesday, October 1, 2002, at 04:10 AM, Lincoln Stein wrote:
> I chose my style to be consistent with LWP:
>
> sub my_attribute {
> my $self = shift;
> my $d = $self->{'attrkey'};
> $self->{'attrkey'} = shift if @_;
> $d;
> }
>
> In addition I thought this style was consistent with the BioPerl
> *documentation*, which says that the accessor returns the current
> value and
> optionally sets a new value. If I had realized that the BioPerl
> implementation returned the *new* value, I would have done it that
> way!!!!
>
> Since I don't rely on this behavior anywhere, I am happy to fix the
> canonical
> accessor to return the new value, but not for the 1.1.1 release.
>
> Lincoln
>
>
>
> On Sunday 29 September 2002 05:34 am, Tim Bunce wrote:
>> Another problem with both styles shown is that you can set an
>> attribute to
>> undef. Here's an alternative to the first style (it's also faster):
>>
>> sub my_attribute {
>> my $self = shift;
>> return $self->{'attrkey'} = shift if @_;
>> return $self->{'attrkey'};
>> }
>>
>> Tim.
>>
>> On Fri, Sep 27, 2002 at 12:04:23PM -0700, Steve Chervitz wrote:
>>> This is yet another "gotcha" that arises from having combined set/get
>>> accessors. It's convenient sure, but not so clean from an engineering
>>> point of view since it relies on a shared convention.
>>>
>>> I can't say I've never done anything with the return value from a set
>>> operation, but I don't normally rely on it. So IMHO, I'd recommend
>>> declaring that, unless it's explicitly documented in the method's
>>> POD,
>>> the return value from a set operation on a combined accessor is not
>>> specified and should not be used by the faint of heart.
>>>
>>> Steve
>>>
>>> --- Hilmar Lapp <hlapp@gnf.org> wrote:
>>>> While I tried to investigate a test failure in Lincoln's BioGraphics
>>>> I noticed a somewhat important style difference between bioperl and
>>>> Lincoln's:
>>>>
>>>> When we set an attribute, we always return the value that was
>>>> just set:
>>>>
>>>> sub my_attribute{
>>>> my ($self,$value) = @_;
>>>> $self->{'attrkey'} = $value if defined $value;
>>>> return $self->{'attrkey'};
>>>> }
>>>>
>>>> In Lincoln's style, the _previous_ value is returned (which in many
>>>> cases will be undef!):
>>>>
>>>> sub my_attribute{
>>>> my ($self,$value) = @_;
>>>> my $v = $self->{'attrkey'};
>>>> $self->{'attrkey'} = $value if defined $value;
>>>> return $v;
>>>> }
>>>>
>>>> Lincoln, I'm just second-guessing that after having seen this in
>>>> multiple getter/setters, that it is probably your preferred style.
>>>> If this is confined to only a few or one module, then my point is
>>>> almost moot (but maybe still worth mentioning).
>>>>
>>>> These styles are inconsistent with each other, and if an interface
>>>> is implemented in different styles (which is the case for e.g.
>>>> Bio::SeqFeatureI in Bio::SeqFeature::* and Bio::Graphics::*), the
>>>> returned values will not be the same, and also not comply with the
>>>> interface definition.
>>>>
>>>> It is going to be very hard to change this all over bioperl ... (I
>>>> guess it also hard to change this all over Bio::Graphics ...) (I
>>>> certainly do not want to rate the merits of one against the other
>>>> here -- to me both styles make a lot of sense)
>>>>
>>>> I don't think that anyone takes advantage yet of using the returned
>>>> value when setting an attribute (speak out if you do). Maybe the
>>>> sensible thing to do in order to prevent future havoc is to advise
>>>> people not to rely on the returned value when setting an attribute.
>>>>
>>>> Lincoln/Ewan/Jason/Heikki, any thoughts on this or votes?
>>>>
>>>> -hilmar
>>>>
>>>> --
>>>> -------------------------------------------------------------
>>>> Hilmar Lapp email: lapp at gnf.org
>>>> GNF, San Diego, Ca. 92121 phone: +1-858-812-1757
>>>> -------------------------------------------------------------
>>>>
>>>> _______________________________________________
>>>> Bioperl-l mailing list
>>>> Bioperl-l@bioperl.org
>>>> http://bioperl.org/mailman/listinfo/bioperl-l
>>>
>>> =====
>>> Steve Chervitz
>>> sac@bioperl.org
>>>
>>> __________________________________________________
>>> Do you Yahoo!?
>>> New DSL Internet Access from SBC & Yahoo!
>>> http://sbc.yahoo.com
>>> _______________________________________________
>>> Bioperl-l mailing list
>>> Bioperl-l@bioperl.org
>>> http://bioperl.org/mailman/listinfo/bioperl-l
>>
>> _______________________________________________
>> Bioperl-l mailing list
>> Bioperl-l@bioperl.org
>> http://bioperl.org/mailman/listinfo/bioperl-l
>
> --
> Lincoln Stein
> lstein@cshl.org
>
--
-------------------------------------------------------------
Hilmar Lapp email: lapp at gnf.org
GNF, San Diego, Ca. 92121 phone: +1-858-812-1757
-------------------------------------------------------------