문제

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