Code Style - Request for Input

GAVollink on 2005-02-24T14:16:48

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


Why K&R style for me

merlyn on 2005-02-24T14:53:45

I prefer K&R style because it eats up less vertical scrolling space on my screen. The indentation shows me that I'm inside a block... I don't need a whole line with a single open curly to show me that. But the close curly shows both the de-indentation and the close of the block... it'd be hard to cuddle that without going a bit nuts.

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...

if ( defined($vara) ) {
  if (1 == $varb) {
    if (2 == $varc) {
      &something;
    }
  }
}
... where it would be easier to read at 4 or 8 spaces. Like the way I do...

# 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*)

Which?

zatoichi on 2005-02-24T16:01:01

I have seen both used but I prefer the K&R way with an uncuddled else:
if () {
    # do it
}
else {
    # do it
}
But whichever way you choose, do it consistantly.

Will it?

cog on 2005-02-24T16:37:06

Will my preference come to haunt me one day?

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... :-)

ANSI Perl

samtregar on 2005-02-24T17:31:28

I haven't seen much Perl written with ANSI brace style, but MKDoc is largely written that way. I think you can feel free to do it however you want when you're working alone. When you're working on a team it makes sense to agree on a shared style and stick to it.

-sam

My style.

schwern on 2005-02-25T02:55:16

I use K&R style. I find ANSI style plus uncluddled else's eats up far too much room on the screen. My exception is when the conditional runs over one line. In that case I find the extra vertical distance provides some needed clarity.
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:
if(
  $foo  eq $bar and
  $this eq $that
) {
  ...stuff...
}
I don't know that anyone else does it that way though.

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.