I come from the C - where the styles are called K&R or ANSI, Perlers may call it something different (if so, please let me know).
K&R:
if ($var) { $used++; }ANSI:
if ($var) { $used++; }I am aware that Perlers generally seem to prefer K&R style (perhaps because Larry Wall prefers it). I have a tendancy of coding in ANSI style. I note that there are some modules written in ANSI style (though, very few).
Will my preference come to haunt me one day? Anybody have any reasons why I should learn to adopt the K&R style? [ I am aware to follow the style of a given project when hacking in that project ]. Anybody have any reasons why I should stick with ANSI?
Thanks
Re:Why K&R style for me
GAVollink on 2005-02-24T16:50:47
vertical space doesn't bother me (except for the five times, ever, that I've found myself coding on a green-screen [ or orange ] ). Admittedly, I need to get used to seeing it K&R style (get used to the block style of ANSI, and K&R can look a bit messy).I'd have much less problem with K&R if so many folks didn't also use a two-space indentation...
... where it would be easier to read at 4 or 8 spaces. Like the way I do...if ( defined($vara) ) {
if (1 == $varb) {
if (2 == $varc) {
&something;
}
}
}# vim: sw=4 ts=4
Re:Why K&R style for me
jplindstrom on 2005-02-24T18:41:39
Regarding that nested if, if, if structure. I know C++ programmers who like to code like that because it makes it easy to dealloc resources in an orderly fashion in case of an error. I don't know if there are other good uses for that idiom, but I really don't like it for a number of reasons.
Anyway, in Perl it's not very common, at least not in the code I've stumbled upon. There's seldom use for that kind of structure when most stuff is garbage collected or can be made to clean up automatically (like file handles closing the file when they go out of scope).
Re:Why K&R style for me
GAVollink on 2005-02-24T19:15:47
Hmm... I am a C programmer...That said, the same structure confusion can happen just as easily in nested loops.
foreach $a (@array) {}
(*shrugs*)
But whichever way you choose, do it consistantly.if () {
# do it
}
else {
# do it
}
I wouldn't call it "haunt", but I'm more fond of submitting patches for my kind of coding style (K&R) than for others...
Others may suffer from the same symptom, even if unaware of it...
-sam
if( $foo eq $bar and
$this eq $that )
{
...stuff...
}
I flip/flop between cuddled and uncuddled else's. In the end, your choice won't come back to bite you unless you decide to become a religious bigot about it.
Just don't do this.
if( $foo eq $bar )
{
...stuff...
}
Re:My style.
btilly on 2005-02-25T05:46:34
I may be alone, but if the conditional runs over one line I treat it as a block:I don't know that anyone else does it that way though.if(
$foo eq $bar and
$this eq $that
) {
...stuff...
}Complex conditionals.
schwern on 2005-02-26T05:18:37
To my eyes that's ok. You still have separation between the complex conditional and the code block making them distinct. What I don't like to see is this:if( $foo eq $bar and
$this eq $that ) {
$baz = 42;
}Re:Complex conditionals.
GAVollink on 2005-02-27T04:28:36
Woa, nelly. So - I'll just stick with ANSI style, then. You K&R folks can't actually seem to decide on a style.Complex styles
schwern on 2005-03-02T17:05:08
Sure we can (or I can). I don't fool myself into thinking I can apply a single rule to different conditions. K&R style is vertically efficient. ANSI style uses more vertical space to clarify the difference between the conditional and the code block. Apply as needed. If you have a simple conditional, use K&R. If you have a complex conditional, use ANSI.
If you ignore the pros/cons each style and just apply one to all your code like a cookie-cutter you're losing their benefits in the name of the dubious goal of consistency.