The Only Correct Indent Style

I know we discussed indent styles once before, but I figured we might as well do it again. This time, I figured we might as well make it a poll and see which of these styles appears to be the most popular one.

Since not everyone knows these styles by name, let me give you an example of each one to refresh your memory. Usually, most programmers only acknowledge one true indent style and consider all the other ones an unholy abomination unto God. So here are the styles to pick from:

K&R

if (x == y) {
    x++;
    foo();
} else {
    x--;
    bar();
}

Allman

if (x == y) 
{
    x++;
    foo();
} 
else 
{
    x--;
    bar();
}

Whitesmith

if (x == y) 
   {
   x++;
   foo();
   } 
else 
   {
   x--;
   bar();
   }

GNU

if (x == y) 
  {
     x++;
     foo();
  } 
else 
  {
     x--;
     bar();
  }

Horstman

if (x == y) 
{   x++;
    foo();
} 
else 
{   x--;
    bar();
}

Pico

if (x == y) {
    x++;
    foo(); } 
else {
    x--;
    bar(); }

Banner

if (x == y) {
    x++;
    foo(); 
    } 
else {
    x--;
    bar(); 
    }
The only correct indent style
View Results

I actually prefer the Allman style. The braces should go underneath the opening block statement. I hate when the K&R people put them on the same line. All the other styles are just to bizarre.

How about you? Which style do you prefer?

This entry was posted in programming and tagged . Bookmark the permalink.



67 Responses to The Only Correct Indent Style

  1. mcai8sh4 UNITED KINGDOM Opera Linux Terminalist says:

    I’ve always gone the Allman way. Just seems neater and easier to read.
    My main reference book is K&R (the bible), but I’ve never used their indent style. Besides, I code mainly in Vim, and the auto-indent happens to follow the Allman style.
    To be honest I’ve never really considered my indent style. My programs are only really ever seen by me, and so in general they’re fairly sloppy, usually filled with useless comments, and var names that have no reasoning. Everything just seems to get mildly obfuscated by the time it reaches my fingers.

    Of late, I’ve been writing programs for a microcontroller, since this is fairly new to me, I’m trying to write better code to help the learning process, but it doesn’t always work. The only think I am consistent with is my indentation style.

    Reply  |  Quote
  2. For me, it’s GNU style because it’s what I am used to. This is from using Emacs and GNU indent all the time. Allman (bsd) style would be my second favorite.

    If someone sends me ugly code to read, I run it through indent first.

    Reply  |  Quote
  3. Steve CANADA Mozilla Firefox Windows Terminalist says:

    Why the hell is K&R winning? That’s got to be one of the worst indent styles. Jesus. Please, I hope never to work as a programmer on code that one of you who voted for that have done. Seriously.

    I agree with Allman or Gnu…both very similar except for the indents before the conditional brackets.

    Reply  |  Quote
  4. Ian Clifton UNITED STATES Mozilla Firefox Ubuntu Linux says:

    Allman… but damn some of those others are ugly :D

    Reply  |  Quote
  5. Allman.. but forced to K&R at Work (only exception: functions and classes are more Allman-Like)

    I think Allman is much more readable… everything besides those two is just insane.

    Reply  |  Quote
  6. Matt Doar UNITED STATES Mozilla Firefox Mac OS says:

    K&R is more compact but just as easy to scan as Allman. It’s my default style, but I go with whatever a project is using. If a project can’t agree on a style, it’s got bigger problems.

    Reply  |  Quote
  7. Yeah, Allman is my ideal choice. I like K&R the best after that but with a new line at the end of a closing bracket instead of piling another conditional in front of it which I think is ugly.

    Reply  |  Quote
  8. Tino UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    K&R is simply the one obvious choice. This is how the people that invented the language wrote the code, and if you are using their syntax, why do you second-guess their indentation? They knew what they were doing.

    Just like written text, code should be divided into logical segments/paragraphs by using empty lines as whitespace. With Allman this segmentation tends to be lost among the surplus single bracket lines. As far as I have seen, most of the people who promote styles other than K&R tend not to have understood what empty lines means in the code, and that proper use of whitespace can make the code more readable.

    Besides that, the question of indentation is more than anything a question of habit. Most of the “this style is better because”-arguments are ex-facto constructions. People should just stick with the original style and stop inventing their own perversions of it.

    Reply  |  Quote
  9. Chrissy UNITED STATES Mozilla Firefox Mac OS says:

    Allman was what I was taught when I learned Java, and it makes sense if you’re going to have a lot of nested brackets. But since I mostly write CSS, I prefer K&R

    Reply  |  Quote
  10. Dan UNITED STATES Konqueror Linux says:

    Allman FTW

    Reply  |  Quote
  11. Mats Rauhala FINLAND Mozilla Firefox Linux says:

    I don’t have a precedence to any of the styles. All of them are neat and readable, if you try. After using K&R for years all the rest were difficult to read, until I started practising Allman.

    Reply  |  Quote
  12. Mart SINGAPORE Mozilla Firefox Windows Terminalist says:

    Allman for me! I prefer to see code blocks in encapsulated by braces in that matter.

    Reply  |  Quote
  13. freelancer SWEDEN Mozilla Firefox Windows Terminalist says:

    “I actually prefer the K&R style. The braces should go on the same line as the opening block statement. I hate when the Allman people put them underneath. All the other styles are just too bizarre.”

    There, fixed your statement, Luke :P

    Reply  |  Quote
  14. Luke Maciak UNITED STATES Mozilla Firefox Windows Terminalist says:

    Heh, it seems that this is pretty much an Allman vs K&R thread. When I checked this post yesterday K&R was winning by like 5 votes despite the comments being very skewed towards Allman. Today Allman is ahead, but only by few votes.

    [quote post="2929"]If a project can’t agree on a style, it’s got bigger problems. [/quote]

    Very true. If code is handed down to me, I will try to follow whatever indent style they were using. But if I started the project, you can pretty much bet your sweet ass it is going to be Allman.

    @Tino: Your argument that:

    [quote post="2929"]the question of indentation is more than anything a question of habit. Most of the “this style is better because”-arguments are ex-facto constructions.[/quote]

    Would be much stronger if you didn’t say this in your previous paragraph:

    [quote post="2929"]Just like written text, code should be divided into logical segments/paragraphs by using empty lines as whitespace. With Allman this segmentation tends to be lost among the surplus single bracket lines. As far as I have seen, most of the people who promote styles other than K&R tend not to have understood what empty lines means in the code, and that proper use of whitespace can make the code more readable.[/quote]

    So you’re like “K&R is better because blah blah blah, and besides your counter argument is invalid because it is a question of habit and comparing styles makes no sense”.

    But you are right – it is a question of habit and preference. As far as I can tell, indent style really has no impact on your performance, and as long as it is consistent throughout the code it has minimal impact on readability. So yeah, this discussion is one of these little petty things that we programmers like to argue about.

    It’s like the vi vs emacs debate – most people are like “who the hell cares about this” but we can go on for hours comparing features, philosophy, design choices, performance, ease of issuing commands, modal editing vs complex keystrokes and so on. :P

    As for K&R and whitespace – that is a good argument for K&R. But the point of Allman style is to make the start of the block more distinctive. So function invocations, loop statements and etc have an extra line below them which contains the brace and white space. It allows to identify these rather important statements at a glance just by the way they are on a line of their own.

    You can do the same thing with K&R buy looking at the indentation but it is not as clear.

    @Chrissy: Yeah, I noticed that like 90% of CSS on the web out there is indented K&R style. WTF?

    Oh, and I’m part of the problem – since I usually steal a lot of CSS from pre-made templates, samples and etc I usually keep my stylesheets in K&R for consistency.

    Reply  |  Quote
  15. naum UNITED STATES Mozilla Firefox Mac OS Terminalist says:

    K & R

    Better than Allman because (a) it spares the superfluous newlines for whitespace and/or comments and (b) visually more aesthetic with nested blocks…

    As stated, please adhere to the indent style when modifying code… …nothing worse than code that is a mishmesh of various indent styles…

    And for the love of Gates, tabs to spaces please. Nothing is more annoying than editing source code that’s mangled because editor tab settings vary…

    Reply  |  Quote
  16. Nick UNITED STATES Mozilla Firefox Gentoo Linux says:

    K & R. I can deal with Allman too but it just seems wasteful in regards to vertical space. I’ve never seen the pico style before but I kinda like it. In regards to GNU per /usr/src/linux/Documentation/CodingStyle:

    First off, I’d suggest printing out a copy of the GNU coding standards, and NOT read it. Burn them, it’s a great symbolic gesture.

    Reply  |  Quote
  17. Tino UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    @Nick: Thanks for the link to the Linux kernel coding style. I had not read it before, and like much of Linus stuff it was both fun and insightful.

    @Luke: Yeah, my comment sounded a bit contradictive, but hey, this is holy war, there is no room for rationality :D

    [quote comment="11980"]But the point of Allman style is to make the start of the block more distinctive. So function invocations, loop statements and etc have an extra line below them which contains the brace and white space. It allows to identify these rather important statements at a glance just by the way they are on a line of their own.[/quote]

    But not all loop statements and if-branches are important. K&R rather lets you use whitespace for emphasis where it helps the reader the most, rather than to abide by rules.

    In K&R the function declarations are ~ headlines, and empty lines are ~ paragraphs. You can put the paragraphs where they make the most sense for your code.

    Allman, on the other hand, is as if someone proposes for written language that “Hey, lets turn every sentence that begins with ‘For, While, If’ into a headline.”

    And you see, ladies and gentlemen, why would Chewbacca want to live on Endor with a bunch of two-foot tall Ewoks? It does not make sense! ;)

    Reply  |  Quote
  18. David Cumps BELGIUM Mozilla Firefox Windows says:

    That’d be Allman for me, I kinda made the switch from K&R when I moved onto C# programming and stuck with it ever since.

    Code is art, and art is in the eye of the beholder :)

    Reply  |  Quote
  19. Boris Said UNITED STATES Mozilla Firefox Windows says:

    I like Allman because the code just look neater, and the braces are easier to read and understand with more complex coding.

    Reply  |  Quote
  20. Hannes Mozilla Firefox Linux says:

    I use K&R and sometimes pico as I like compactness. I find the extra line with a single character in Allman reel weerd and hard to scan over (though I’ve learned that it’s really just a matter of what each individual is used to).

    But unlike most ;-) programmers I’m not fanatic about it and I see the logic behind most of the other styles. Allman is indeed taught in all the school and university textbooks I know, but I agree with Tino: “K&R is the obvious choice”

    Reply  |  Quote
  21. John Galt UNITED STATES Mozilla Firefox Windows says:

    Allman is the ONLY indent style you should be using. Visual Studio (best IDE out there) has it as a default, as does ANSI standard. The only reason K&R used to be popular, is because back in the 80′s all programmers had were crappy 24 line monitors, so you had to preserve space. If you can’t afford a decent 20″ or bigger monitor these days, you’ve got bigger problems than indentation… A serious programmer needs at least 2 or three monitors anyway…

    K&R is for amateurs – listen to professionals and stick to Allman’s.

    Reply  |  Quote
  22. Gary UNITED KINGDOM Google Chrome Windows says:

    Allman is the clear winner for me. Code readability is key, and using an extra line is a small price to pay, especially given the size of modern monitors.

    Reply  |  Quote
  23. Leeroy UNITED STATES Mozilla Firefox Windows says:

    Allman. K&R is a travesty. People trying to save that one extra line at the expense of clarity should be shot.

    You like saving space so much, why don’t you go a step further and just write the entire block on one line.

    if(x==y){x++;foo();}else{x–;bar();}

    As for those other styles WTF, are they TRYING to be ridiculous?

    @ tino “This is how the people that invented the language wrote the code, and if you are using their syntax, why do you second-guess their indentation? They knew what they were doing.”

    You do know they had tiny green monitors, right? They tried to conserve a bit of space, and I don’t blame them, but today’s programmers have no excuse.

    Reply  |  Quote
  24. Karl SWITZERLAND Mozilla Firefox Mac OS says:

    For me, a slightly modified K&R.

    One of the advantages (IMHO) that proponents of Allman quote, is the extra readability that the blank line gives between a function header and body.

    e.g.

    int main ()
    {
    statement 1;
    statement 2;
    }

    I say, adjust K&R like this

    int main () {

    statement 1;
    statement 2;

    }

    For me, this gives the extra readability and the beauty of K&R.

    Reply  |  Quote
  25. Bo UNITED STATES Google Chrome Linux says:

    I like both Allman and K&R. But, after much “testing” and consideration, I choose K&R.

    It’s flexible. Add whitespace if you want.

    It’s easier to read (for me anyway). I fell like having the opening brace out of the way is nice. I don’t need it to see what’s going on.

    Reply  |  Quote
  26. Brian UNITED STATES Internet Explorer Windows says:

    Lately, coding in Java, I’ve been using a combination of Allman and K&R.

    I use K&R for class declarations, and use Allman for everything else. This makes it easy to just glance over the code and tell if a declaration is a class.

    Reply  |  Quote
  27. makeSense CANADA Internet Explorer Windows says:

    I love ruby style. There is no brace at all! Simple, clear and elegant. That is the way a language should have been designed.

    Reply  |  Quote
  28. JC AUSTRALIA Google Chrome Windows says:

    New projects: Allman.

    Exceptions: CSS, Javascript closures (and similar constructs elsewhere) can be K&R. These should typically be small blocks.

    End of debate. Readability is the most important thing, followed by consistency. K&R proponents should try something new.

    Established Projects: Use the project standard, or if none, as above.

    Tabs vs spaces: Easy. On any line, tabs to indent, spaces to align. And aligning should be an exception. Everybody is happy. If you don’t know what that means then your opinion is invalid. If you’re to lazy to do this properly then you wouldn’t work with me.

    Reply  |  Quote
  29. Marcos Sartori Google Chrome Windows says:

    Everytime I get my hands in a code written using Allman, I have to reindent it using astyle back to K&R if I want to perform any modification larger then a printf.
    Allman is horrible to read, blocks get confused, even the simplest code looks huge using this style. The Only place I accept brackets on an empty line is when opening classes definitions.

    The worst part is that my boss is an allman. And every code I write I have to indent and reindent back to allman.

    Reply  |  Quote
  30. Wookie AUSTRALIA Mozilla Firefox Windows says:

    Allman for PHP, and K&R for JavaScript – makes it easier to tell them apart at a quick glance. Javascript’s naming conventions lend themselves to a more compact code, and since a .js file counts as bandwidth, it’s good to make it as small as possible.

    PHP:

    function print_foo($text_data)
    {
    return $something ."/". $in_concat ."/". $with_something_else;
    }

    Javascript:

    function printFoo(textData) {
    return something +"/"+ inConcat +"/"+ withSomethingElse;
    }

    Reply  |  Quote
  31. Nikos Mozilla Firefox Windows says:

    @Carl: I agree 100%! I used to write in Allman and then moved into a project that
    used K&R. at first I was annoyed but then it grew on me and finally I settled on the ‘modified’ version. It’s both clear and flexible since you can skip the spaces for really simple statements.

    Reply  |  Quote
  32. Dave UNITED STATES Mozilla Firefox Linux says:

    Allman. The opening brace should be below its statement. The extra line and the fact that the braces line up vertically makes it easier to tell the structure of the code. Also I insist that the braces must be at the same indentation level as the associated control statement or function declaration, so those other weird styles are definitely out. I once worked with a guy who liked Horstmann, and found it to be acceptable to me. K&R tends to drive me nuts; its compactness inevitably makes code harder to read – I consider conciseness to be primarily a trait of the code itself, not the indent style around it.

    Reply  |  Quote
  33. Mark UNITED STATES Mozilla Firefox Windows says:

    So far, no one has commented on the fact the curly braces are not actually optional lexical elements of the iterative and conditional control structures. The curly braces are part of a completely different grammatical production known as <compound-statement> (a.k.a. <block>).

    <compound-statement> ::= '{' <statement-list> '}'

    <statement-list> ::= <statement> | <statement-list> <statement>

    The grammatical productions for <if> and <if-else> are:

    <if> ::= "if" '(' <expression> ')' <statement>

    <if-else> ::= "if" '(' <expression> ')' <statement> "else" <statement>

    If the curly braces were optional elements of the control structures, the grammatical productions for <if> and <if-else> would look like:

    <if> ::= "if" '(' <expression> ')' <statement> | "{" <statement-list> "}"

    <if-else> ::= "if" '(' <expression> ')' <statement> | "{" <statement-list> "}" "else" <statement> | "{" <statement-list> "}"

    The reason why we can use the curly braces to control a list of statements with a control structure is because the non-terminal symbol for the <compound-statement> grammatical production resides on the right-hand side of the <statement> grammatical production.

    <statement> ::= <if> |
    <if-else> |
    <for> |
    <do-while> |
    <compound-statement> |
    ...

    With the above said, if we follow the rule that any statement that is controlled by a control structure must be indented on the next line, then the only correct indent style is GNU. The GNU indent style is the only style that recognizes that <compound-statement> is a actually separate statement that controls a statement list.

    One last thing: there is exactly one control structure in C++, Java, an C# for which the curly braces are in fact lexical elements; namely, <try-catch-finally>.

    Reply  |  Quote
  34. XP1 UNITED STATES Opera Windows says:
    
    "Just like written text, code should be divided into logical
    segments/paragraphs"
    
    Paragraphs, you say?
    You mean like this?
    
    if (x == y)
        {    x++; foo(); var O; var Q = new Date(); var R =
         Q.getMonth() + 1; T = {}; j = p[g]; Z = P(a[g], j, Z);
         o = "";}
    else
        {    x--; bar();}
    
    Well, they do look like indented paragraphs, right?
    
    Eureka! I think that I may have just invented a new style:
    
    if (x == y)
        {x++; foo(); var O; var Q = new Date(); var R =
         Q.getMonth() + 1; T = {}; j = p[g]; Z = P(a[g], j, Z);
         o = "";}
    else
        {x--; bar();}
    
    I shall call it the "block quotation style" because it looks like
    an indented block quotation with quotation marks in
    written text, like this:
        "This is a block quotation. ...
         ...
         ...
         ... Blah, blah, blah."
    
    Or if you want something easier to read, try this,
    the block quotation list style:
    
    if (x == y)
        {x++;
         foo();
         var O;
         var Q = new Date();
         var R = Q.getMonth() + 1;
         T = {};
         j = p[g];
         Z = P(a[g], j, Z);
         o = "";}
    else
        {x--;
         bar();}
    
    The next statement goes on a new line, like a list.
    
    Enjoy!
    
    Reply  |  Quote
  35. redlenses UNITED STATES Google Chrome Windows says:

    Allman is my favorite. I dislike K&R

    if (x = 20)
    {
    blah;
    }
    else if (y = 10)
    {
    blah;
    }
    else // (x != 20 && y != 10)
    {
    blah;
    }

    is easier for me to read than:
    if (x = 20) {
    blah;
    } else if (y = 10) {
    blah;
    } else { // (x != 20 && y != 10)
    blah;
    }

    Reply  |  Quote
  36. holygrail LATVIA Mozilla Firefox Ubuntu Linux says:

    Of course I prefer K&R, but on my current work we use that ugly Allman style… not a big deal, in fact, but obviously K&R is the best. Anything else just ins’t made for white people.

    Reply  |  Quote
  37. Kon Mozilla Firefox Windows says:

    For some this might look something small. I believe it is something that has to do with logic and how do we perceive things. In all my works I had to do use the ugly old fashioned K&R style , but when I could I used Allman style and although that weren’t the policy, every one else could read my code easier so they didn’t complained. I am very happy to see that Allman style is gaining, and maybe in few years will be the programming industry main coding style.

    Reply  |  Quote
  38. Jeremy UNITED STATES Google Chrome Mac OS says:

    K&R style is that hard to read? Really? Try reading “monkey pounded on the tab key and spacebar randomly” style and get back to me. Or “10 programmers with 10 different styles and text editor configurations have completely scrambled this code” style.

    If you’re using any vaguely sane indentation style consistently, I seriously doubt your choice of style is the reason your code is unreadable.

    I’m waiting for the “K&R doomed our project!” anecdotes though. Superstition-driven development indeed.

    Reply  |  Quote
  39. I’m tired of reading the bias and pointless feuds over this. If you’re going to make arguments for one side, make sure you are actually comparing the orginal stules, not some variation. I just read several comments using Allman style without indents. One of which compared that to K&R without indent (which is a bit unfair). Another person also acted like a complete jerk and removed newlines in K&R completely to twist an argument. I also saw a racial comment in defense of K&R which is just inconsiderate in the 21st century.

    Let’s making sensible arguments here. Screen estate isn’t the main goal of K&R, and I feel like it never really was. Code needs to be properly grouped, and the best way to do this is by sectioning lines off and isolating it with new lines. There is no purpose to having an extranneous new lines that confuse this simple aspect of organization. I’m seeing a lot of people favoring Allman and not using any indentation whatsoever. Indentation is the main key to clearly seeing blocks, not curly braces. I would loke to see an argument that actually counters this.

    Changing the topic a bit, I want to see Horstman catch on. It has the benefeits of both. styles.

    Reply  |  Quote
  40. lake393 UNITED STATES Mozilla Firefox Windows says:

    Brandon,

    When you are complaining that various examples of code in this thread are not indented properly, I think you are forgetting that HTML does not preserve whitespace.

    Those people may very well have indented their examples, but the spaces were eliminated during the posting process. In order for spaces to be preserved, HTML requires <pre> tags (or an equivalent CSS attribute) to surround the text.

    For example, this blog makes it tricky to keep your indenting. I am indenting the below example, to no avail.

    int main(int argc, char**argv)
    {
    std::cout << "Hello world!" << std::endl;
    return 0;
    }

    But in the following case, I forced the lines to be indented by using nonbreaking spaces, a special HTML character code (&nbsp;).

    int main(int argc, char**argv)
    {
        std::cout << "Hello world!" << std::endl;
        return 0;
    }

    P.S. Allman is the best style.

    Reply  |  Quote
  41. John Galt UNITED STATES Mozilla Firefox Windows says:

    Brandon Cheng (gluxon) wrote:

    I’m tired of reading the bias and pointless feuds over this. If you’re going to make arguments for one side, make sure you are actually comparing the orginal stules, not some variation. I just read several comments using Allman style without indents.

    You do realize that’s just HTML removing whitespace, because the poster didn’t enclose it inside ‘code’ tags?

    Reply  |  Quote
  42. sigfried GERMANY Mozilla Firefox Windows says:

    I prefer Allmann. Blocks are easier to identify viusally by level of indentation.

    Reply  |  Quote
  43. devbytes PHILIPPINES Google Chrome Windows says:

    It’s Allman dude! I’m really really frustrated when reading K&R. It makes my eyes squint :(

    Reply  |  Quote
  44. compiler SPAIN Google Chrome Linux says:

    As a Python programmer, “forced” to code in C, C++ or Java, I would choose Allman with no doubt.

    I imagine that K&R places it in the same line to distinguish them from { } scope operators:


      (...)
      {
        int i;
        (...)
      } // i "dies" here
      (...)

    But it’s not enough for me that difference. I choose Allman Style, with 4 spaces as indent (soft-tabs) using Vim to code and autoindent + to indent :P

    Reply  |  Quote
  45. Tino SWEDEN Google Chrome Linux Terminalist says:

    It is funny how this one post just keeps on going and going with comments. It is almost as if this the issue was controversial…

    @compiler
    If you identify yourself “as a Python programmer”, shouldn’t your preferred style be Pico? (see original post), it seems quite similar to Python.

    Actually, you can do even better. Just use Python indentation and hack together a schellscript to be run during make that uses the indentation to add missing brackets during compile time :).

    Reply  |  Quote
  46. Jamal CANADA Mozilla Firefox Windows says:

    I have been programming using the Allman style, but have decided to try out Horstmann for a while. It may take a little getting used to looking at, but I love how the braces are aligned AND it saves you a line. I really can’t stand the look of K&R, but don’t particularly like how Allman creates verbose vertical whitepsace at times.

    Reply  |  Quote
  47. MeMyselfAndI GERMANY Google Chrome Linux says:

    when I started learning C Allman always made most sense to me – without knowing that it was “Allman”. For me it feels most intuitive and natural. Most will argue that K&R is the original and that Allman is a waste of space but I think that’s a low price for maintainability. I’m surprised that it got most of the votes now because “out there” (work and especially in the open source area) you mostly see K&R or Stroustrup style. I never could convince people to use Allman. So I had to to compromise and also got used coding in other styles. Jeet Kune Do or Bruce Lee style if you will. :P
    In the end I don’t think you can really argue about it because it all comes down to personal preference.

    Reply  |  Quote
  48. Chris UNITED STATES Google Chrome Windows says:

    Allman is great for the programming labs at university for helping students new to programming better understand syntax. And that’s why I enforce it in the Lab and grading. Once you start getting past a few hundred lines of code in your entire body of work, K&R is the obvious choice.

    The others? Kids are so cute these days…

    Reply  |  Quote
  49. Marvin CANADA Mozilla Firefox Ubuntu Linux says:

    How about Whitesmith’s style for CSS?

    body
    {
    background-color: #333333;
    color: #009999;
    /* blah blah */
    /* blah blah */
    }

    Reply  |  Quote
  50. Marvin CANADA Mozilla Firefox Ubuntu Linux says:

    There is no right way imo. I suggest to use any style so long it’s to increase code readability, as it’s humans who have to read the code to understand. I sometimes use K&R to indent elements of an array to assign to a variable in PHP. Also for some closures. For anything else, such as classes, control structure, All man all the way.

    (Hmm, the code tag is worse than I thought.) Anyway, below is “suppose” to be whiteman style, this comment form doesn’t seem to allow whitespace indentation.
    body
    {
    background-color: #333333;
    color: #009999;
    /* blah blah */
    /* blah blah */
    }
    p.small
    {
    color: silver;
    font-size: 9px;
    }

    Makes finding styles in large blocks of CSS code easier IMO. For anything else, I prefer ALLMAN.

    Reply  |  Quote
  51. Pingback: The C Programming Language | TomHartung.com UNITED STATES WordPress

  52. Pingback: The C Programming Language | TomWHartung.com UNITED STATES WordPress

  53. Christian UNITED STATES Google Chrome Windows says:

    K&R if you know what you’re doing, Allman if you are having trouble reading code. It’s really a non-issue since most IDEs will let you auto format it to your style of choice with the press of a button.

    Reply  |  Quote
  54. Chris B CANADA Mozilla Firefox Windows says:

    ALLMAN style is by far the best… and I wish it was made the defacto standard for languages like Java, C++, and C# because I’m tired of being told to do it the K&R way.

    The reason some of the other styles were invented was to save space on screen by reducing the number of lines of code. This allowed book publishers to save some paper at the cost of reduced readability.

    Bottom line: Allman style is the easiest to read and the most “foolproof”. You are less likely to forget an opening brace using Allman style, and it’s easier to work with blocks of code. Start spreading the word and get people to switch back to this style! (It is standard at Microsoft by the way.)

    Reply  |  Quote
  55. Matt Doar Google Chrome Mac OS says:

    “This allowed book publishers to save some paper at the cost of reduced readability.”

    That seems rather unlikely to me. Saving space on an older screen so you could see more code is more likely.

    Reply  |  Quote
  56. Chris B CANADA Mozilla Firefox Windows says:

    @ Matt Doar:

    Fair enough. Whether it’s page space in textbooks or screen space on tiny low resolution displays from the 1970′s, the K&R style of formatting tries to solve a problem that is no longer a problem at the expense of making code more difficult to read. The notion of “saving lines of code” is outdated.

    When looking at other coding styles besides Allman, they all have serious drawbacks:

    - K&R looks awful (the code looks broken), wrecks the block structure of the code, and makes it easy to forget opening braces by making them difficult and unpredictable to locate. (Since every line is a different length.)

    - GNU requires too many tabs and too much maintenance regarding the indenting. It also wrecks the block structure of the code by creating an imaginary additional indent “level” that does nothing.

    - Horstman is horrendous because you can’t easily copy/paste code lines around without blowing away the braces.

    - Pico and Banner are a “dog’s breakfast” to try and read. Compact yes, but horrific to look at and likely the cause of most of the world’s programming bugs.

    - Whitesmith is an inaccurate/failed version of Allman because the braces that delineate the start and end of the block are not where the actual start and end of the block is. The braces are simply at the wrong level.

    The only drawback to using Allman style is that the code is spread over more lines, a problem long since resolved with larger, higher resolution monitors, and the use of classes to break code into smaller more manageable chunks.

    Reply  |  Quote
  57. John Galt UNITED STATES Mozilla Firefox Windows says:

    Simply put – anyone advocating coding style other than Allman is a complete tool. Glad to see it’s beating ridiculous K&R by almost 100%…

    Reply  |  Quote
  58. Jeremy UNITED STATES Safari Mac OS says:

    @ Chris B:

    You must not work on the same codebases I do. I feel bad when a file pushes into the thousands of lines, but I work with an open source project where multi-thousand-line functions are the norm. I often have to scroll back and forth through pages of code just to figure out when a mutex is locked and unlocked.

    Of course, I contend fixing that would do far more for the code’s readability than any indentation style can.

    Reply  |  Quote
  59. Chris B CANADA Mozilla Firefox Windows says:

    @ Jeremy:

    I guess I would strongly question how that code base was designed and why it has functions with over a thousand lines of code in them. As you suggested, it sounds like there’s more wrong there than any particular indenting style alone can fix.

    Another point I wanted to mention was ROW LENGTH. Some of the most unreadable code that I’ve ever seen is unreadable because of artificially restrictive row lengths on the code lines. Instead of having one easy to read line, it gets split into two or even three lines of code. While it may be true that humans prefer narrow columns when reading novels, programmers prefer to maintain the block and command structure in their code. If a command statement such as a compound IF can be kept to one line instead of two, it becomes more readable. If a function call displays all arguments passed in on the same line, it’s easier to read than having them waterfall underneath.

    I guess the three areas of programming I would change (if I could) is the following:

    1. Allman style indenting would be the standard for all languages that use braces.

    2. Row lengths would be increased to take advantage of wide screen monitors and reduce unnecessary line continuations in code.

    3. I would beat programmers over the head with a stick for not putting comments in their code. (Arguably my greatest pet peeve.)

    To date, there has never been a software project that had too much documentation. 99.9% of software projects don’t have enough or don’t have any. Having been put into the “wonderful” position of having to maintain other programmer’s junk on several occasions, I’m the guy that companies hire to tell them how their system actually works (since nobody understands it) and to document it so that future developers have an easy job. I can literally pick up a piece of my code from 10 years ago, look at a few of my comments, know exactly what I need to do, and pick up where I left off without missing a beat. But I digress…

    Reply  |  Quote
  60. Chris B CANADA Mozilla Firefox Windows says:

    That should have said “IF statements with multiple conditions” not “compound IF statements”. ^^^

    Example:

    if(color == “BLUE” || color == “RED” || color == “GREEN)
    {
    …doSomething();
    }

    Example:

    x = object.myFunction(longArgumentName1, longArgumentName2, longArgumentName3);

    Reply  |  Quote
  61. James UNITED STATES Mozilla Firefox Windows says:

    When I first learned C, the professor used the K&R style, but when I wrote my code, it looked like the Allman style. It just seemed more organized to me. It was easier for me to keep track of the braces. My professor would comment about how easy it was to read my code (I assumed it was because I coded so well–maybe it was just the coding style).
    I use the same style whether I’m in C, C++, C#, or Java.

    Reply  |  Quote
  62. Chris B. CANADA Mozilla Firefox Windows says:

    @James:

    I agree. I was taught to use the Allman style in school, and it was the default “template” in Visual Studio for writing code when I was in school. Whenever an editor tries to enforce a K&R style, I change the template right away.

    I’ve noticed that the K&R community has been trying very hard to foist their style on everyone, and I truly believe that over the long run it will lose out to the Allman style.

    A standard is determined by what the majority of people want to use, not what a small group of eggheads in ivory towers think the world should use. That’s why it isn’t surprising that Allman style is the preferred style. Allman style is what people want to use because it’s more natural, easier to read, and less prone to bugs. Those factors make it the clear winner on indenting style and brace location.

    Reply  |  Quote
  63. Danny Google Chrome Windows says:

    @ Chris B:
    I was really excited when a certain software company that discontinued one of their products placed their code on their website. They announced it was open source. The code was in C++; there were two header files and four implementation files. One of the files was over 54,000 lines of code. All the source code was written in a sort of K&R style with no blank spaces between functions, no whitespace in expressions, and not one comment.
    What a nightmare!

    Reply  |  Quote
  64. Tino SWEDEN Google Chrome Linux Terminalist says:

    @ Danny:

    Had you been happier with 75,000 lines of c++ in Allman style instead? It hardly is the K&R style that is the problem of that code. Also, why even complain about the K&R style? Just use one of the myriad of tools to re-indent the files for you. Indentation style is really only an issue when working on collaborative projects.

    Chris B. wrote:

    I’ve noticed that the K&R community has been trying very hard to foist their style on everyone, and I truly believe that over the long run it will lose out to the Allman style.

    This is a complete fabrication of history. The K&R style was the original style from the birth of C. It is these measly Allman followers who have since come along and now try to force their ugly context-breaking whitespaces on everyone. I use K&R so that I can proudly insert empty lines under my control statements precisely when I mean to do so, so that I can group statements by context, not by syntax.

    Reply  |  Quote
  65. Luis Henrique Loss Google Chrome Windows says:

    I used to program, on 1980s, with Algol and Pascal. It used “begin” and “end” instead of { and }, and they was aligned to block code. The first C courses I attended, in 1984 used WhiteSmith, and I get used with it, specially because it used the same identation as it predecessors Algol and Pascal. For me it makes more sense to have the signs (or reserver words), that starts and end the block, alignet to the block – these signs belongs to the block!!! Why put them away of the block? Well, once someone gets used with some style, it is difficult to change – it is a matter of personal visibility and fast code interpretation. For me, Allman is barely “acceptable”, but the others styles that places the braces elsewere… What the hell they creators had in mind?!?!? And a lot of new fool programmers got used with them!!! Fortunately, good editors can convert easily from one style to another. I like to use UltraEdit, from IDM Computer Solutions, Inc.

    Reply  |  Quote
  66. Anon CANADA Mozilla Firefox Linux says:

    i_like(Pico);{
    everyone_else(gasps);}

    Reply  |  Quote
  67. Rhiakath Mozilla Firefox Ubuntu Linux says:

    int main ( int argc, char *argv[] )
    {
    printf ( "Whitesmith all the way\n" );
    printf ( "Allman a second best\n" );
    return 0;
    }

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>