Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

grantm (164)

  (email not shown publicly)

Just a simple [] guy, hacking Perl for fun and profit since way back in the last millenium. You may find me hanging around in the monestary [].

What am I working on right now? Probably the Sprog project [].

GnuPG key Fingerprint:
6CA8 2022 5006 70E9 2D66
AE3F 1AF1 A20A 4CC0 0851

Journal of grantm (164)

Sunday July 30, 2006
09:48 PM

Scary code

[ #30481 ]

From our production codebase:

unless (not condition1 xor (condition2 or condition3)) {

I think most of us would at least have to slow down to extract the essential meaning of that line.

The only saving grace of this piece of code was that condition1, condition2 and condition3 were all named boolean scalars.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • It was such a staple in my assembler days and now I can’t remember the last time I used it.

    Anyway, you can easily decomplexify that condition:

    1. We start here:

      unless ( not $condition1 xor ( $condition2 or $condition3 ) )

    2. unless should be reserved for trivial conditions:

      if ( not( not $condition1 xor ( $condition2 or $condition3 ) ) )

    3. The properties of xor mean that not(a xor b) trivially distributes as not(a) xor not(b):

      if ( not( not $condition1 ) xor not( $condition2 or $condition3 ) )

    • The properties of xor mean that not(a xor b) trivially distributes as not(a) xor not(b):

      You are wrong here. Take for instance the case where a and b are both false. Then a xor b is false, and hence, not (a xor b) is true. However, both not(a) and not(b) are true, and hence, not(a) xor not(b) is false.

      (a xor b) is equal to not (a) xor not (b). not (a xor b) is equal to not (a) xor b, or a xor not (b).

      Which means that the original case could have been written as:

        if ($condition1 xor ($condition2