Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Journal of GAVollink (5690)

Thursday February 24, 2005
09:16 AM

Code Style - Request for Input

[ #23352 ]
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).

if ($var) {


if ($var)

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?


The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • 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.
    • Randal L. Schwartz
    • Stonehenge
    • 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) {

      ... where

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

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


  • 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 )

    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( $f

    • I may be alone, but if the conditional runs over one line I treat it as a block:
        $foo  eq $bar and
        $this eq $that
      ) {
      I don't know that anyone else does it that way though.
      • 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;
        • Woa, nelly. So - I'll just stick with ANSI style, then. You K&R folks can't actually seem to decide on a style.
          • 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