Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
As I state in the OP, however, my point about readability and clarity is concerned more with more complex constructs, of which that example is not one :)
I got the impression the OP condemned all use of ?:. If your opinion is "use ?: when that's readable, use if/then/else when that is", you and I agree, because that's what I do. I use ?: when I find that more readable if/then/else, otherwise, I use if/then/else.

Chained/cascading ternaries quickly lose readability, especially when combined with other code elements.
So do chained/cascading if/then/else constructs. Or nested loops, or nested indices.

Your original example, I would write either as:

my $number = $logical_test ? $value : 0;
or
my $number = $value; $number = 0 unless $logical_test;
or
my $number = 0; $number = $value if $logical_test;
depending whether I want to stress the default value of $number. (I might use one of the latter two if $logical_test is expected to have a particular value, and it not having it is an exception, I use the former line if $logical_test could go both ways).
If $logical_test is 0 if it's false, I might even write it as:
my $number = $logical_test && $value;

I wouldn't easily write:

my $number; if ($logical_test) { $number = $value; } else { $number = 0; }
as it assigns an initial value to $number in a different scope than its declaration - IMO, that doesn't score bonus points when it comes to readability. It's also 7 lines instead of 1, although it can easily be shortened to:
my $number; if ($logical_test) {$number = $value} else {$number = 0}
But still, it's three lines. And worse, both blocks have two thirds of their tokens in common - which just shouts "factor out, factor out". Which would lead to:
my $number = do {if ($logical_test) {$value} else {0}};
But then I prefer:
my $number = $logical_test ? $value : 0;

In reply to Re^3: Ternary operators: a hinderance, not a help by Anonymous Monk
in thread Ternary operators: a hinderance, not a help by Tanalis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (6)
    As of 2020-05-30 04:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      If programming languages were movie genres, Perl would be:















      Results (171 votes). Check out past polls.

      Notices?