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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With