[Pdx-pm] Too much validation
Joe Oppegaard
joe at oppegaard.net
Thu Nov 20 00:39:05 CST 2003
Mongers,
I notice that sometimes in OO code that I write I'll do something like
this obviously contrived example:
----
package WordCharacters;
sub new {
my ($class, $value) = @_;
# Validation check here
unless ($value =~ /^\w+$/) {
die "Non-word character used in value: $value";
}
my $self = {
value => $value
};
bless $self, ref($class) || $class;
return $self;
}
package main;
print "> ";
chomp(my $input = <>);
unless ($input =~ /^\w+$/) {
# More validation here
die "Word characters only!\n";
}
my $wc = WordCharacters->new($input);
----
So as a general rule of thumb, when should data validation be done?
Catch it early or catch it when it actually matters? Or both? (Ugh,
duplicate code).
Seems to me that typically you should catch it when it actually matters,
so the calling code doesn't have to worry about what is and isn't
acceptable. On the other hand, I guess I just feel dirty passing through
data that I know could be invalid.
-Joe Oppegaard
More information about the Pdx-pm-list
mailing list