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 | [reply] [d/l] |
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.
| [reply] |
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.
| [reply] [d/l] |
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 | [reply] |
Cool; I don't "cuddle" them by default. Phew!
Thanks! | [reply] |