Anonymous Allman Classes

Any recommendations for using anonymous classes while keeping the Allman indent style consistent? I do not like what I came up with, for example.

// Pass as parameter. foo(new Clazz( ) { // Do stuff. }); // Assign to variable. Clazz bar = new Clazz( ) { // Do stuff. }; 
+6
java coding-style anonymous-class
source share
3 answers

This is what I decided.

 Foo foo = new Foo() { // Do stuff. }; 

And I just no longer define anonymous classes inside method calls.

0
source share

The best compromise I came up with for my own code separates the anonymous class from one tab level and puts 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. }; 
+2
source share

The Allman style does concern alignment, not all (parentheses). I believe that you are free to do both if you want, but it seems like a source of problems (like this one) for me, without an obvious payback in readability. In other words, a logical fetish :-)

You can follow the guide http://mbreen.com/javastyle.html : "An instruction containing a declaration with a block of code is first indented as an expression." I think this will change your examples to

 foo (new Clazz( ) { // Do stuff. }); Clazz bar = ( new Clazz( ) { // Do stuff. }); 
+1
source share

All Articles