다음을 통해 공유


It's all a matter of style

Probably one of the most religious issues for developers (at least those using the
same platform and programming language) is bracing style.  There are several
styles available, with the two most popular being K&R style & Allman style. 
I found this nice description on the different indent styles here,
and pasted the different indentation styles below:

'K&R style' -- Named after Kernighan & Ritchie, because
the examples
in K&R are formatted this way. Also called `kernel style' because the
Unix kernel is written in it, and the `One True Brace Style' (abbrev.
1TBS) by its partisans. In C code, the body is typically indented by
eight spaces (or one tab) per level, as shown here. Four spaces are
occasionally seen in C, but in C++ and Java four tends to be the rule
rather than the exception.
if () {
'Allman style' -- Named for Eric Allman, a Berkeley hacker who wrote
a
lot of the BSD utilities in it (it is sometimes called `BSD style').
Resembles normal indent style in Pascal and Algol. It is the only style
other than K&R in widespread use among Java programmers. Basic indent per level
shown here is eight spaces, but four (or sometimes three)
spaces are generally preferred by C++ and Java programmers.
if ()
{
'Whitesmiths style' -- popularized by the examples that came with
Whitesmiths C, an early commercial C compiler. Basic indent per level
shown here is eight spaces, but four spaces are occasionally seen.
if ()
{
'GNU style' -- Used throughout GNU EMACS and the Free Software
Foundation code, and just about nowhere else. Indents are always four
spaces per level, with `{' and `' halfway between the outer and inner
indent levels.
if ()
{

My "religious" preference is definitely Allman style, but from what I understand
authors tend to prefer K&R so that they don't lose precious book space when printing
source.  The cool thing is that Whidbey lets you format your code and use whatever
indentation style you prefer (there's 60+ other choices you can make to format your
code).  What style do you use, and are you religious about it?  Say you
found a sample on a web site that used a different bracing style, would you change
the bracing style before running the app? I would, but only if I were planning on
keeping the code...

Comments

  • Anonymous
    November 23, 2003
    Definitely Allman style

  • Anonymous
    November 23, 2003
    I find Allman code easiest to read & to write, but it's no big deal. I care more about tabs. Use an editor that doesn't convert them to spaces, so I can be happy with my 2-space tabs and you can keep your great big ones.

  • Anonymous
    November 23, 2003
    Well the beauty of VS is that when I type a } it makes everything 'right', so most stuff tends to get formatted before running.And of course Allman style is the only way to write ;).

  • Anonymous
    November 24, 2003
    The comment has been removed

  • Anonymous
    November 24, 2003
    Big tabs are another pet peeve of mine, if you code inside a namespace, and then you have a class with a method and that method has nested logic, you're talking about four tabs for code, not very pretty...I definitely agree with Luke that mixing and matching styles is a big no-no...

  • Anonymous
    November 24, 2003
    The comment has been removed

  • Anonymous
    November 24, 2003
    The comment has been removed

  • Anonymous
    November 25, 2003
    Check out the java coding conventions, this is the true way to indent if you want other to have a good time reading your code:http://java.sun.com/docs/codeconv/html/CodeConventions.doc3.html#262

  • Anonymous
    November 25, 2003
    Here is the one about methods:http://java.sun.com/docs/codeconv/html/CodeConventions.doc6.html#430

  • Anonymous
    November 25, 2003
    The comment has been removed

  • Anonymous
    December 01, 2003
    The comment has been removed

  • Anonymous
    December 13, 2003
    /* *There is a lot of things VB dosn't have. */Read the source of Linux which employs the K&R format. The code is easy to read and it is structured nicely. The largest collaborative project in the world uses it, there is no more to say.

  • Anonymous
    February 01, 2004
    The comment has been removed

  • Anonymous
    March 27, 2004
    I like to use the Horstmann style, which has the advantage of space saving like the K&R style but the readability of the Allman style. After the opening brace, press tab and start typing up the code. If I found a piece of code that I would use in my programs that used a different style, I would probably go ahead and change it, just to be consistent.

  • Anonymous
    May 27, 2004
    K&R style is a abomination that should be stricken
    from the face of the planet. :-)

    Allman style is the only way to go, IMNSHO.

  • Anonymous
    June 24, 2004
    The comment has been removed

  • Anonymous
    July 15, 2004
    The comment has been removed

  • Anonymous
    July 22, 2004
    The comment has been removed

  • Anonymous
    July 29, 2004
    The comment has been removed

  • Anonymous
    July 31, 2004
    The comment has been removed

  • Anonymous
    May 17, 2005
    The comment has been removed

  • Anonymous
    July 27, 2005
    The comment has been removed

  • Anonymous
    November 16, 2005
    The comment has been removed

  • Anonymous
    January 06, 2006
    I've had to use all of these styles at one time or another, so I'm pretty flexible. K&R was really useful in the days when most of us programmed on 24x80 terminals. I did find one case where K&R style could prevent a problem. I had a programmer working for me who took the error messages produced by the compiler quite literally. There was a case where the compiler told him that there was a semicolon expected, so he added one:

    if (cond) ;
    {
    code that now always executes
    }

    With K&R style, the code would still work as written. Of course, the real problem was the programmer...

  • Anonymous
    February 03, 2006
    Being in an "Allman" shop has really made me hate the style. Taking up extra space for a { makes reading the code more difficult because it makes detecting the indentation of the code more difficult. It also makes catching and handling exceptions hard to read because a rather run-of-the mill exception handling routine can take up many screens. I used to use Allman style but converted to R&K when I realised that I found K&R easyer to read.

  • Anonymous
    April 12, 2006
    Allman is best. I have nothing more to add than what others have already posted. I have noticed more programmers have converted from K & R to Allman than vice versa. The arguments for Allman's win hands down. End of discussion.

  • Anonymous
    June 08, 2006
    The comment has been removed

  • Anonymous
    November 17, 2006
    Call me stupid... but I am running Visual Studio 2005 express edition, but I can not change the default indentation style to Allman style! Who knows how to do it! Thanks. Of course, I found Tools/Options/Text Editor...

  • Anonymous
    November 18, 2006
    The comment has been removed

  • Anonymous
    January 21, 2007
    The comment has been removed

  • Anonymous
    March 31, 2007
    The comment has been removed

  • Anonymous
    April 30, 2007
    K&R wins hands down for me. I used to be an Allman guy, since that seemed to be what most people were using. I then worked on a project which used K&R and was far more readable as a result. Like the person who commented before, I find it far easier to scan for if...} or while...} etc than {...} This is just plain horrible: try { ... } catch ( ... ) { ... } I also find it near impossible to get an overall picture of the structure of Allman code since so little of it fits on the screen.

  • Anonymous
    May 14, 2007
    The comment has been removed

  • Anonymous
    August 29, 2007
    Anything except K&R.  I simply find it hard to read.  I have some odd SQL coding conventions that I follow, too, all in the name of readability.

  • Anonymous
    October 13, 2007
    The comment has been removed

  • Anonymous
    February 01, 2008
    After I use Python, I enjoy programming in the K&R style.

  • Anonymous
    September 22, 2008
    I need Allman style. If I even use a piece of code that isn't written in Allman style, I will make it Allman style. This is time consuming. I think of anyone who doesn't use Allman style as making more work on me to properly indent their code. ;) As far as how many spaces to indent, I'm not picky, so long as it's consistent throughout it's entire scope, and the matching brackets are equally indented.

  • Anonymous
    March 18, 2009
    Allman style, 2-space tabs. I use 4-space tabs in python since blocks are indicated by tabs instead of braces.

  • Anonymous
    February 10, 2010
    Much prefer the K&R style. I used to be an Allman developer in college until I saw the light. More screen space to see your code is a plus but the main reason I like it is that the controlling statement nicely matches up with the ending brace. Visually it's easier for me to see this than if I had an opening brace right below it. In the place I work now everyone uses Allman and it's a nightmare looking at code that has a few lines in each if / else construct. The code just looks so bloated.

  • Anonymous
    March 06, 2010
    The comment has been removed

  • Anonymous
    June 29, 2010
    I used to favor Allman style, but I switched to K&R when I had to deal with multiple programming languages (VB.NET, C#, Ruby). Although I can agree that Allman style is (at times) more readable, I favor the ability of K&R to allow just about ANY programming language to use the SAME number of lines. Examples: C/C++/C#/Java/PHP/Perl:


1    if (expression1) { 2        statement1; 3        statement2; 4        statement3; 5    } else if (expression2) { 6        statement4; 7        statement5; 8    } else { 9        statement6; 10    } VBScript/VB6/VB.NET:

1    If Expression1 Then 2        Statement1 3        Statement2 4        Statement3 5    ElseIf Expression2 Then 6        Statement4 7        Statement5 8    Else 9        Statement6 10    End If Pascal/Delphi:

1    if expression1 then begin 2        statement1; 3        statement2; 4        statement3 5    end else if expression2 then begin 6        statement4 7        statement5 8    else 9        statement6 10    end; Ruby:

1    if expression1 2        statement1 3        statement2 4        statement3 5    elsif expression2 6        statement4 7        statement5 8    else 9        statement6 10    end COBOL:

1    IF EXPRESSION-1 2        STATEMENT-1 3        STATEMENT-2 4        STATEMENT-3 5    ELSE IF EXPRESSION-2 6        STATEMENT-4 7        STATEMENT-5 8    ELSE 9        STATEMENT-6 10    . Python's indentation rule is an annoyance, because I strongly feel that progressive INdentation must be followed by eventual progressive DEdentation. If something is indented 4 spaces, there must be something present on a line that dedents this block 4 spaces. I might think of using a comment to denote the end of the block Python:

1    if expression1: 2        statement1 3        statement2 4        statement3 5    elif expression2: 6        statement4 7        statement5 8    else: 9        statement6 10    #end if

  • Anonymous
    September 22, 2010
    Whitesmiths Style all the way, it makes my code look cleaner, as well as making the distinction that braces are part of the block of code, NOT the opening statement.