问题
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) ...
回答1:
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
回答2:
I believe the correct answer is <CTRL>+<SHIFT>+F
in Eclipse :)
回答3:
This is completely subjective. Coding standards vary from shop to shop. To steal from yshavit's comment: Do whatever you want unless you are working in a team environment, in which case you should follow the team standards.
I'm a fan of the second pattern.
回答4:
This is a preference question. But usually you want to follow the style guidelines of either your company or whoever else is working on the codebase with you. As long as it's consistent, any style is fine.
Personally, I'd keep everything on the same line. (ever dealt with tabs vs. spaces?)
However, if you have enough conditions to fill multiple lines, you should probably break your problem into smaller pieces.
来源:https://stackoverflow.com/questions/19825746/whats-the-proper-way-to-format-if-statements-in-java-with-multiline-ands-or