In the current implementation, the following code repeats almost the same branch logic four times:
Static analyzers report this as a SIMILAR_BRANCHES issue due to the high duplication.
if (this.tryPattern[direction][0].call(this, itemCollision, gridsterItem)) {
this.pushedItemsOrder.push(itemCollision);
pushedItems.push(itemCollision);
}
else if (this.tryPattern[direction][1].call(this, itemCollision, gridsterItem)) {
this.pushedItemsOrder.push(itemCollision);
pushedItems.push(itemCollision);
}
else if (this.tryPattern[direction][2].call(this, itemCollision, gridsterItem)) {
this.pushedItemsOrder.push(itemCollision);
pushedItems.push(itemCollision);
}
else if (this.tryPattern[direction][3].call(this, itemCollision, gridsterItem)) {
this.pushedItemsOrder.push(itemCollision);
pushedItems.push(itemCollision);
}
else {
makePush = false;
break;
}
The same logic can be expressed more concisely with a loop, which improves readability and maintainability:
let matched = false;
for (let patternIndex = 0; patternIndex < 4; patternIndex++) {
if (this.tryPattern[direction][patternIndex].call(this, itemCollision, gridsterItem)) {
this.pushedItemsOrder.push(itemCollision);
pushedItems.push(itemCollision);
matched = true;
break; // first match found, no need to check the rest
}
}
if (!matched) {
makePush = false;
break;
}
In the current implementation, the following code repeats almost the same branch logic four times:
Static analyzers report this as a SIMILAR_BRANCHES issue due to the high duplication.
The same logic can be expressed more concisely with a loop, which improves readability and maintainability: