13

I'm a bit puzzled by the number of developers I see writing methods and classes with curly braces below the class name or the method. What convention are they following?

Sun clearly states that the correct declaration would be:

// this declaration follows sun's conventions
class Sample extends Object {

    Sample(int i, int j) {
        ....
    }
}

Yet more and more I see this declared as (even in books):

// this declaration follows a convention I cant identify
class Sample extends Object 
{

    Sample(int i, int j) 
    {
        ....
    }
}

And I know that a convention is just a convention and, as long as you follow one, all is good. I'm just looking for the answer on what convention does the latter declaration follow?

Jonik
  • 80,077
  • 70
  • 264
  • 372
Frankie
  • 24,627
  • 10
  • 79
  • 121
  • 4
    Shouldn't it now read as "Oracle clearly states..."? :P – FrustratedWithFormsDesigner Dec 15 '10 at 16:38
  • 3
    Despite the question clearly being just about the **convention name**, it's curious how eager people are to bring forth their opinions & preferences as off-topic answers (like [this](http://stackoverflow.com/questions/4452288/java-conventions-for-curly-braces/4452360#4452360) and [this](http://stackoverflow.com/questions/4452288/java-conventions-for-curly-braces/4452413#4452413)) or as votes on those answers. – Jonik Dec 15 '10 at 16:42
  • Rule 0: Don't sweat the small stuff. All of the standard brace positions are equally valid. Use any of them you like. Mix and match. They have no significant impact on readability. They don't merit inclusion in a convention. – Andy Thomas Dec 15 '10 at 16:43
  • 3
    @Andy: I agree about "don't sweat the small stuff", but still wouldn't recommend "mixing and matching". Rather, pick one and stick to it. Even small stuff can affect readability by being a distraction. – Jonik Dec 15 '10 at 16:49
  • @Andy, it is struck on us all (StackOverflow in general), to be more or less curious about some tiny-ultra-micro-spec that doesn't matter a bit to the final outcome but satisfies our hunger for knowledge. Just as it is. Just as was asked. – Frankie Dec 15 '10 at 17:09
  • 1
    Puzzles me why someone would downvote the question without dropping a comment or voting for close. Well... – Frankie Dec 15 '10 at 17:11
  • I learned to place my braces that way from the book I learnt programming from (Java). I'm used to it now – jobukkit Sep 11 '13 at 16:59

7 Answers7

30

This is a standard style convention called the ANSI style. It's common in other languages besides Java, as well.

asthasr
  • 9,125
  • 1
  • 29
  • 43
  • 12
    +1 because you answered the question that was actually asked instead of going off on a Jihad about your preferred style. – JeremyP Dec 15 '10 at 16:51
  • I always reckoned it was just the microsoft style :) – Joeri Hendrickx Dec 16 '10 at 08:18
  • 3
    I would like to add that this is called the "Allman style". It has been incorrectly attributed to the ANSI C documentation. http://en.wikipedia.org/wiki/Indent_style#Allman_style – SirTophamHatt Oct 15 '13 at 20:12
12

As you say, you've seen it quite a lot - doesn't that make it a convention in its own right, whether or not there's a single source or name for that convention? It's so easy to describe, I don't think it needs a particular attribution. Wikipedia's "indent style" page calls it the Allman Style or ANSI style, but frankly I've been using it for years without ever hearing that name, and I don't think knowing the name is going to change anything about the way I code :)

Unlike conventions such as the naming of public types and methods, brace location has no impact on developers using the compiled version of your code... so it's okay (IMO) for different projects and companies to have different takes on this... likewise whether to use spaces or tabs, the number of spaces to use for indentation etc. I suspect that's why you've seen more variation on bracing (and probably private variables) than on other aspects of convention.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 3
    The benefit is to make source control operations like diff and merge easier. When someone goes and reformats entire files simply because they prefer one style over another, it can make merging from the source control tool into a royal pain. – FrustratedWithFormsDesigner Dec 15 '10 at 16:40
  • @FrustratedWithFormsDesigner: That's the benefit of being consistent within a project - but it's *not* a benefit for a cross-project convention. If I use a 3rd party library, I wouldn't start reformatting its code even if I included the code within my own source control system. – Jon Skeet Dec 15 '10 at 16:42
  • @Jon I'm afraid, like several others on this thread, you've hit yourself on a soft-spot reading the question. I was looking for the name of the convention, only. With that knowledge I will eventually dig up other aspects of the convention (or not if there were none). Nevertheless, on broader terms, I subscribe to all of your answer. – Frankie Dec 15 '10 at 17:09
  • @Frankie: I agree, which is why I moved the first paragraph up from the middle to the top. I would suspect that the reason you got the reaction was the tone of the question - which I certainly initially read as *criticising* those developers (particularly due to the "Sun clearly states" etc). It was only when I reread the question that I change the tone of my answer. – Jon Skeet Dec 15 '10 at 17:11
  • @Jon yep. Being a non-native I may have sounded a bit passive-aggressive. Add that to the fact that I'm much cozier with `Allman indent style`. Ah! Doesn't that sound good? Putting words where your thoughts are? Keep it up. I'm cheering for your 250k! ;) – Frankie Dec 15 '10 at 17:16
  • @Frankie: I think it'll be a long time before the human race manages to express emotions accurately and unambiguously in text :) – Jon Skeet Dec 15 '10 at 17:18
  • @JonSkeet **I am thanking Jon Skeet forever that he uses Allman styles in his books.** For me, it's the one and only way to code. My desire for symmetry loves Allman and hates all other styles. – SQL Police May 25 '20 at 16:16
5

See: http://en.wikipedia.org/wiki/Indent_style

Syrion has identified it as the ANSI style, Sun follows the K&R style of brace placement.

Tangent: and now I know that accursed style is called Whitesmiths style

wkl
  • 77,184
  • 16
  • 165
  • 176
5

I prefer the second convention because the closing bracket will be directly below the opening one, so it's easier to find.

124697
  • 22,097
  • 68
  • 188
  • 315
3

The latter looks like the Allman indent style which is quite popular in C++ but I haven't seen it used often in Java.

vitaut
  • 49,672
  • 25
  • 199
  • 336
3

The second convention is also the default indentation style of Visual Studio (and therefore rather common) for C#

Martijn
  • 11,964
  • 12
  • 50
  • 96
1

The first convention saves precious space. The bracket matching is done by the editor anyway.

Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47
  • 3
    Space is not precious in this day and age. Vertical space increases readability. I like to be able to match braces by eye without having to place the cursor or caret over one or the other. – JeremyP Dec 15 '10 at 16:49
  • 2
    I was certainly not referring to disk space. You can see more code on your hi-res screen when using the first convention. – Costis Aivalis Dec 15 '10 at 16:54