Shawn wrote:
> > > > I would prefer it to return the old value:
> > > >
> > > > sub foo {
> > > >   my $self = shift;
> > > >   my $old_foo = $self->{'foo'};
> > > >   $self->{'foo'} = shift if @_ > 0;
> > > >   return $old_foo;
> > > > }
> > > >
> > > >

Someone asked:  (Sorry, I've lost the attribution)

> > > 
> > > If you set a new value, why would you want your client to still be
> > > using the old value?
> > 

I (Lawrence) responded:

> > Because you may need to put the old value back.  It's more of a
> > philosophical question.  "What should the mutator return?" 
> > 
> > The Object Purist says, "Nothing -- it should be declared void."
> > 
> > Another school says, "It should return the value you just set it to".
> > I think is useless -- it's a tautology.  You KNOW what value you just
> > set it to.
> 

Jenda wrote:

> You know the value you assigned to a variable as well so why does 
>       $x = 5
> evaluate to 5 and not the old value of $x? Maybe because you may want 
> to write
> 
>       $x = $y = $z = 0;

You may be amazed the number of beginning programmers who are baffled
by that construct.  

> 
> I bet most people would be very surprised if they ended up with $x 
> containing the old value of $y, $y with the old value of $z and $z 
> equal to 0. So IMHO just like
>       $x = 5
> evaluates to 5 and
>       $h{key} = 5
>       $h[$i] = 5
>       $hr->{key} = 5
> all evalate to 5 so should
>       $obj->size(5)
> evaluate to 5.

A very good argument.

> 
> I'm not sure it's such a bad habit. Though probably it's better to 
> use something like this:
> 
>       for ($fruit) {
>               $_->name('apple');
>               $_->color('red');
>               $_->texture('crunchy')
>       }
>

Hrm ... a Clever Construction, indeed.  Our local style guide bans the
use of for without an explicit index variable , so that bit of
cleverness would lead to the following silly code:

    for my $localfruit ($fruit) { 
      $localfruit->name('apple');
      $localfruit->color('red');
      $localfruit->texture('crunchy');
    }

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
        Lawrence Statton - [EMAIL PROTECTED] s/aba/c/g
Computer  software  consists of  only  two  components: ones  and
zeros, in roughly equal proportions.   All that is required is to
sort them into the correct order.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to