Jindent - Java Source Code Formatter http://www.jindent.com
 



title
5.6.2.3 Braces Style

Brace Styles are separated in five different main sections.
This way it is possible to set different styles for:
Each of these blocks contains eleven sub control settings and predifined presets.

From there it is possible to choose different brace styles for different source code elements. For instance, try-catch statements can be formatted different than methods.



title
5.6.2.3.1 Class/Interface/Annotation Type

Brace style for class/struct/union type declarations

Brace style for class/struct/union type declarations

Left brace { new line


Controls whether the left brace of a class/struct/union type declaration appears on a new line or not.


Setting left brace to a new line:

class MyClass : public MyOtherClass     
{                                       
    ...                                 


Do not set left brace to a new line:

class MyClass : public MyOtherClass {   
    ...                                 



Right brace } new line


Since right braces of class/struct/union type declarations always appear in a new line this setting has no effect in this case.



Indent left brace {


Specifies the indentation size of left braces.


Setting left brace to a new line and using an indentation size of 4:

class·MyClass·:·public·MyOtherClass     
····{                                   
····...                                 


Setting left brace to a new line and using an indentation size of 0:

class·MyClass·:·public·MyOtherClass     
{                                       
    ...                                 


Do not set left brace to a new line and use indentation size of 1:

class·MyClass·:·public·MyOtherClass·{   
    ...                                 


Do not set left brace to a new line and use indentation size of 0:

class·MyClass·:·public·MyOtherClass{    
    ...                                 



Indent right brace }


Specifies the indentation size of right braces.


Left brace is on a new line and set indentation size of 4 for left and right braces:

class·MyClass·:·public·MyOtherClass     
····{                                   
····...                                 
····}                                   


Left brace is on a new line and set indentation size of 0 for left and right braces:

class·MyClass·:·public·MyOtherClass     
{                                       
····...                                 
}                                       



Indent after right brace }


Since no tokens (except comments) can appear directly after right braces of class/struct/union type declarations this setting has no effect in this case.



Cuddle braces of empty blocks {}


Specifies how to format braces of empty class/struct/union type blocks.


Cuddle braces of empty blocks:

class MyClass : public MyOtherClass {}  


Do not cuddle braces of empty blocks:

class MyClass : public MyOtherClass {   
}                                       



Indent cuddled braces {}


Controls the indentation size of cuddled braces.


Cuddle braces of empty blocks and use an indentation of 0:

class·MyClass·:·public·MyOtherClass{}   


Cuddle braces of empty blocks and use an indentation of 1:

class·MyClass·:·public·MyOtherClass·{}  



Prohibit blank lines after left brace {


Prohibits blank lines directly after a left brace of class/struct/union type blocks.

Some code conventions require a blank line before each comment.
Such a formatting looks proper if the comment, for instance, appears between two fields.
But if a comment follows directly after a left brace of class/struct/union type blocks an unecessary gap can appear.


Left brace of class block is set to a new line using an indentation size of 0.
Additionally a blank line shall be inserted before comments:

class MyClass : public MyOtherClass     
{                                       
                                       
    
// comment with a preceding blank line
    
int field;                          
                                       
    
// another comment                  
    
...                                 
}                                       


In the example above the left brace and blank line before the comment seem to create an unnecessary gap.
To avoid such a gap just prohibit blank lines after left braces:

class MyClass                           
{                                       
    
// comment with a preceding blank line
    
int field;                          
                                       
    
// another comment                  
    
...                                 
}                                       

Now the blank line before the first comment disappeared and closed the gap, but all other comments still contain the expected blank line as usual.
Of course this works for all kind of blank lines and not only for blank lines before comments.

See also... See also: Jindent - Settings - Formatter - C / C++ - Blank Lines - Comments , Jindent - Settings - Formatter - C / C++ - Blank Lines




If number of lines in body is at least ... then insert blank line after {


Inserts an extra blank line after left braces of class/struct/union types containing a certain number of lines in their bodies.

Code conventions which prefer the K&R brace style (also known as Kernal, Cpp or Sun Style: do NOT put left brace to a new line) can run into a specific issue regarding readability of source code.


Let's assume the following source code example is formatted in a typical K&R brace style:

class MyClass : public MyOtherClass {   
    
String getString() {                
        callMethodA();                  
        callMethodB();                  
        
return callMethodC();           
    }                                   
}                                       
                                       
struct MyStruct {                       
    
int x;                              
}                                       

In this case formatting of struct MyStruct looks fine, but class MyClass : public MyOtherClass seems to be formatted too close: The class head class MyClass : public MyOtherClass { and the first method declaration String getString() { are connected too close and could cause confusion.
To avoid a too close formatting for specific class/struct/union types use setting "If number of lines in body is at least ... then insert blank line after {" to insert an extra blank line before the first class element.


The same example as above, but now using setting "If number of lines in body is at least ... then insert blank line after {" with a value of 2.


class MyClass : public MyOtherClass {   
                                       
    
String getString() {                
        callMethodA();                  
        callMethodB();                  
        
return callMethodC();           
    }                                   
}                                       
                                       
struct MyStruct {                       
    
int x;                              
}                                       

Now formatting of class MyClass seems to be much clearer and struct MyStruct is still formatted as before.
This setting counts the lines of code in every class/struct/union type body and if at least 2 lines are counted then an extra blank line is inserted after the left brace of class/struct/union type head.
With this setting small classes, structs or union types can be kept compact, but other declarations with an extended body are formatted more spacious and clearer.


Using value 0:

class MyClass : public MyOtherClass {   
                                       
    
String getString() {                
        callMethodA();                  
        callMethodB();                  
        
return callMethodC();           
    }                                   
}                                       
                                       
struct MyStruct {                       
                                       
    
int x;                              
}                                       

And all classes/structs/union types are formatted more spacious.

To turn this setting completely off just choose value infinite.



If number of lines in body is at least ... then insert blank line before }


Inserts an extra blank line before right braces of class/struct/union types containing a certain number of lines in their bodies.

This setting works exactly in the same way as "If number of lines in body is at least ... then insert blank line after {", but now the extra blank line will be inserted at the end of class/struct/union type bodies.


Let's assume "If number of lines in body is at least ... then insert blank line after {" is set to 2 and "If number of lines in body is at least ... then insert blank line before }" is set to 2 too:

class MyClass : public MyOtherClass {   
                                       
    
String getString() {                
        callMethodA();                  
        callMethodB();                  
        
return callMethodC();           
                                       
}                                       
                                       
struct MyStruct {                       
    
int x;                              
}                                       

This additional setting creates an even more spacious formatting style.



Do not insert blank line before single left brace


Since right braces of class/struct/union type declarations always appear in a single way (in a new line) this setting should be unchecked to avoid confusion.



See also... See also: Jindent - Settings - Formatter - C / C++ - Braces Style - Presets