develooper Front page | perl.perl5.porters | Postings from April 2010

Re: RFC: Perl manual pages -- update to follow the perlstyle.pod guidelines

Thread Previous | Thread Next
Jari Aalto
April 2, 2010 16:42
Re: RFC: Perl manual pages -- update to follow the perlstyle.pod guidelines
Message ID:
Tom Christiansen <> writes:

>>    - Are safer than average (in this case I refer to: 'and', 'or' ops)
> Not only are the spelt-out logical operators no safer than the
> punctuation versions and may even be worse ...

[more than enough for nightly reading; have to print this one out]

I agree everything you you demonstrated in general.

> they're also much harder to read.

But I don't see any of those demonstrating how "&&" and "||" would ever
be easier to read. They are punctuation, no matter what. Unfamiliar to
the reader unless explained.

Pick any non-programmer from the street and show them "&&" or the
alternative "and", I bet my money, that they know the English word
spelled out. But they fail to understand meaning of "&&" without "a
priori" explanation of the showed context. This would be inherently an
unfair contest to start with, because it helps that mathematic boolean
tables are teached using words OR and AND in schools.

It's different thing to say that "||" and "&&" have their uses. They do.
This has become an idiom:

    $var = $a || $b || $c;

I see it. I like it, but analyzing it shows that the OR-op here is used
for a side effect. We have all been taught to read it like "oh, assign
from $a, then try $b, and last $c if nothing else works"

That kinda is not what the logical test are all about. They are supposed
to test the truth. That's their primary function in mathematics.

Yes, I favor that use. But I don't for a second think that a beginner
fully understands it when he has just learnt about "logical operators".
This would be like explaining: "Oh, by the way, there is more, and more..."

Compared to the age old 1st line examples head, I would consider any 2nd
line to be the preferred one:

    if ((condition) && (condition) && (condition)
    if (condition  and  condition  and  condition)

    function(<do something>) || <die, error, you name it>
    <do something with function>  or  <die, error, you name it>

Or perhaps examples are better:

    if (defined($str) && (length($str) > 4))


    if (defined $str  and  length $str > 4)

That is what I meant by "safer". Extra parens are almost always needed
in order to make the traditional logical comparison safe.

> Punctuation is your mind's friend: Eschew it at your own peril, but
> lead not the little children into that desolate sea of flavorless
> porridge full of silent syntactic snafus. It doesn't help anyone. It
> hurts them.

I agree when punctuation is used in small doses; where applicable.

Punctuation can also get in the way. Consider this:

    if (exists($hash{'key'}) && defined($hash{'key'}) && (split(' ', $hash{'key'})[0] == 1))
        # Do something


    if ( exists $hash{key}  and  defined $hash{key}  and  (split ' ', $hash{key})[0] == 1 )
        # Do something

But I don't really think there is disagreement in here.

> That's enough for now. The rest of what you wrote is plenty
> reasonable. I just want to asphyxiate this myth that the spelt-out
> boolops are somehow more readable or safer than the originals, when in
> fact quite often they're precisely the opposite of those things. I
> know I'm not alone in this view.

My mistake not to give more examples.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About