Question

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:

  • Before a curly brace "{" there should always be a new Line

[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.

Was it helpful?

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 Horstman Brace Style or here Horstman. 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.

OTHER TIPS

Could you turn off the relevant parts of the code formatter and make use of the Templates instead. For example when typing

private_

and hitting ctrl+space would invoke the private_method template. You could then modify the private template to be something like this -

private ${return_type} ${name}(${}) 
{ ${cursor}
}

You'd have to do similar things to the other block statements and you'd have to modify your coding style to start using the templates more often, but I reckon it could work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top