Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the name of this convention for curly braces?

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?

like image 920
Frankie Avatar asked Dec 15 '10 16:12

Frankie


2 Answers

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

like image 168
asthasr Avatar answered Oct 22 '22 16:10

asthasr


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.

like image 35
Jon Skeet Avatar answered Oct 22 '22 16:10

Jon Skeet