What's the proper way to format "if" statements in Java with multiline "ands" or "ors"?

Dasmowenator picture Dasmowenator · Nov 7, 2013 · Viewed 26.4k times · Source

Apparently "if", "and", and "or" are such generic search parameters that I can't find the answer on google for my life. Which of these is the correct format according to the Java standards?

Option 1:

if (condition1
    && condition2
    && condition3) ...

or Option 2:

if (condition1 &&
    condition2 &&
    condition3) ...

Answer

Dawood ibn Kareem picture Dawood ibn Kareem · Nov 7, 2013

The Oracle/Sun guidelines ("Code Conventions for the Java TM Programming Language") tell us to break before an operator. And they give this example.

if ((condition1 && condition2)
        || (condition3 && condition4)
        ||!(condition5 && condition6)) {
    doSomethingAboutIt();
} 

Many companies that I've worked for adopt the Oracle/Sun guidelines as the standard for their own code.

Refer http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-136091.html#248