Re^2: in search of a more elegant if then else
by ikegami (Patriarch) on Feb 19, 2010 at 00:11 UTC
|
It gets unreadable for all but the most trivial of expressions, but it's perfect for assigning one of two values to a variable. Also useful in that area are
# Use default when val is
my $var = $val || $default; # ... false (= stringifies to '' or '0').
my $var = $val // $default; # ... undefined. (Requires Perl 5.10.)
| [reply] [Watch: Dir/Any] [d/l] |
|
It gets unreadable for all but the most trivial of expressions
Right. It's possible to back yourself (or front yourself, if you're twisted :) into monstrosities with it. The classic C 12 Days of Christmas puts it to good (?) use, among its other distortions.
Like a lot of things in perl (or any language, really), you just have to keep an eye on yourself to be sure you stay inside the lines. My two rules of thumb are length and nestedness:
- If I start nesting, that's a sign I should write it out in more explicit form
- If it starts getting much longer than 1 line, or there're more than maybe one or two logical ops (&&, ||, etc) in the condition, I should probably switch to if/else
(None hard rules of course, but signs that I'm wandering off the beam and should take a step back to reevaluate)
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re^2: in search of a more elegant if then else.. (ternary)
by toolic (Bishop) on Feb 19, 2010 at 00:10 UTC
|
| [reply] [Watch: Dir/Any] |
Re^2: in search of a more elegant if then else
by aaron.m (Novice) on Feb 19, 2010 at 00:17 UTC
|
If it "fits," and is easy to read, use the ternary. Otherwise, use the if-then. Think in terms of which one will be easier to understand when someone else - ie. your boss - will be looking over your shoulder while you try to figure out a bug in something you wrote $x months or years ago.
Be kind to yourself. | [reply] [Watch: Dir/Any] |
|
I go for the Friday 4 p.m. test. It's broken, it's 4 p.m. on Friday, you've had a LOOONGGG week, and you want to think about going home.
Which one is going to be easier to read and fix?
| [reply] [Watch: Dir/Any] |
Re^2: in search of a more elegant if then else
by Xiong (Hermit) on Feb 19, 2010 at 13:51 UTC
|
You should note that the ternary operator and if-then-else don't do the same thing. The ternary operator is an operator, intended to return a value (which can be assigned, tested, or further operated upon). If-then-else executes one of two alternate blocks.
| [reply] [Watch: Dir/Any] |
|
print do{ if( $_ ) { 'fred' }else{ 'bill' } } for 0, 1;;
bill
fred
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [Watch: Dir/Any] [d/l] |
|
This gets a little prettier in Perl 6, since it parses the inside of parens as a statement:
say (if $_ { 'fred' } else { 'bill' }) for 0..1;
Also, you can usually omit the curlies on a do, if the insides can terminate the whole statement...which it can't if there's a modifier, as above, so we'll invert it:
for 0..1 {
say do if $_ { 'fred' } else { 'bill' }
}
Of course, there's also the ternary operator, spelled ?? !!, if you want it... | [reply] [Watch: Dir/Any] [d/l] [select] |
|
|
|