wfsp has asked for the wisdom of the Perl Monks concerning the following question:
For example, a new method calls a private init method. Should the init method return attributes?
or just set them:sub new { my ($class, $args) = @_; my $self = {}; bless ($self, $class); my ($attr_1, $attr_2) = $self->my_init($args); $self->{attr_1} = $attr_1; $self->{attr_2} = $attr_2; return $self; } sub my_init { my ($self, $args) = @_; # do stuff with $args; my $attr_1 = 1; my $attr_2 = 2; return ($attr_1, $attr_2); }
To my eye the latter looks neater but does the "action at a distance" make it less clear/maintainable than the former?sub new { my ($class, $args) = @_; my $self = {args => $args}; bless ($self, $class); $self->my_init; return $self; } sub my_init { my ($self) = @_; my $args = $self->{args} # do stuff with $args $self->{attr_1} = $attr_1; $self->{attr_2} = $attr_2; }
I'm using both approaches at the minute and I've met myself coming back. Are there better alternatives?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing object attributes around
by GrandFather (Saint) on Apr 26, 2008 at 11:13 UTC | |
|
Re: Passing object attributes around
by mscharrer (Hermit) on Apr 26, 2008 at 11:38 UTC | |
|
Re: Passing object attributes around
by stiller (Friar) on Apr 26, 2008 at 11:49 UTC | |
|
Re: Passing object attributes around
by dragonchild (Archbishop) on Apr 26, 2008 at 16:56 UTC | |
|
Re: Passing object attributes around
by doom (Deacon) on Apr 27, 2008 at 00:28 UTC | |
by mscharrer (Hermit) on Apr 28, 2008 at 15:28 UTC | |
|
Re: Passing object attributes around
by bart (Canon) on Apr 27, 2008 at 06:44 UTC | |
|
Re: Passing object attributes around
by shmem (Chancellor) on Apr 27, 2008 at 11:56 UTC |