An overly complex conditional is an overly complex conditional regardless of whether it appears as part of an if statement or an unless statement.
I think I should've formulated the question in other words: "Quick, when will we enter into the block?".
- if ($bar !~ /bar/ and ($baz =~ /qux/ or not defined $undef)): If this doesn't match that, and at least one of the other conditions.
- unless ($bar =~ /bar/ or ($baz !~ /qux/ and defined $undef)): Err, well... applying DeMorgan... err... when there's no match... and...
Or, put it yet another way: if we know the values of $bar, $baz, and $undef, is it easier to walk the if or the unless conditional?
| [reply] |
unless (not $var) { ... }
into
if (not not $var) { ... }
The major transformation I did was simply eliminating unless's implicit not. The whole point of if being simpler is that it doesn't have unless's implicit not. Eliminating the not is what makes it simpler, not making the not explicit. (Although tye will point out there is value in making the not explicit over using unless.)
| [reply] [d/l] [select] |
No. But then I wouldn't code unless( not $var ){ in the first place, and I wouldn't transform a legitimate use of unless into if( not ... ).
But I would happily code unless( $var ) { in preference to if( not $var ){.
And if the logic of my code dictates that using a guard statement to quit early is preferable to an extended if statement, then I prefer the OP's:
unless( -d or $_ eq '.' or $_ eq '.. ) { die ... }; which I read as
Unless it's a directory and it's neither '.' nor '..' get the hell outta here.
which I think reads easily, and far better than either
if( not( -d or $_ eq '.' or $_ eq '.. ) ) { die ... }; which I'd have to read as
If( not( it's a directory and it's neither '.' nor '..' ) ) get the hell outer here.
or
if( !-d or ( -d and ( $_ ne '.' or $_ ne '..' ) ) ) { die ... }; and read as
If( it's not a directory or ( it is a directory but it's either '.' or '..' ) ) get the hell outta here.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
Your last expression is not equivalent to the others.
if( !-d or ( -d and ( $_ ne '.' or $_ ne '..' ) ) )
means
unless( -d and $_ eq '.' and $_ eq '..' )
It should have been
if ( !-d and $_ ne '.' and $_ ne '..' ) { ... }
"If it's not a directory, not '.' and not '..', then ..."
I can't understand
"Unless it's a directory and it's neither '.' nor '..' get the hell outta here."
I don't even know what it would be if it were grammatically correct English. Would it be
"Unless it's a directory, and unless it's neither '.' nor '..', get the hell outta here."
or
"Unless it's a directory, and if it's neither '.' nor '..', get the hell outta here."?
The answer is kinda moot since I don't understand either version.
By far, the three easiest for me to understand are:
-
"If it's not a directory, not '.' and not '..', then ..."
if ( !-d && $_ ne '.' && $_ ne '..' ) { ... }
(Because the tight binding makes it easier to process mentally.)
-
"If it's neither a directory, '.', nor '..', then ..."
if ( none -d, $_ eq '.', $_ eq '..' ) { ... }
(Because of the resemblance to English and French.)
-
... unless it's a directory, '.', or '..'
... unless ( -d || $_ eq '.' || $_ eq '..');
(Because of the resemblance to English and French.)
| [reply] [d/l] [select] |