Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Uncuddled else?

by footpad (Abbot)
on Oct 16, 2000 at 21:55 UTC ( [id://36984]=perlquestion: print w/replies, xml ) Need Help??

footpad has asked for the wisdom of the Perl Monks concerning the following question:

The novice hesitantly asks:

perlstyle mentions something to the effect of avoiding "uncuddled elses."

Er, what's an "uncuddled else?"

If I missed it in one of the FAQ's or man pages (I did look), please feel free to smack me with a link.

Replies are listed 'Best First'.
RE: Uncuddled else?
by cwest (Friar) on Oct 16, 2000 at 22:08 UTC
    Perl Style:
    if ( $_ eq 'true' ) { print "It's true\n"; } else { print "It's false\n"; }
    C Style:
    if ( $_ eq 'true' ) { print "It's true\n"; } else { print "It's false\n"; }
    Note the uglyness of the C style compared to the clean, compact nature of the Perl style, IMHO.
    --
    Casey
       I am a superhero.
    
      Note that the braces are completely unnecessary in the 'C' example, and looks quite pretty as:
      if (!strcmp (invar, "true")) printf ("It's true\n"); else printf ("It's false\n");
      You can make Perl just as ugly, and it wasn't very nice to yank 'C' in as a bad example. Remember, Perl is written in 'C'...

      Not to mention, 'C' doesn't have a 'eq' operator, doesn't support variables with $ in the name, and only single characters may be single quoted, so it wouldn't have compiled anyway.

      And sometimes, I sure wish Perl didn't require bracing the code block after 'if' or 'else', because I don't like cuddled style parans, anyway.

      --Chris

      e-mail jcwren
        Yes I know braces are optional when there is one statement in the block.

        Perl can be as ugly as one writes it.

        Many programmers come from 'C', find the braces mandatory and write Perl like they would write 'C' with braces. It's a fair example.

        Just because Perl is written in 'C' doesn't mean I can't think it's ugly ;-) ( note the smiley ).

        I know 'C' syntax, don't flame me over trivial matters.

        I'm not attacking 'C', I'm using it as an example. That's all. Not everyone writes 'C' like that, I don't.

        Please calm down and note the smiley!

        :c)~

        --
        Casey
           I am a superhero.
        
        Hmmm. i think that's what larry might have had in mind when he let's us reverse the directives, as in:
        $do_this if $this;
        But you're right, i also miss the one-liner-skip-brackets syntax :-( Also, I agree with you- flaming C is bad with or without a smiley face.

        But I find cuddled parentheses useful when I have a long block of code, let's say in a while loop, where I don't want to search of the end of some cryptic line where the matching parenthesis is :-O. of course, if one uses a real editor, parenthesis matching is builtin as a quick back hilite but since brackets are not color-coded to matching brackets, it seems simpler to look up your code a few lines and see a matching bracket, indented the same spaces as the matching one below. That's all. But I guess it's just a stylistic issue and nothing painfully important.

        AgentM Systems or Nasca Enterprises is not responsible for the comments made by AgentM- anywhere.
      This is a style issue.

      Both languages will ignore the usage of white space in this matter and therefore the style of "cuddling" your else's is valid in either language.

      You can do however you please with your elses because the whitespace is ignored. The use of whitespace is important stylistically, as is indicated below, both uses are valid, but someone sight reading the code might miss that there is a loop at all in the first case, which begs the point that white space is still "important."

      IE
      while(condition){statement}
      
      rather than
      
      while(condition)
      {
          statement
      }
      
      Really more important is that you delineate the code with tabs for readability regardless of what you do.

      The } else { is a "cuddled else." It is important to note that cuddling just the else is considered mature coding style, not skipping the use of whitespace altogether. The reason behind this being that the else is really part of an if statement, not a separate statement as it appears to be when uncuddled (stylistically). This can be confusing to someone just learning coding and good style, so generally it is taught to just leave it uncuddled, so the line looks the same as it would on the if line. Cuddling just the else looks like this.

      if (condition)
      {
          statement
      } else {    <== cuddled else
          statement
      }
      


      Just Another Perl Backpacker
      Cool; I don't "cuddle" them by default. Phew!
      Thanks!
RE: Uncuddled else?
by Maclir (Curate) on Oct 16, 2000 at 23:27 UTC
    It has been said that the perl style is:
    if ( some condition ) { print "It's true\n"; } else { print "It's false\n"; }
    I think a far more readable style would be:
    if ( some condition ) { print "It's true\n"; } else { print "It's false\n"; }
    Why am I proposing such a heretical thing, with the "else" starting on a line by itself, and being indented the same as its matching "if"? Simple - extend the statement to:
    if ( Some condition 1 ) { print "First case\n"; } elsif ( Some condition 2) { print "Second case\n"; } else { print "Default case\n"; }
    See - each clause of the compount if-elsif-else structure is logically (imho) aligned.
      I agree that it makes more sense (to me, at least) to do as you illustrate. In fact on pg. 547 of The Camel Book, Ed.2 it says:
      • Put a newline between a closing brace and else
      So I've always done it that way.
RE: Uncuddled else?
by Adam (Vicar) on Oct 17, 2000 at 03:52 UTC
    Despite the suggestion of L.Wall et.al. I have always prefered to put my braces on lines all by themselves, and have never considered it ugly. My reason is simple: no matter what editor I am using (even print outs!) the braces are always aligned vertically. This makes my life so much easier when I need to look through some code real quick to find some particular block. I can scan through my code on any machine with any editor. That seems a far cry from ugly.

    So in my (ocasionally) humble opinion you should not cuddle your elses. But of course, to each there own.

      Sounds like a case for a poll *grin*
      To cuddle, or not to cuddle?
      Personally, I don't: I like having open and close braces aligned: makes finding them easier (of course, xemacs bracket match withh full highlighting on in cperl-mode helps, too...!)
        #i like cuddling. vertical real estate is a premium. also: # easy to see just 2 statements if (...) { } elseif (...) { } if (...) { } # harder to see only 2 statements if (...) { } elsif (...) { } if (...) { }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-16 20:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found