Google "Where to Break" Style Guide
Java
https://google.github.io/styleguide/javaguide.html#s4.5.1-line-wrapping-where-to-break
- When a line is broken at a non-assignment operator the break comes before the symbol. (Note that this is not the same practice used in Google style for other languages, such as C++ and JavaScript.)
This also applies to the following "operator-like" symbols:
- the dot separator (
.
)- the two colons of a method reference (
::
)- an ampersand in a type bound (
<T extends Foo & Bar>
)- a pipe in a catch block (
catch (FooException | BarException e)
).
- When a line is broken at an assignment operator the break typically comes after the symbol, but either way is acceptable.
- This also applies to the "assignment-operator-like" colon in an enhanced
for
("foreach") statement.
- A method or constructor name stays attached to the open parenthesis (
(
) that follows it.- A comma (
,
) stays attached to the token that precedes it.- A line is never broken adjacent to the arrow in a lambda, except that a break may come immediately after the arrow if the body of the lambda consists of a single unbraced expression. Examples:
MyLambda<String, Long, Object> lambda =
(String label, Long value, Object obj) -> {
...
};
Predicate<String> predicate = str ->
longExpressionInvolving(str);
Kotlin
https://developer.android.com/kotlin/style-guide#where_to_break
The prime directive of line-wrapping is: prefer to break at a higher syntactic level. Also:
- When a line is broken at an operator or infix function name, the break comes after the operator or infix function name.
When a line is broken at the following "operator-like" symbols, the break comes before the symbol:
- The dot separator (
.
,?.
).- The two colons of a member reference (
::
).- A method or constructor name stays attached to the open parenthesis (
(
) that follows it.- A comma (
,
) stays attached to the token that precedes it.- A lambda arrow (
->
) stays attached to the argument list that precedes it.
C++
https://google.github.io/styleguide/cppguide.html#Boolean_Expressions
When you have a boolean expression that is longer than the standard line length, be consistent in how you break up the lines.
if (this_one_thing > this_other_thing &&
a_third_thing == a_fourth_thing &&
yet_another && last_one) {
...
}
Note that when the code wraps in this example, both of the && logical AND operators are at the end of the line. This is more common in Google code, though wrapping all operators at the beginning of the line is also allowed. Feel free to insert extra parentheses judiciously because they can be very helpful in increasing readability when used appropriately, but be careful about overuse. Also note that you should always use the punctuation operators, such as && and ~, rather than the word operators, such asand
andcompl
.