(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:
开发者_JAVA百科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?
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.
If you are employed somewhere, and they enforce a specific convention, then shut up and use it. Otherwise, just do whatever you feel works best for you and don't worry about what everyone else thinks. It is more important to write code than bicker about how to paint bike sheds.
EDIT: I just saw that that page was hideously misformatted, probably due to the Oracle-Sun buyout. COME FROM's comment has the correct Java standard documentation, so if your question was which is the correct documentation then you should use the link he posted. However, I will still stand by my original sentiment that this is something which deeply does not matter.
What i feel, is that the code conventions are for formatting the code, so that if some other guy reads that, there is no such problem in understanding the code,
Untill, you are writing the code in such a way, that any one can understand the code, just by a look into it, then its fine not to use tha Java Code Conventions.
精彩评论