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



title
5.6.1.3.3 Lambda

Brace style for lambda blocks

Brace style for lambda blocks

Left brace { new line


Controls whether the left brace of a lambda expressions appears on a new line or not.


Setting left brace to a new line:

Arrays.stream(x)                        
      .map((n) -> n * 
5)                
      .filter((n) ->                    
              {                         
                 
System.out.println("Filtering: " + n);
                 
return n % != 0;     
              })                        
      .reduce(
0Integer::sum);         


Do not set left brace to a new line:

Arrays.stream(x)                        
      .map((n) -> n * 
5)                
      .filter((n) -> {                  
                 
System.out.println("Filtering: " + n);
                 
return n % != 0;     
              })                        
      .reduce(
0Integer::sum);         



Right brace } new line


Since right braces of lambda expressions 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 3:

Arrays.stream(x)                        
······.map((n)·->·n·*·5)                
······.filter((n)·->·                   
    ·············
{                      
·················System.out.println("Filtering:·"·+·n);
·················return·n·%·3·!=·0;     
··············})                        
······.reduce(0,·Integer::sum);         


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

Arrays.stream(x)                        
······.map((n)·->·n·*·5)                
······.filter((n)·->·                   
    ··········
{                         
·················System.out.println("Filtering:·"·+·n);
·················return·n·%·3·!=·0;     
··············})                        
······.reduce(0,·Integer::sum);         


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

Arrays.stream(x)                        
······.map((n)·->·n·*·5)                
······.filter((n)·->·{                  
·················System.out.println("Filtering:·"·+·n);
·················return·n·%·3·!=·0;     
··············})                        
······.reduce(0,·Integer::sum);         


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

Arrays.stream(x)                        
······.map((n)·->·n·*·5)                
······.filter((n)·->{                   
·················System.out.println("Filtering:·"·+·n);
·················return·n·%·3·!=·0;     
··············})                        
······.reduce(0,·Integer::sum);         



Indent right brace }


Specifies the indentation size of right braces.


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

Arrays.stream(x)                        
······.map((n)·->·n·*·5)                
······.filter((n)·->·                   
    ·············
{                      
·················System.out.println("Filtering:·"·+·n);
·················return·n·%·3·!=·0;     
·················})                     
······.reduce(0,·Integer::sum);         


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

Arrays.stream(x)                        
······.map((n)·->·n·*·5)                
······.filter((n)·->·                   
    ··········
{                         
·················System.out.println("Filtering:·"·+·n);
·················return·n·%·3·!=·0;     
··············})                        
······.reduce(0,·Integer::sum);         



Indent after right brace }


Since no tokens (except comments) can appear directly after right braces of lambda expressions this setting has no effect in this case.



Cuddle braces of empty blocks {}


Specifies how to format braces of empty lambda expression blocks.


Cuddle braces of empty blocks:

Arrays.stream(x)                        
      .map((n) -> n * 
5)                
      .filter((n) -> {})                
      .reduce(
0Integer::sum);         


Do not cuddle braces of empty blocks:

Arrays.stream(x)                        
      .map((n) -> n * 
5)                
      .filter((n) ->                    
              {                         
              })                        
      .reduce(
0Integer::sum);         



Indent cuddled braces {}


Controls the indentation size of cuddled braces.


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

Arrays.stream(x)                        
······.map((n)·->·n·*·5)                
······.filter((n)·->{})                 
······.reduce(0,·Integer::sum);         


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

Arrays.stream(x)                        
······.map((n)·->·n·*·5)                
······.filter((n)·->·{})                
······.reduce(0,·Integer::sum);         



Prohibit blank lines after left brace {


Prohibits blank lines directly after a left brace of lambda expression blocks.

Some code conventions require a blank line before each comment.
Such a formatting looks proper if the comment, for instance, appears between two statements.
But if a comment follows directly after a left brace of lambda expression blocks an unnecessary gap can appear.


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

Arrays.stream(x)                        
      .map((n) -> n * 
5)                
      .filter((n) ->                    
              {                         
                                       
                  
// comment with a preceding blank line
                  
callAnotherMethod();  
                                       
                  
// another comment    
                  
...                   
              })                        
      .reduce(
0Integer::sum);         


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:

Arrays.stream(x)                        
      .map((n) -> n * 
5)                
      .filter((n) ->                    
              {                         
                  
// comment with a preceding blank line
                  
callAnotherMethod();  
                                       
                  
// another comment    
                  
...                   
              })                        
      .reduce(
0Integer::sum);         

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 - Java / SQLJ - 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 lambda expression blocks containing a certain number of lines in their bodies.

Code conventions which prefer the K&R brace style (also known as Kernal, Java 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:

Arrays.stream(x)                        
      .map((n) -> n * 
5)                
      .filter((n) -> {                  
                  callMethodA();        
                  callMethodB();        
                  
return callMethodC(); 
              })                        
      .reduce(
0Integer::sum);         

In this case formatting lambda expression block seems to be formatted too close: The lambda expression declarations (n) -> { and the first method call callMethodA(); are connected too close and could cause confusion.
To avoid a too close formatting for lambda expressions 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 statement.


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.


Arrays.stream(x)                        
      .map((n) -> n * 
5)                
      .filter((n) -> {                  
                                       
                  callMethodA();        
                  callMethodB();        
                  
return callMethodC(); 
              })                        
      .reduce(
0Integer::sum);         

Now formatting of the lambda expression seems to be much clearer.
This setting counts the lines of code in every lambda expression block and if at least 2 lines are counted then an extra blank line is inserted after the left brace of lambda expression blocks.
With this setting small lambda expressions can be kept compact, but bigger expressions are formatted more spacious and clearer.


Using value 0:

Arrays.stream(x)                        
      .map((n) -> n * 
5)                
      .filter((n) -> {                  
                                       
                  callMethodA();        
                  callMethodB();        
                  
return callMethodC(); 
              })                        
      .reduce(
0Integer::sum);         

And all lambda expressions 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 lambda expression blocks 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 lambda expression blocks.


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:

Arrays.stream(x)                        
      .map((n) -> n * 
5)                
      .filter((n) -> {                  
                                       
                  callMethodA();        
                  callMethodB();        
                  
return callMethodC(); 
                                       
              })                        
      .reduce(
0Integer::sum);         

This additional setting creates an even more spacious formatting style.



Do not insert blank line before single left brace


Since right braces of lambda expressions 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 - Java / SQLJ - Braces Style - Presets