Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: var comparison

by MidLifeXis (Prior)
on Sep 06, 2012 at 18:48 UTC ( #992161=note: print w/ replies, xml ) Need Help??


in reply to var comparison

( grep { $var eq $_ } qw( foo bar ) ), but I would probably stick to your first example. This is close to the point where I would refactor out the comparison into a sub named with a description of what I was trying to check.

--MidLifeXis


Comment on Re: var comparison
Download Code
Re^2: var comparison
by nemesisgus (Acolyte) on Sep 06, 2012 at 19:01 UTC

    Thanks MidLifeXis. I like the grep way.

    Just... Why would you prefer the first option over using a regexp? This simplified example shows only two elements to compare but if there were more the expression could get quite long.

      Clarity of intent.

      • The first indicates (to me) that you are doing a whole string comparison. There is only one thing that the eq operation is able to do.
      • The regexp opens the possibility that there is something more complex happening. Even in this simple case, I still would need to switch languages to verify that there was nothing else going on in the statement.

      Admittedly, in this case, it is not a very complex regexp, so it probably does not make much of a difference. If I am doing a full string comparison, I reach for eq. If I am matching a pattern, I reach for a regexp. It seems to me that any cue that you can give to the future-you reading your code is a good thing.

      Update: As far as what to do once it gets a number of comparisons, refactor the comparisons out into a subroutine with a descriptive name and call it.

      The following statement takes a bit to digest:

      ( $var eq 'a' || $var eq 'b' || $var eq 'c' || $var eq 'd' )

      where this replacement, at least to me, is much clearer:

      ( isAnAllowedCharacter( $var ) )

      This also allows you to change the definition of what a valid character (or whatever you are testing for) is without changing the code that is performing the test:

      sub isAnAllowedCharacter { my $testee = shift; ( $testee eq 'a' || ... ) # or perhaps %valid_characters = map { $_ => 1 } ( 'a' .. 'd' ); $valid_characters{ $testee }; # or even perhaps my $validation_service = Remote::Validation::Service->new(...); $validation_service->isValid( $testee ); }

      Update 2: Missed this one before:

      This simplified example shows only two elements to compare but if there were more the expression could get quite long.

      It seems to me that a regexp with many strings can be just as unreadable as a series of $var eq '...' comparisons. Whitespace (and /x on the regexp) can make a world of difference.

      --MidLifeXis

        You've convinced me, specially with the part of the "cue that you can give to the future-you reading" ;^). I totally agree with your reasons.

        And nice snippet of code. I don't need something so elaborated at the moment but I will backup it for the future.

        Thanks again.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992161]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-09-20 16:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (160 votes), past polls