in reply to Re^6: Documenting Methods/Subs
in thread Documenting Methods/Subs
Personally, I could do without the assertion at this point, relying instead on programmer's ability to read; okay, so the errors may happen some way away, but the error should be explicit enough for the programmer to realise what's amiss. However, if you insist on asserting that the logger is a Logger, the correct place to do this is definitely within the setting method itself, that way you catch all errors when someone tries to set the logger to an illegal value (assuming they don't just monkey directly with the hash, in which case they deserve all that's coming to them.)sub new { my $class = shift; return bless {}, $class; } sub new_with_logger { my $proto = shift; my $logger = shift; return $proto->new->set_logger($logger); } sub set_logger { my $self = shift; my $logger = shift; # If you insist... eval { $logger->isa('Logger') } or croak "New logger must be an instance of Logger class" $self->{logger} = $logger; return $self; }
Your comment about attempting to assert that 'this has to be a logger object in all subclasses' being impossible seems to miss the point. After all, a later subclass could take as its logger argument either a Logger or a logspec, which can be used to build an appropriate logger. All of a sudden your assertion about the logger argument is incorrect.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: Documenting Methods/Subs
by adrianh (Chancellor) on Jan 19, 2003 at 09:38 UTC | |
by pdcawley (Hermit) on Jan 19, 2003 at 18:51 UTC | |
by adrianh (Chancellor) on Jan 19, 2003 at 19:12 UTC |