Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re^3: var comparison by MidLifeXis
in thread var comparison by nemesisgus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-23 22:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found