| Jan Eden 2005-08-06, 4:59 pm |
| Hi,
I recently rewrote a program using OO techniques. Because my scripts usuall=
y do not exceed two hundred lines, I am not too familiar with OO design gui=
delines in Perl.
So while I got everything to work (with the help of Randal's "Learning Perl=
ORM"), I am unsure about the preferred way to do the following.
My program consists of a constructor class, methods to add data to a class =
instance and methods to display the instance data.
I currently use the data methods like so:
sub display_children {
# displays subordinate pages
my $self =3D shift;
return '' unless $self->children();
...
}
sub children {
# determines the subordinate pages, returning an anonymous AoH
my $self =3D shift;
...
$self->{children};
}
In other words, the display class calls the corresponding data class, retur=
ning the empty string if the latter does so, too.
I wonder if I should rather place calls to the data classes in the construc=
tor method and base the return statement in the display class explicitly on=
the existence of appropriate data within the instance:
sub new {
...
$self->children();
...
}
sub display_children {
...
return '' unless $self->{children};
}
My second (related) question is: I currently invoke all the methods uncondi=
tionally, placing a conditional return statement early within each method:
return '' unless $self->{type} eq 'page';
While this makes the code outside the methods cleaner, I wonder if it would=
make a significant difference in performance if the calls to the methods c=
ontained all the conditions:
$self->children() if $self->{type} eq 'page';
Thanks in advance,
Jan
--=20
These are my principles and if you don't like them... well, I have others. =
- Groucho Marx
|