Pergunta

What's the Java style for formatting a long throws list?

Let's say I have this:

 public void some() throws IOException, ClassNotFoundException, NoSuchMethodException,InvocationTargetException, IllegalAccessException {
  }

Should it be:

    public void some() 
        throws IOException, 
               ClassNotFoundException,
               NoSuchMethodException,
               InvocationTargetException,
               IllegalAccessException {
  }

,

    public void some() throws IOException,ClassNotFoundException,
               NoSuchMethodException,InvocationTargetException,
               IllegalAccessException {
  }

Or something else?

Foi útil?

Solução

The old java code conventions would argue against the first one, as it extends past 80 characters.

Avoid lines longer than 80 characters, since they're not handled well by many terminals and tools.

The Java code convention does not distinguish between long throws and other reasons for long method signatures. I would suggest that the second and third are equivalent because they both:

  • Break after a comma.
  • Align the new line with the beginning of the expression at the same level on the previous line.

I would vote for the former, as it is easier to read. To be honest, I'd hate to come across that method.

The real rule of thumb, of course, is to follow the code convention of the team you are working in. If they all use editors that can support 81+ character lines and prefer non-wrapped lines to save vertical real estate, that should be formatting style you use.

Outras dicas

I would prefer the second version, it is clear and readable. One item per line.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top