Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Code blocks with ternary operator or trailing conditionals

by kcott (Bishop)
on Feb 09, 2014 at 04:13 UTC ( #1074094=note: print w/replies, xml ) Need Help??

in reply to Code blocks with ternary operator or trailing conditionals

G'day puterboy,

Trying to force multiple statements and conditions into one line of code does not necessarily result in "cleaner" code. You leave yourself open to all sorts of precedence and syntax issues.

Future maintenance can also become a headache: instead of simply adding "statement 3;" to a well laid out block of code, you may need to deal with bugs due to unforeseen precedence issues or you may need to rewrite that whole block because "statement 3;" introduces syntactical incompatibilities.

Code like:

if (condition) { statement 1; statement 2; } else { statement 3; statement 4; }

will almost certainly be more readable and maintainable than a syntactically correct version of:

condition ? {statement 1; statement 2} : {statement 3; statement 4}

Having said that, here's two ways that you might have written your first example:

$ perl -e 'print "hello\n" and print "bye\n" if 1' hello bye
$ perl -e 'print("hello\n"), print "bye\n" if 1' hello bye

Here's how you might have handled the ternary example (using my previous example you should be able to see a second way to do this):

$ perl -e '1 ? (print "hello\n" and print "bye\n") : (print "hello2\n" + and print "bye2\n")' hello bye

Had I needed to write code like this, I probably would have used do {...} blocks as shown by Athanasius (in fact, that was my first thought before scrolling down and seeing that solution).

You should also familiarise yourself with the precedence issues explained in the ternary operator documentation.

-- Ken

Replies are listed 'Best First'.
Re^2: Code blocks with ternary operator or trailing conditionals
by puterboy (Scribe) on Feb 09, 2014 at 15:51 UTC
    Based on the sage advice in this thread and my own past experience, I have decided on the following (personal approach):

    1. For very simple, repetitively used, definitively *scalar* compound statements with if/unless, use the comma operator to allow for single line visually cleaner code. e.g,
    $error = "Bad result", next unless defined $result;
    2. Consider using the 'do' statement if the compound statement is still very short and the code will be visually cleaner and easier to read but the operators are non-scalar, so the comma operator can't be used. e.g.,
    do{push @results, $item; next} if defined $item;
    Although, that may not be much better/cleaner than:
    if(defined $item){push @results, $item; next}
    3. Otherwise use the full-blown if/else alternative

    Of course, others will choose other options...
      Use parentheses for those list-eating functions.
      push(@results, $item), next if defined $item;
Re^2: Code blocks with ternary operator or trailing conditionals
by puterboy (Scribe) on Feb 09, 2014 at 15:36 UTC
    Indeed, I have tried these in the past -- but:
    1. The 'and' method requires one to be very careful about return values -- making sure you have the logic right the first time and that the logic never changes or has exceptions (as you and others have pointed out).
    2. The comma method is great (and even works with the ternary operator, if you use parentheses similar to how you did when using 'and'). However, this only works with scalar statements and can create real issues/confusion with operators that can operate on lists (like print).

    So thanks for showing these options -- even though I have mostly shied away from them -- due to the syntax/grammer issues you and others have raised. Thanks!!!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1074094]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2022-05-26 16:42 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (93 votes). Check out past polls.