QR code

Paired Brackets

  • comments

Javajava

Here is a notation rule I’m using in Java code: a bracket should either start/end a line or be paired on the same line.

The notation applies universally to any programming language (incl. Java, Ruby, Python, C++, PHP, etc.) where brackets are used for method/function calls.

Here is how your code will look, if you follow this “Paired Brackets” notation:

new Foo( // ends the line
  Math.max(10, 40), // open/close at the same line
  String.format(
    "hello, %s",
    new Name(
      Arrays.asList(
        "Jeff",
        "Lebowski"
      )
    )
  ) // starts the line
);

Obviously, the line with a closing bracket should start at the same indentation level as the line with its opening pair.

This is how your IDE will render the code if you follow this notation (IntelliJ IDEA):

The figure

Sublime Text will also appreciate it:

The figure

As you see, those light vertical lines at the left side of the code help you to navigate, if you follow the notation.

Those multiple closing brackets may look strange to you at the beginning—but give yourself some time and you will get used to them.

Fluent

This is how I would recommend formatting fluent method calls (this is Java in NetBeans):

The figure

Arrays

Here is how you format an array in “Paired Brackets” notation (this is Ruby in RubyMine):

The figure

As you see, the same principle applies to square and curled brackets.

JSON

The same principle is applicable to JSON formatting. This is a small JSON document in Coda 2:

The figure

JavaScript

JavaScript should also follow the same principle. This is how your .js code would look in Atom:

The figure

Python

Finally, here is Python in PyCharm:

The figure
sixnines availability badge   GitHub stars