[Bioperl-l] attribute setting and return value

Tim Bunce Tim.Bunce@pobox.com
Sun, 29 Sep 2002 10:34:00 +0100


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