Leonid Leonid - 1 year ago 48
Java Question

Formatting multiple arguments passed to a function in Java

Often the number of arguments passed to a function can be large. Consider the following case:

long milliseconds,
int somethingelse)

Is there a guideline in
that offers a way to align the arguments? Fitting all arguments in a line would not look pretty.

Answer Source

According to the Sun's Java coding conventions, paragraph 4.1 "Wrapping Lines":

When an expression will not fit on a single line, break it according to these general principles:

  • Break after a comma.
  • Break before an operator.
  • Prefer higher-level breaks to lower-level breaks.
  • Align the new line with the beginning of the expression at the same level on the previous line.
  • If the above rules lead to confusing code or to code that’s squished up against the right margin, just indent 8 spaces instead.

The document also includes some examples for method calls:

function(longExpression1, longExpression2, longExpression3,
         longExpression4, longExpression5);

var = function1(longExpression1,