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

RE: Uncuddled else?

by cwest (Friar)
on Oct 16, 2000 at 22:08 UTC ( [id://36987]=note: print w/replies, xml ) Need Help??


in reply to Uncuddled else?

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.

Replies are listed 'Best First'.
(jcwren) RE: (2) Uncuddled else?
by jcwren (Prior) on Oct 16, 2000 at 22:17 UTC
    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.
RE: RE: Uncuddled else?
by Nitsuj (Hermit) on Oct 17, 2000 at 06:53 UTC
    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
RE: RE: Uncuddled else?
by footpad (Abbot) on Oct 16, 2000 at 22:10 UTC
    Cool; I don't "cuddle" them by default. Phew!
    Thanks!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-18 21:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found