Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

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

Consider writing 2 == $val rather than $val == 2. It looks odd, but that way the common typo of = rather than == will give you an immediate compilation error rather than a nasty bug.

I often see people recommending this, but I have yet to actually see it in practice.

Is the = typo really so common that it warrants this kind of obfuscation? Not only does it "look odd," but it doesn't really make sense. You're not trying to find out if 2 equals the unknown, you want to know if the unknown equals 2--but in your code it's backwards. The truth is that I honestly can't remember the last time I made this mistake. No, I'm not bragging; my code has bugs too, and lots of them, but in my personal experience, the = typo really isn't a big problem. It's the type of mistake you make once, spend an enormous amount of time trying to debug, and upon discovering the deceptively simple cause, make sure you never make it again.

Maybe the "constant == unknown" syntax is more useful in C or C++ where you've only got one equality operator, but in Perl, you have both == and eq, and you must actually think about which one to use for each conditional, thus you're less likely to make the mistake of typing = instead of ==.

OT, but tilly, may I ask if you yourself use the "2 == $var" syntax?


In reply to Re: Re: eq vs. == by William G. Davis
in thread eq vs. == by c

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 admiring the Monastery: (4)
As of 2024-04-19 06:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found