[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Does Style Count? (formally, 'file tests')



At 22.02 -0500 1998.10.28, Jon Jacob and Joetta Moon wrote:
>I hate the tertiary operation!  Any operator that you can't spell, must
>be suspect ; )
>
>Seriously, it is difficult to read at a glance.  It is efficient code,
>but it is difficult to read even for experienced programs.  It just does
>not flow as nicely as a regular conditional.

Well, I personally like the operator.  Get used to it.  ;-)

I think if used carefully, it is very readable.  In fact, I find it more
readable than an if {} else {} block in some cases.  Consider this again:

 $READ = -r $_ ? "YES" : "NO";

If you don't know the ?: operator, sure, it is confusing.  If you do, this
is very clear.  Test the readability of -r.  If it is readable, return
"YES".  If not, return "NO".  Seriusly, you should get used to it, because
people use it a lot, and it is not going away.

You do have a point, though.  This, while I like it, may be a bit over the
edge:

    my $reply =
        (exists($hash->{'_reply'}) ? $hash->{'_reply'} :
            exists($self->{REPLY}) ? $self->{REPLY} :
                $reply->[0] ne 'null') ? kAEWaitReply : kAENoReply;  # :D


>Also the _ "thing" is new
>to me but is also not so easy to read.  Why not use $_ is it does the
>same thing?  You don't really save anything, and too many folks are
>going to be unclear about what you are trying to say.

No, it saves you a lot.  _ says "use the last stat(), don't bother getting
the data again".  File tests and stat() operations are expensive.  Using _
saves perl a considerable amount of work.

use Benchmark;
stat('MacPerl');
timethese(10000,{
  slow => q{stat("MacPerl");},
  fast => q{stat(_);},
});

Benchmark: timing 10000 iterations of fast, slow...
      fast:  0 secs (-0.00 usr  0.00 sys = -0.00 cpu)
            (warning: too few iterations for a reliable count)
      slow:  9 secs ( 9.43 usr  0.00 sys =  9.43 cpu)

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch