in reply to best practice

...and cuddle my elses

I think if you look at perlstyle you'll see that they encourage not cuddling your elses. I'd imagine that the reason is (this is the reason I don't like cuddled elses anyway) that the "if" doesn't line up with the "else". Example..

# cuddled - I feel that it's harder to read. if ($foo) { if ($bar) { print "We have a foo and a bar.\n"; } else { print "We have a foo.\n"; } } else { print "You have no foo.\n; } #uncuddled - one way (the way I use) if ($foo) { if ($bar) { print "We have a foo and a bar.\n"; } else { print "We have a foo.\n"; } } else { print "You have no foo.\n; }

update: as dragonchild and MeowChow point out, this is still something that people don't agree on. Some people write their elses (and subs for that matter) as MeowChow has shown. That's why I mentioned that my example was "one way". I just wanted to point out that you might have misread perlstyle

Replies are listed 'Best First'.
by dragonchild (Archbishop) on Aug 27, 2001 at 21:18 UTC
    Careful, you fanatic heretic! You're treading very close to a HOLY WAR ... we at feel that else's have been deprived of their right to feeling safe, warm, and fuzzy long enough! Your patriachal, unfeeling, psychobabble has caused irreperable damage to generations of hapless elses (and their bastard siblings, elsif) and you should be jailed for else-abuse!

    You'd best be careful, or I'm going to call the EPS (Else Protection Services) on you, you whitespacer!

    <Removes tongue from cheek>

    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

(MeowChow) Re2: best practice
by MeowChow (Vicar) on Aug 27, 2001 at 21:53 UTC
    Just as long as ya don't do this...
    if ($larry) { foo(); } elsif ($mo) { pie(); if ($stooges) { nyack(); } } else { cow(); }
                   s aamecha.s a..a\u$&owag.print

      I guess you'd best not make eye-tracks on my Perl code... :)

      Care to elaborate on your point? Personally, I prefer that my braces align vertically...


        Not much to elaborate on really, just an aesthetic preference. IMO, it consumes way too much vertical real-estate, and it's harder to scan over. Of course, ymmv :)
                       s aamecha.s a..a\u$&owag.print

      I actually prefered the above style for a long time. But it turns out many people can't stand it because it turns all code into a venisian blind of alternating white and black.

      On the other hand lining up the braces seems very natural.

      I have come up with the following compromise, which sacrifices ease of editing for readability:

      if ($larry) 
      {   foo();
      elsif ($tom)
      {   pie();
          if ($stooges)
          {   nyack();
      }   }
      else # Randal
      {   cow();
      BTW: Code Complete rocks