On Mon, 2008-08-11 at 20:28 +0100, Rob Dixon wrote:
> - To answer your question, yes it's a good idea to write accessor methods like
> type so that they will both read and write a value. But I would guess that you
> shouldn't be able to modify an object's type once it has been created?

There is much arguing over the best practise for writing accessor
methods.  Some prefer method that do both at once; others, methods that
do both at once but return the old value; and finally those that prefer
independent getters and setters.  Much of the debate is around how the
code looks when you have to temporarily set a value, process some data,
and then reset it to a previous value.  Here's what they look like:

  # One method to get and set, does not return previous value
  my $previous = $obj->access();
  $obj->access( $new_value );
  ...
  $obj->access( $previous );


  # One method to get and set, returns previous value
  my $previous = $obj->access( $new_value );
  ...
  $obj->access( $previous );


  # Separate getter and setter
  my $previous = $obj->get();
  $obj->set( $new_value );
  ...
  $obj->set( $previous );


Some argue that you should always use independent getters and setters
since some values are read-only; there is no setter.  A line like this:

  $obj->access( $new_value );

would do nothing but someone unfamiliar with the program might not
realize this and make a mistake in understanding the program.


-- 
Just my 0.00000002 million dollars worth,
  Shawn

"Where there's duct tape, there's hope."

"Perl is the duct tape of the Internet."
        Hassan Schroeder, Sun's first webmaster


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


Reply via email to