Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: No braces

by The Mad Hatter (Priest)
on Feb 11, 2004 at 02:43 UTC ( #328107=note: print w/replies, xml ) Need Help??

in reply to No braces

Less clutter and more readability, really. Kinda like Perl's trailing conditionals...
$x = 8 if $x == 7;
which then you can use to semi-duplicate in a twisted way C and Java's style:
$x = 0 unless $x == 7; $x = 8 if $x == 7; # ... or maybe ternary operator? ... $x = $x == 7 ? 8 : 0;
Update Fixed code example as per Abigail-II's reply. Sorry about that, and yes, the first is less efficient, but we weren't talking about efficiency here, only style (not to say it isn't a good point to bring up).

Replies are listed 'Best First'.
Re: Re: No braces
by ff (Hermit) on Feb 11, 2004 at 06:15 UTC
    I like the ternary also instead of using braces. Less noise.

    But to help me make sense of the statement when I (or someone else) look at it later on, I use multiple lines as in:

    $x = $x == 7 ? 8 : 0;

    Thus, if you don't speak fluent "precedence", this structure does all the work. (Even bordering on "it's right when it's beautiful"?)

      The greatest fear of that operator comes from chaining. It's deceptively cool looking to use, so occasionally folks will do something like this (C, Java, Perl, it doesn't matter....)
      $x = (y == z) ? (a == c) ? foo (a,c) : bar (y,c) : (a == b) ? baz (a,y) : zap (y,y);

      Identation helps, but there are still cases when a nested if-comb can be more readable.

        A big problem is that Perl doesn't have a proper switch statement, which means a chained ? : is used far more often that it otherwise would.

        : () { :|:& };:

        Note: All code is untested, unless otherwise stated

Re: No braces
by Abigail-II (Bishop) on Feb 11, 2004 at 10:49 UTC
    $x = 8 if $x == 7; $x = 0 if not $x == 7;
    But that will always do two tests. If the test is expensive, or done a lot of times, it's less efficient.

    But what is worse is that it's wrong. The original code ends with x being 8 if it was originally 7. Your code ends with $x being 0, all the time.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://328107]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2018-05-24 22:15 GMT
Find Nodes?
    Voting Booth?