Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

While Sun's Java code convention suggests to put line break before the operator many other guidelines disagree with it. I do not see any obvious pros and cons, so are there advantages of using one of these styles over another?

String longVarName = a + b + c + d +
          e + f;


String longVarName = a + b + c + d
          + e + f;
share|improve this question
Can you please post a simple code example showing both conventions? –  Michael Jul 17 '11 at 14:06
First I would try to avoid the situation by using something like this: download.oracle.com/javase/1.4.2/docs/api/java/lang/… –  Job Jul 17 '11 at 15:13
add comment

6 Answers

up vote 5 down vote accepted

This could sound silly, but:

It's expression, leave everything on one line - and as soon as it will become messy start with refactoring:

  • renaming vars
  • introducing new vars/functions

Code formatting is important, however proper and meaningful architecture is much more powerful.

subtotal = price * (1+tax_ratio) / 100 vs. subtotal = price + tax

share|improve this answer
add comment

I can imagine readability being an argument

result = longidentifier +
   short -
   alittlelonger -


result = longidentifier
   + short
   - alittlelonger
   - c;

In the second example the operators are nicely lined up and you can easily see with which sign the variable enters into the equation. I think this also makes sense for binary operators, but with bracing etc., you should just do whatever is clearer.

share|improve this answer
For situations where the operators are important (like math expressions and such) I would choose number two, because, as you said, it is much more readable. But for strings I would choose the first options, as the operators are "meaningless". They don't do anything other than bringing strings together, and because the strings are the important bit, then I would prefer the first options. –  Niklas H Jul 17 '11 at 14:50
Both cases have merit. Both cases are better than putting it all on one very long line! My preference is to to use an opening bracket at the start (even though its not needed) and then having everything line up under that. It makes it far more obvious. –  quickly_now Jul 18 '11 at 9:00
add comment

In code I tend to put the break after the operator:

foo = some_long_expression() +

Here that dangling operator at the end of a line is a big clue to the reader that the code continues. In languages that don't have statement terminators, that dangling operator can serve as a sufficient clue to the compiler/interpreter that the code continues (otherwise I would have to use some ugly continuation line construct).

When documenting that expression (if it needs documentation), I tend to put the break before the operator.

share|improve this answer
At least some languages (e.g. Python) don't take a trailing binary operator as hint that the line continues but require more. Note that newlines inside parens usually aren't counted , so you don't need an explicit (and error-prone) line continuation character. –  delnan Jul 17 '11 at 15:22
add comment

As long as you remain consistent, then know there's no real advantage either way. This is especially important when considering code merges and white space.

share|improve this answer
add comment

For long arithmetic equations, I typically do one of two things.

leave everything on a single line:

foo = bar + baz - fizz + buzz + alpha - beta;

I typically do this for equations containing only addition and subtraction, i find it very easy to make a typo with multiplication and division that can seriously mess up the scope of the operator.

the second format I use is progressive operators:

foo = bar;
foo += baz;
foo -= fizz;
foo += buzz;
foo /= alpha - beta;
foo *= spiff;

I see no reason to shorten it to a single line, unless it can be proven to improve performance in a noticeable manner. Additionally, there's no ambiguity of what's going on where, and there's less of a chance to misplace a parenthesis for the / and * operators.

share|improve this answer
add comment

[A very similar question][1] was asked a few months ago.

Placing the concatenation character (or any operator) at the beginning of the line improves readability. We scan code by focusing on the beginning of each line. When a line starts with an operator, the reader can tell that the line is a continuation of the previous statement by scanning that one character.

Long mathematical expressions are always typeset so that each new line begins with an operator. There is no reason that code should not follow this convention.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.