in reply to Code review
Any suggestions as to how to simplify/condense this code snippet ?
As opposed to condensing it, if I came across this code I'd rather it be expanded:
my %Y; while (<>) { ... my ($y) = ...; next unless $y; if (@ARGV) { for (@ARGV) { $Y{$y}++; next if $y eq $_; } } else { $Y{$y}++; } }
As far as changes, I agree with the implementation of more descriptive variable names. I'm also picky about unless for some weird, unconfirmed reason (probably because 'unless' isn't seen in other languages and/or the '!' stands out loudly), so I'd write that as next if ! $y, but I digress.
One simplification that can condense things a bit is changing this:
if (@ARGV) { for (@ARGV) { $Y{$y}++; next if $y eq $_; } }
... to this:
for (@ARGV) { $Y{$y}++; next if $y eq $_; }
In other words, unless you explicitly need to, there's no need to check whether the @ARGV variable is true before you attempt to iterate over it. The for()/foreach() implicitly does this for you.
|
---|