Search code examples
javafor-loopconventionsif-statement

Code convention for if-else, for statements in Java


(code convention doc: http://www.oracle.com/technetwork/java/codeconventions-142311.html#449)

I've always written if-else statements as:

if(condition) {
    statements;
} else {
    statements;
}

however, in the Java code conventions document, it says to write it like this:

if (

condition) {


statements;
} else {


statements;
}

And also, I've always written for statements like this:

for(initialization;condition;update) {
    statements;
}

But the coding convention says:

for (

initialization;

condition;

update) {


statements;
}

The indentation and spacing seems unnecessarily cumbersome to me. Which is the correct/better way and why?


Solution

  • I think that might just be a formatting problem on that HTML page?

    Have a look at the PDF, I think it looks better there:

    http://www.oracle.com/technetwork/java/codeconv-138413.html

    There they have the conditions on the same line as the if.