質問

I have seen in many tutorials , some programmers uses curly braces in same line with code , others use curly braces in separate line , and rest uses mixed approach .

So my question is , Should curly braces be on their own line or not?

if (you.hasAnswer()) {
    you.postAnswer();
} else {
    you.doSomething();
}

or should it be

if (you.hasAnswer())
{
    you.postAnswer();
}
else
{
    you.doSomething();
}

or even

if (you.hasAnswer()){
    you.postAnswer();
}
else{
    you.doSomething();
}

In Oracle official site about code convention , they given code like this

if (condition) {
    statements;
}

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

if (condition) {
    statements;
} else if (condition) {
    statements;
} else {
    statements;
}
役に立ちましたか?

解決

First or third. I favour the first for compactness mostly, but in "if else" trees sometimes use the third for 'syntactical simplicity' of commenting lines in/out.

Vertical space is valuable, since research finds the comprehension limit of an algorithm is closely related to the vertical size that can be seen on one screen. Wasting vertical space with extra lines for unimportant if-branches is pointless.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top