in reply to Programming patterns
His point seems quite clear to me. When defining an interface for (say) a bank account class that has an calcInterest() method that must only be called if the account is in credit. And then exposing an inCredt() method for the caller to check before calling calcInterest():
while( my $accnt = $allAccnt->next ) { $accnt->calcInterest() if $accnt->inCredit(); }
Simply make calcInterest() perform the inCredit() check internally, and have it do nothing at all otherwise. Take no actions and raise no exceptions.
It adds one line inside calcInterest():
sub calcInterest { my $self = shift; return unless $self->inCredit(); ... }
And removes a test (and a scope) in caller code:
$_->calcInterest() while $allAccnts->next;
for every caller; and removes the need for, and the need to document externally, a method from the public interface.
This is simplifies everyones code; the interface and its documentation; and reduces coupling. Everyone wins.
|
---|