[Bioperl-l] Re: Automatic generation of set and get methods

Lincoln Stein lstein@cshl.org
Fri, 15 Nov 2002 08:04:06 -0800


I like the validate_$field idea.  It makes what's happening explicit.

Lincoln

On Friday 15 November 2002 12:27 am, Steve Chervitz wrote:
> I inadvertantly sent off my last post before I was done typing. Here's the
> complete version of the autogenerated set/get accessor with the built in
> throw call if validation fails:
>
> --- Steve Chervitz <stevechervitz@yahoo.com> wrote:
>
> [ snip ]
>
> Then you hand-code the sub validate_<field> methods to do all the checking
> you desire. We could autogenerate the throw call on validation failure if
> we specify that the validate method should return nothing on success and a
> string on failure explaining the reason for failure:
>
>  *$func =
>     sub {
>        my $self = shift;
>        my $current = $self->{$field};
>        if(@_) {
>           $self->{$field} = shift;
>           if( $self->can("validate_$field") ) {
>              if( my $msg = $self->validate_$field ) {
>                 $self->throw(-text=>$msg,
>                              -class=>'Bio::Root::BadParameter',
>                              -value=>$field);
>              }
>           }
>        }
>        return $current;
>     }
>
> With this version, the module developer gets a fair amount of functionality
> for free and can focus on the logic necessary for validating the field's
> value within their hand-coded validate_<field> method. If it doesn't
> validate, just return a string explaining why, along with what values are
> acceptable.
>
> You'll note that I'm using the "set returns previous value" policy. My
> sense is that most existing code which relies on the return value from
> setters assumes it is the previous value, so if our autogenerated code does
> this, it's less likely to break existing code.
>
> Steve
>
>
>
>
> =====
> Steve Chervitz
> sac@bioperl.org
>
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Web Hosting - Let the expert host your site
> http://webhosting.yahoo.com