Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Code blocks with ternary operator or trailing conditionals

by puterboy (Scribe)
on Feb 09, 2014 at 15:51 UTC ( #1074134=note: print w/ replies, xml ) Need Help??


in reply to Re: Code blocks with ternary operator or trailing conditionals
in thread Code blocks with ternary operator or trailing conditionals

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...


Comment on Re^2: Code blocks with ternary operator or trailing conditionals
Select or Download Code
Re^3: Code blocks with ternary operator or trailing conditionals
by Anonymous Monk on Feb 09, 2014 at 19:32 UTC
    Use parentheses for those list-eating functions.
    push(@results, $item), next if defined $item;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2014-12-23 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (135 votes), past polls