Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Re: eq vs. ==

by William G. Davis (Friar)
on Jul 20, 2003 at 17:47 UTC ( #276073=note: print w/replies, xml ) Need Help??

in reply to Re: eq vs. ==
in thread eq vs. ==

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?

Replies are listed 'Best First'.
Re: Re: Re: eq vs. ==
by tilly (Archbishop) on Jul 20, 2003 at 18:01 UTC
    Yes, I use the 2 == $var syntax. And yes, I have caught several typos over the years by doing so. YMMV.

    As for how common the error is, my experience suggests that we tend to not have a good sense of the errors we make. A useful, painful, and humbling experience is to keep a log of every error of yours that you catch. Categorize them, then go back periodically to review them and think about how you could have prevented them up front.

    Odds are that you will find you make a lot more mistakes than you think you do. And they will be of different kinds than you think. (Me, I make more "careless, stupid" errors than I care to admit.)

Re: eq vs. ==
by Abigail-II (Bishop) on Jul 21, 2003 at 07:49 UTC
    I use
    4 == func $a, $b

    because if I write it as:

    func $a, $b == 4

    I can never remember whether that's that calls func with the arguments $a and $b and compares that with 4, or whether it calls func with argument $a and the second argument true/false depending whether $b is 4 or not.


      I find both hard to read because of lack of parentheses. I guess I spent too much time in other languages that force that on you...


Re: Re: Re: eq vs. ==
by mbadolato (Hermit) on Jul 21, 2003 at 07:18 UTC
    I use that syntax and have also caught many more errors since doing that. It does look a tiny bit strange, but it really does help me catch some stupid typo errors.

    edit, removed the typo in 'typpo', which was rather ironic, no? :)

Re: Re: Re: eq vs. ==
by Jenda (Abbot) on Jul 21, 2003 at 10:52 UTC

    How common is the '=' vs. '==' typo depends on which languages do you mix. If you only write in Perl and C/C++/C#/Java/JavaScript, then it's quite likely you'll not make this mistake. But if you have to write some VB or Pascal or ... then it can happen easily. OTOH, I more often write '==' in a condition in VB than '=' in Perl.

    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

      But if you have to write some VB or Pascal or ...

      or SQL or XSLT... ;-(

      That has tripped me up in the past and although I initially found the "2 == $var" notation very confusing, I must say it is growing on me...


      In Pascal (at least in the version as intended by Go^H^HWirth), you are not very likely to make that mistake. Pascal uses = to compare two values, and := for assignment. So, you'd have to type an extra character (instead of leaving one off) to make this mistake, but even if you do:
      if a := b then begin ... end
      is a compile time error. Not really a problem. In Perl using assignment instead of comparison in a condition leads to a compile time warning. However, in C, the use of assignment instead of comparison is just find, and only some compilers will warn, if you turn on the appriate warnings. But when compiling C, it's much more common (and troublesome) to turn warnings on than in Perl.

      Conclusing, when coding in Perl, and certainly in C, is more likely to make this mistake than when coding in Pascal.


        I did not say (or at least did not mean to say) that it's more likely to make such mistake in Pascal than in Perl. What I meant was that if you use both Perl and Pascal, it's more likely you'll make this mistake in Perl.

        Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
           -- Rick Osborne

        Edit by castaway: Closed small tag in signature

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2023-09-29 00:19 GMT
Find Nodes?
    Voting Booth?

    No recent polls found