More compact way to write this
|
|
Thread rating:  |
Steve Bertrand - 03 Jul 2009 00:46 GMT Hi all,
I've been toying with different techniques in how to make this code more compact. I have a few sanity checks like this, but against different incoming fields.
The problem I have, is that I don't like the fact that the "if" condition contains the exact same line of code that a sub-section of the add_message() function is receiving as a parameter. I know there is a way to bundle it better, but in my testing, I haven't been able to do it.
While it may likely be mostly irrelevant, the code below is operating within the namespace of an object in the third level of the call stack. It has been passed a string to test (which gets copied into %$data), and an Error object which it must update if necessary. (I hope the code renders properly in the email).
# item name
if (length($data->{item_name}) == 0) { $error->add_message( "item_name is undefined" ); } if ($self->safe_string($data->{item_name})) { $error->add_message( "item_name has potentially dangerous chars:". $self->safe_string($data->{item_name}) ); }
Steve
ps. I'm currently working on a hash within my Vars class that will map names to error messages (and types).
Uri Guttman - 03 Jul 2009 01:03 GMT >>>>> "SB" == Steve Bertrand <steve@ibctech.ca> writes: SB> The problem I have, is that I don't like the fact that the "if" SB> condition contains the exact same line of code that a sub-section of the SB> add_message() function is receiving as a parameter. I know there is a SB> way to bundle it better, but in my testing, I haven't been able to do it.
use some temp vars (NOT named temp) to factor out the common code.
my $item_name = $data->{item_name} ;
SB> if (length($data->{item_name}) == 0) {
unless ( length( $item_name ) ) {
length will be false if it is 0 so you don't need to check against 0.
and if you don't allow '' or '0' you can drop length as well.
SB> $error->add_message( "item_name is undefined" ); SB> } SB> if ($self->safe_string($data->{item_name})) {
if( my $safe_name = $self->safe_string($item_name) ) {
SB> $error->add_message( "item_name has potentially dangerous chars:". SB> $self->safe_string($data->{item_name})
$error->add_message( "item_name has potentially dangerous chars: $safe_name" )
you can also use statement modifiers for the if blocks now that the code is shorter
so it looks like this now:
my $item_name = $data->{item_name} ; $error->add_message( "item_name is undefined" ) unless length $item_name ;
my $safe_name = $self->safe_string($item_name) ) { $error->add_message( "item_name has potentially dangerous chars: $safe_name" ) if $safe_name ;
that is shorter, faster (no blocks, no duplicate code), and easier to read in general.
uri
 Signature Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Free Perl Training --- http://perlhunter.com/college.html --------- --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
-- To unsubscribe, e-mail: beginners-unsubscribe@perl.org For additional commands, e-mail: beginners-help@perl.org http://learn.perl.org/
Steve Bertrand - 03 Jul 2009 01:44 GMT >>>>>> "SB" == Steve Bertrand <steve@ibctech.ca> writes: > [quoted text clipped - 43 lines] > that is shorter, faster (no blocks, no duplicate code), and easier to > read in general. Thanks Uri,
I get the gist of what you are doing here. My example 'item_name' was a bad one ;)
'item_name' is literally a key in a hash where the value is the name of a purchased item. It is extracted from a hash that has ~10 other purchase-type items (comment, amount etc) ;)
Not every key in the incoming %$data parameter has the same value type, so AFAIK, I have to check each item in the hash individually as opposed to just assigning it to a temporary var. (...I'm working away from a predecessor's method of using $tmp, $a, $b, $c, $left, $right and everything else, so I don't do things like that ;)
Common-code doesn't really bother me, so long as I'm writing it in a context where it needs to be repeated for different data types, and once it's done, I can re-use it. Shrinking the repeating code blocks is a different story.
I'm learning new tricks far faster than I can code. If I can get 100 lines of great working code done today, then while I'm reading tonight, I'll learn new ways on how to write that code ;)
Thanks Uri,
Steve
ps. The module in question is here: http://ipv6canada.com/Sanity.pm pps. I'm currently reading "Advanced Perl Programming" by Sriram Srinivasan. (Yes, I know there's a new version).
Steve
Uri Guttman - 03 Jul 2009 03:13 GMT >>>>> "SB" == Steve Bertrand <steve@ibctech.ca> writes: SB> Uri Guttman wrote: >> SB> The problem I have, is that I don't like the fact that the "if" SB> condition contains the exact same line of code that a sub-section of the SB> add_message() function is receiving as a parameter. I know there is a SB> way to bundle it better, but in my testing, I haven't been able to do it. >> >> use some temp vars (NOT named temp) to factor out the common code. >> >> my $item_name = $data->{item_name} ; >> SB> if (length($data->{item_name}) == 0) { >> >> unless ( length( $item_name ) ) { >> >> length will be false if it is 0 so you don't need to check against 0. >> >> and if you don't allow '' or '0' you can drop length as well. >> SB> $error->add_message( "item_name is undefined" ); SB> } SB> if ($self->safe_string($data->{item_name})) { >> >> if( my $safe_name = $self->safe_string($item_name) ) { >> SB> $error->add_message( "item_name has potentially dangerous chars:". SB> $self->safe_string($data->{item_name}) >> >> $error->add_message( >> "item_name has potentially dangerous chars: $safe_name" ) >> >> you can also use statement modifiers for the if blocks now that the code >> is shorter >> >> so it looks like this now: >> >> my $item_name = $data->{item_name} ; >> $error->add_message( "item_name is undefined" ) >> unless length $item_name ; >> >> my $safe_name = $self->safe_string($item_name) ) { >> $error->add_message( >> "item_name has potentially dangerous chars: $safe_name" ) >> if $safe_name ; >> >> that is shorter, faster (no blocks, no duplicate code), and easier to >> read in general.
SB> Thanks Uri,
SB> I get the gist of what you are doing here. My example 'item_name' was a SB> bad one ;)
SB> 'item_name' is literally a key in a hash where the value is the name of SB> a purchased item. It is extracted from a hash that has ~10 other SB> purchase-type items (comment, amount etc) ;)
SB> Not every key in the incoming %$data parameter has the same value type, SB> so AFAIK, I have to check each item in the hash individually as opposed SB> to just assigning it to a temporary var. (...I'm working away from a SB> predecessor's method of using $tmp, $a, $b, $c, $left, $right and SB> everything else, so I don't do things like that ;)
SB> Common-code doesn't really bother me, so long as I'm writing it in a SB> context where it needs to be repeated for different data types, and once SB> it's done, I can re-use it. Shrinking the repeating code blocks is a SB> different story.
SB> I'm learning new tricks far faster than I can code. If I can get 100 SB> lines of great working code done today, then while I'm reading tonight, SB> I'll learn new ways on how to write that code ;)
SB> Thanks Uri,
SB> Steve
SB> ps. The module in question is here: http://ipv6canada.com/Sanity.pm SB> pps. I'm currently reading "Advanced Perl Programming" by Sriram SB> Srinivasan. (Yes, I know there's a new version).
SB> Steve
 Signature Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Free Perl Training --- http://perlhunter.com/college.html --------- --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
-- To unsubscribe, e-mail: beginners-unsubscribe@perl.org For additional commands, e-mail: beginners-help@perl.org http://learn.perl.org/
Uri Guttman - 03 Jul 2009 03:19 GMT >>>>> "SB" == Steve Bertrand <steve@ibctech.ca> writes: SB> Uri Guttman wrote:
>> my $item_name = $data->{item_name} ; >> $error->add_message( "item_name is undefined" ) >> unless length $item_name ; >> >> my $safe_name = $self->safe_string($item_name) ) { >> $error->add_message( >> "item_name has potentially dangerous chars: $safe_name" ) >> if $safe_name ; >> >> that is shorter, faster (no blocks, no duplicate code), and easier to >> read in general.
SB> I get the gist of what you are doing here. My example 'item_name' was a SB> bad one ;)
item_name is an ok name if the term item is clear enough. in a proper context it works. item by itself without context is too generic. so is name. also it depends on scoping. key and val are poor names but if you are doing a tight loop over hash keys, they are fine.
SB> Not every key in the incoming %$data parameter has the same value SB> type, so AFAIK, I have to check each item in the hash individually SB> as opposed to just assigning it to a temporary var. (...I'm SB> working away from a predecessor's method of using $tmp, $a, $b, SB> $c, $left, $right and everything else, so I don't do things like SB> that ;)
then you should use a validation module or write a sub to do it yourself. doing validation inline for each field is tedious and repetitive.
SB> Common-code doesn't really bother me, so long as I'm writing it in a SB> context where it needs to be repeated for different data types, and once SB> it's done, I can re-use it. Shrinking the repeating code blocks is a SB> different story.
common code should bother you. it is a source of bugs (changed the code in one place but not the other!), it can slow things down if the code does repeated slow deep hash accesses, etc. consider the whole concept of a sub was to factor out duplicated common code into a single location.
SB> ps. The module in question is here: http://ipv6canada.com/Sanity.pm SB> pps. I'm currently reading "Advanced Perl Programming" by Sriram SB> Srinivasan. (Yes, I know there's a new version).
and the two versions are basically very different books. there is almost no overlap. the first one is also obsolete in many areas or covered better by new books and docs.
uri
 Signature Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Free Perl Training --- http://perlhunter.com/college.html --------- --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
-- To unsubscribe, e-mail: beginners-unsubscribe@perl.org For additional commands, e-mail: beginners-help@perl.org http://learn.perl.org/
|
|
|