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

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

by puterboy (Scribe)
on Feb 09, 2014 at 15:51 UTC ( [id://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...

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1074134]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-19 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found