开发者

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?


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


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.


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


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


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜