javaeclipseformatterbraces

Eclipse Java Formatter. New line before curly braces, but not after


we have here at work a very strange coding convention, and I didn't managed to setup the Java Formatter in Eclipse right to do what I want. The convention says:

[UPDATE] There is no rule in our convention saying, if after a "{" should be a line break or not. The examples actually use a line break (and almost ANY convention I saw so far says or implies that after a "{" and a "}" should always be a line break). So sample 1 and 2 are both "syntactically correct". [/UPDATE]

As this blows the code, our team have decided to write code like this (no, this wasn't my choice!):

public void methode(final boolean b)
{ if (b)
  { do.something();
  }
  else
  { do.somethingElse();
  }
}

But in the formatter I only managed to get this:

public void methode(final boolean b)
{
  if (b)
  { 
    do.something();
  }
  else
  { 
    do.somethingElse();
  }
}

or this:

public void methode(final boolean b) { 
  if (b) { 
    do.something();
  }
  else {
    do.somethingElse();
  }
}

Is there a way to tell the formatter to break lines before a "{" but not after that? I know the first style is awful, and I would be pleased to use one of the last two, but this is a company decision.


Solution

  • So, here an Information about this Topic. I have done some more research. The here so abominated Brace-Style (sample 1) has a name: The Horstmann Brace Style or here Horstmann. There is a small group of people defending it, as it combines the advantages from the K&R and the Allman (sample 2) Style. As the braces are lined up, and there is no "waste" of space.

    But this is not the only true. This style miserable for the VCS. If you need to add a Line between the opening brace and the first statement, you need first to break the line, and put your new line there. In the diff or merge you will see then not "one line been added", but "one line been exchanged by two lines". But the actually old statement was changed by you.

    So another argument, not to use this style.