VOOZH about

URL: https://www.javacodegeeks.com/2018/02/java-code-style-operator-new-line-formatting.html

⇱ Java code style operator new line formatting - Java Code Geeks


Last week, I discovered what helped me deciding on what I have struggled with for a longer time: whether to place operators such as &&, || or + on the same or next line.

When constructing boolean expressions or concatinating Strings, we might need to break long lines. According to the Google Java Style Guide, the break should come β€œbefore the symbol”, therefore:

String veryLong = "This is an example of a very, very, very, very, very, very, "
 + "very, very long String";

boolean found = strings.stream()
 .anyMatch(s -> s.startsWith("foo"));

if (someLogic() && someOtherLogic() && someMoreComplexLogic()
 || found)
 // ...

As you can see in this example, the +, || and also . operators are placed on the new line, before the next symbol. The exceptions to the rules are assignment operators, open parenthesis, commas, and lambda arrows.

I think this guide is consistent, makes sense and solves my problem to think about which one to go with over and over again ;-)

Published on Java Code Geeks with permission by Sebastian Daschner, partner at our JCG program. See the original article here: Java code style operator new line formatting

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

πŸ‘ Photo of Sebastian Daschner
Sebastian Daschner
February 21st, 2018Last Updated: February 21st, 2018
0 147 1 minute read

Sebastian Daschner

Sebastian Daschner is a self-employed Java consultant and trainer. He is the author of the book 'Architecting Modern Java EE Applications'. Sebastian is a Java Champion, Oracle Developer Champion and JavaOne Rockstar.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz