Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allman-style anonymous classes

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.
            };
like image 332
gdejohn Avatar asked Dec 21 '10 00:12

gdejohn


2 Answers

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.
    };
like image 86
Zaven Nahapetyan Avatar answered Oct 19 '22 22:10

Zaven Nahapetyan


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.
    });
like image 30
happenstance Avatar answered Oct 19 '22 22:10

happenstance