JavaScript coding style: brace after multi-line condition

In my company projects, the curly braces in JavaScript are always on the same line after the if condition. Given the multi-line condition in the following example, do you consider the indentation of the string "list [i] .Position ++;" looks okay? It seems a little strange to me, because visually it has the same indentation as the condition lines.

for (var i = 0; i < list.length; i++) {
    if (list[i].Position < previousPosition &&
        list[i].Position >= savedPosition) {
        list[i].Position++;
    }
}

Would you do something with this code regarding the coding style?

+3
source share
2 answers

Yes, I would do something with this. I would not say that the problem is with braces. Multi-line conditions are hard to read, I would recommend pulling this out to a separate function.

function isRightPosition(position, previous, saved) {
    return position < previous
         && position >= saved;
}

for (var i = 0; i < list.length; i++) {
    if(isRightPosition(list[i].Position, previous, saved))
        list[i].Position++;
}

, , .

for (var i = 0; i < list.length; i++) {
    if (list[i].Position < previousPosition &&
        list[i].Position >= savedPosition) {
            list[i].Position++;
    }
}

, :

for (var i = 0; i < list.length; i++) {
    if (list[i].Position < previousPosition
        && list[i].Position >= savedPosition) {
            list[i].Position++;
    }
}
, . , ( ).
+1

, list.length , .

for (var i = 0,L=list.length; i < L; i++)
+1

All Articles