[Bioperl-l] get/set returns and arrays

David Block dblock@gnf.org
Tue, 27 Nov 2001 13:01:12 -0800 (PST)


Hi Chad!

On Tue, 27 Nov 2001, Chad Matsalla wrote:

> 
> I have an opinion on something-
> 
> I think that get/set methods like this:
> 
> sub units {
>         my ($self,$units) = @_;
>         if (!$units) {
>                 return $self->{units};
>         }
>         $self->{units} = $units;
> }
> 
> should be reversed so that they _always_ return a value, ie like this:
> 
> sub units {
>         my ($self,$units) = @_;
>         if ($units) {
> 		$self->{units} = $units;
>         }
> 	return $self->{units};
> }

Lincoln likes to return the previous value -

sub units {
	my ($self,$units) = @_;
	my $this_units=$self->{units};
	if ($units) {
		$self->{units}=$units;
	} 
	return $this_units;
}

so this convention is still in debate...

> 
> Somebody using that object can feel free to discard the result if they are
> confident that the set is actually being done but if you want to make
> sure, you can always check the result, like this:
> ok($object->set_something($number) == $number);
> 
> Would anybody object if I do this (always return a value) in my stuff?
> 
(I agree with you, and do that).

> And can somebody explain to me why it seems the convention is to return
> arrays rather then references to arrays? I am just not sure.
> 
Look at these:

foreach ($obj->some_method_call) {

vs.

foreach (@{$obj->some_method_call}) {

When you can get away with it, an array is much cleaner to work with in 
that way.

> Chad Matsalla
> 
-- 
David Block
GNF http://www.gnf.org  La Jolla, California
dblock@gnf.org        Let's not talk about the weather...