Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^3: Burned by precedence rules (== true)

by tye (Sage)
on Dec 27, 2008 at 17:50 UTC ( [id://732826]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Burned by precedence rules
in thread Burned by precedence rules

I disagree.

The problem is not in defining 'true' and 'false' constants other than ones predefined by the language. The real problem is using any such Boolean constants in (in)equality expressions.

One should never write anything like "== false" and one should rewrite any such code that one runs into (when practical). For one thing, it is needless complex, even redundant.

Yes, defining 'true' and 'false' constants exacerbates this problem and the problem is somewhat mitigated when a language has a first-class Boolean data type (that can't be made to hold other than the two special values) -- which also means that the language likely defines its own symbolic representations for those two special values.

But one can certainly sanely use 'true' and 'false' constants even in Perl. Just don't test for equality against Boolean values, especially against Boolean constants. And one should follow this practice even in languages where a real Boolean datatype mitigates the seriousness of such redundant constructs.

- tye        

  • Comment on Re^3: Burned by precedence rules (== true)

Replies are listed 'Best First'.
Re^4: Burned by precedence rules (== true)
by demerphq (Chancellor) on Dec 30, 2008 at 19:50 UTC

    I'm sure i shouldnt say this, but more than once I have written

    if ( !$x == !$y ) { ... } if ( !$x != !$y ) { ... }

    which I think is a fair exception to your rule. I kinda view ! and !! as "boolean constructors", and so long as both sides of your (in)equality are guaranteed to be a "boolean" obtained one of these constructors you can compare them.

    But I'm really just nit-picking. :-)

    ---
    $world=~s/war/peace/g

      There's a logical operator for that: xor

      if ( !$x == !$y) ) # bool($x) == bool($y) if ( !$x != !$y ) # bool($x) != bool($y)
      can be written as
      if ( !($x xor $y) ) # bool($x) == bool($y) if ( $x xor $y ) # bool($x) != bool($y)

      Unforunately, both sets have readability issues.

        In Perl 6 we can use positive boolean context, which helps readability a little:
        if ?$x == ?$y {...} if ?$x != ?$y {...}
        We can use junctions, which again helps the different case more than the same case:
        if not $x ^ $y {...} if $x ^ $y {...}
        or if you prefer:
        if !one($x,$y) {...} if one($x,$y) {...}
        That's probably enough ways to do it...

        Ah yes, scalar xor, i always forget about it. Personally the !$x == !$y makes more sense to me than !($x xor $y). But whatever.

        ---
        $world=~s/war/peace/g

Re^4: Burned by precedence rules (== true)
by gwadej (Chaplain) on Dec 28, 2008 at 20:46 UTC

    Even outlawing comparisons to the constants isn't enough.

    sub is_tested { return FALSE; }

    and then used as

    if( is_tested() ) { print "is tested.\n"; }

    and, by the way, FALSE is defined to be 2. This was the sort of thing that haunted me for days.

    I was using those expressions to describe the problem not the usage.

    G. Wade

      Wow, defining your FALSE constant to a value that isn't actually false is such an amazingly bad idea that I never even considered it. Yes, don't define Boolean constants that completely defy your language's concept of "true" or "false". Duh! :) Thanks for mentioning that.

      - tye        

        I have often said that I have learned as much from the really bad code that I have maintained as I have from the really good code I have maintained. (That applies to both my code and other people's code.)

        If a value of FALSE that is not false doesn't scare you enough, I won't tell you what I found FOUR defined as...<grin/>

        G. Wade

Log In?
Username:
Password:

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

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

    No recent polls found