Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 333
thatbennyguy Avatar asked Jun 13 '26 13:06

thatbennyguy


1 Answers

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:

https://www.oracle.com/java/technologies/javase/codeconventions-contents.html

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

like image 168
Daniel Lundmark Avatar answered Jun 15 '26 03:06

Daniel Lundmark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!