Any recommendations on how to use anonymous classes while staying consistent with Allman indent style? I don't really like anything I've come up with, e.g.
// Pass as parameter.
foo(new Clazz( )
{
// Do stuff.
});
// Assign to variable.
Clazz bar = new Clazz( )
{
// Do stuff.
};
The best compromise I came up with for my own code, is indenting the anonymous class a single tabbing level, and putting the closing parentheses on a new line.
// Pass as parameter.
foo(new Clazz( )
{
// Do stuff.
}
);
void func () {
foo(new Clazz( )
{
// Do stuff.
}
);
}
// Assign to variable.
Clazz bar = new Clazz( )
{
// Do stuff.
};
Allman style is really about aligning the {braces}, not all the (brackets). I suppose you are free to do both if you want, but it looks like a source of problems (like this one) to me, without a clear payback in readability. In other words, a logical fetish :-)
You could follow the guide at http://mbreen.com/javastyle.html: "A statement containing a declaration with a code block is indented first as a statement." I think that would change your examples to
foo (new Clazz( )
{
// Do stuff.
});
Clazz bar = (
new Clazz( )
{
// Do stuff.
});
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