Braces

Controls the handling of curly braces (the Java block delimiters).

Layout

Lets you define how the enclosing block delimiters—open and closing curly brace—are printed. You can either choose from a predefined set of common styles or build one on your own. The brace style can either be configured individually for each block construct (details view) or one global style used (global view).

Figure 2.34. Braces Layout settings page (Global view)

Braces Layout settings page (Global view)

Use global style

Defines whether one brace style should be used for all block elements. When disabled, a combo box appears that lets you define the desired brace style for the different block elements individually.

Since 1.6

Figure 2.35. Braces Layout settings page (Details view)

Braces Layout settings page (Details view)

Configure style for

Lets you choose the block construct whose brace style should be configured. Select an item to display the available brace style options for the chosen block construct. This option is only available when the global brace style check box is disabled.

Since 1.6

Choose from common style

Lets you choose a pre-configured brace style. Choosing a style will adjust all individual brace style options accordingly. The available styles are:

  • C style.

    Activates the C brace style. This style is sometimes called "Allman style" or "BSD style" named for Eric Allman, a Berkeley hacker who wrote a lot of the BSD utilities in it. The style puts the brace associated with a control statement on the next line, indented to the same level as the control statement. Statements within the braces are indented to the next level.

    Example 2.37. C style

    private void routine()
    { 
        if (!done)
        {
            doSomething();
        }
        else
        {
            System.out.println("Finished");
        }
    }
    

    Advantages of this style are that the indented code is clearly set apart from the containing statement by lines that are almost completely whitespace, improving readability and the ending brace lines up in the same column as the beginning brace, making it easy to find the matching brace. Additionally, the blocking style delineates the actual block of code associated from the control statement itself. Commenting out the control statement, removing the control statement entirely, refactoring, or removing of the block of code is less apt to introduce syntax errors because of dangling or missing brackets. Proponents of this style often cite its use by ANSI and in other standards as justification for its adoption. The motivation of this style is to promote code readability through visually separating blocks from their control statements, deeming screen real estate a secondary concern.

  • Sun Java style.

    Activates the Sun brace style, a variant of the so called "K&R style" named after Kernighan & Ritchie, because the examples in their famous "The C Programming Language" [Kernighan88] reference are formatted this way. It keeps the first opening brace on the same line as the control statement, indents the statements within the braces, and puts the closing brace on the same indentation level as the control statement (on a line of its own). Most of the standard source code for the Java API is written in this style.

    Example 2.38. Sun style

    private void routine() {
        if (!done) {
            doSomething();
        } else {
            System.out.println("Finished");
        }
    }
    

    Advantages of this style are that the beginning brace does not require an extra line by itself, and the ending brace lines up with the statement it conceptually belongs to. The big drawback here is that for block statements wrapped expressions require extra indentation because otherwise the block body is difficult to gather which violates symmetry.

  • GNU style.

    Activates the GNU brace style. A brace style used throughout GNU EMACS and the Free Software Foundation code. Braces are put on a line by themselves. The braces are indented by 2 spaces, and the contained code is indented by a further 2 spaces.

    Example 2.39. GNU style

    private void routine()
      {
        if (!done)
          {
            doSomething();
          }
        else
          {
            System.out.println("Finished");
          }
      }
    

Synchronize with

Lets you synchronize the brace style of the currently selected block construct with the brace style of another one. Select an item in the list to have the brace style options updated accordingly. This option is only available when the global brace style check box is disabled.

Since 1.6

Line Wrapping

Lets you manually adjust the line wrapping behavior for braces. If one of the common brace styles does not satisfy your needs, you can manually adjust the appearance here.

Line break before left brace

Controls the line break behavior before the left curly brace. When enabled, a line break gets printed before the left curly brace.

Example 2.40. No line break before left brace

if(true){
    doSomething();
}

Example 2.41. Line break before left brace

if(true)
{
    doSomething();
}

Line break after right brace

Controls the line break behavior after the left curly brace. When enabled, a line break gets printed after the left curly brace whenever possible.

Example 2.42. No line break after right brace

try{
    doSomething();
}catch (Exception ex){
}

Example 2.43. Line break after right brace

try{
    doSomething();
}
catch (Exception ex){
}

Treat class/method blocks different

It is common in the Java developer community to have the opening brace at the end of the line of the keyword for all types of blocks (Sun brace style). One may find the C++ convention of treating class/interface and method/constructor blocks different from other blocks useful (K&R style). With this option you can achieve exactly that: if enabled, class/interface and method/constructor blocks are then always printed in C brace style (line break before left brace).

Example 2.44. Sun brace style

class VolkswagenBeetle extends AbstractAutomobile {
    public void tootHorn() {
        if (isNull) {
            throwConstraintViolated();
        } else {
            updateValue();
        }
    }
}

Example 2.45. Sun brace style, but class/method block treat different

class VolkswagenBeetle extends AbstractAutomobile
{
    public void tootHorn()
    {
        if (isNull) {
            throwConstraintViolated();
        } else {
            updateValue();
        }
    }
}

This option is only available when the global brace style check box is enabled.

Treat class/method blocks different if wrapped

Enabling this option forces a line break before the left brace for class/interface or method/constructor blocks (C style), if either the parameter list spawns several lines and a throws clause follows, or one of the possible clauses (extends, implements, throws) was wrapped. This option is only available when the global brace style check box is enabled.

Treat statement blocks different if wrapped

Using the Sun brace style can make it hard to identify the start of the block body. The recommended workaround is to increase indentation for the expression statements during line wrap (see “Block continuation indentation”). But it may be easier to change the style of the block brace for such cases. Enabling this option does exactly that: It forces a line break before the opening brace for block statements (if/for/while etc.), if the statement expression could not be printed in just one line.

Since 1.7

Example 2.46. Wrapped block statement (Sun brace style)

if (((x > 0) && (y > 0)) || ((x < 0) && (y < 0))
    && ((x > y) || (x < -y))) {
    reverseCourse();
}

Example 2.47. Wrapped block statement - left curly brace treated different

if (((x > 0) && (y > 0)) || ((x < 0) && (y < 0))
    && ((x > y) || (x < -y)))
{
    reverseCourse();
}

This option is only available when the global brace style check box is enabled or the style for Statements is currently configured.

Strictly obey brace style

When “Prefer wrap after assignments” and the section called “Line break before left brace” are enabled, a line break may be printed before the left curly brace of array initializers if the initializer does not fit into one line. But one might prefer to leave the curly brace in the same line as the variable declaration to obey the general brace style. With this option you can decide what you favor most: consistent brace style or consistent wrapping after assignments. The option is only available, if line breaks should be printed before left curly braces (see the section called “Line break before left brace”) and either a global brace style (see the section called “Use global style”) used or the brace style for arrays (see the section called “Configure style for”) is configured.

Since 1.8

Example 2.48. Favor consistent brace style

private String[] data = {
        "aaaaaaaaaaaaaaaaassssssssssssssssssssss",
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
        "cccccccccccccccccccccccccccccccccccccccc",
        "dddddddddddddddddddddddddddddddddd",
        "eeeeeeeeeeeeeeeeeeeeeeeeeeee", "ffffffffffffffffffffffffffff"
    };

Example 2.49. Favor wrap after assignment

private String[] data =
    {
        "aaaaaaaaaaaaaaaaassssssssssssssssssssss",
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
        "cccccccccccccccccccccccccccccccccccccccc",
        "dddddddddddddddddddddddddddddddddd",
        "eeeeeeeeeeeeeeeeeeeeeeeeeeee", "ffffffffffffffffffffffffffff"
    };

White Space

Lets you adjust the indentation white space for curly braces individually.

Before left brace

Defines the amount of blank space that should be printed before the left curly brace.

Example 2.50. No white space before left brace

if(true){
    doSomething();
}

Example 2.51. One space before left brace

if(true) {
    doSomething();
}

Before right brace

Defines the amount of blank space that should be printed before the right curly brace.

Example 2.52. No white space before right brace

if(true){
    doSomething();
}else{
    quit()();
}

Example 2.53. One space before right brace

if(true){
    doSomething();
}else {
    quit()();
}

After right brace

Defines the amount of blank space that should be printed after the right curly brace.

Example 2.54. No white space after right brace

if(true){
    doSomething();
}else{
    quit()();
}

Example 2.55. One space after right brace

if(true){
    doSomething();
} else{
    quit()();
}