[Bioperl-l] attribute setting and return value

Hilmar Lapp hlapp@gnf.org
Thu, 26 Sep 2002 16:18:14 -0700


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
-------------------------------------------------------------