Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: in search of a more elegant if then else

by Anonymous Monk
on Feb 18, 2010 at 23:57 UTC ( [id://824063]=note: print w/replies, xml ) Need Help??


in reply to in search of a more elegant if then else

thanks for your help! i think this will make my code look a little better.. :-)

what are the up's and downs of using the ternary operator?

Replies are listed 'Best First'.
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.)
      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)

Re^2: in search of a more elegant if then else.. (ternary)
by toolic (Bishop) on Feb 19, 2010 at 00:10 UTC
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.
      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?
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.

      Just to blur the lines a little :)

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-03-28 15:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found