此函数的圈复杂度为 11,大于 10 授权
The Cyclomatic Complexity of this function is 11 which is greater than 10 authorized
if ((['ALL', ''].includes(this.accountnumber.value) ? true : ele.accountnumber === this.accountnumber.value) &&
(['ALL', ''].includes(this.description.value) ? true : ele.description === this.description.value) &&
(['ALL', ''].includes(this.country.value) ? true : ele.country === this.country.value) &&
(!this.entryDate ? true : (this.entryDate === dateEntry)) &&
(!this.editedDate ? true : (this.editedDate === dateEdited))) {
return true;
}
有时最好编写更多代码以便更好地理解正在发生的事情。
第一个快速更改是扩展您的 if
内容,例如:
请注意,三进制不是强制性的。一个简单的 ||
运算符将具有相同的效果。
PS:因为我没有考虑过你为什么做你正在做的事情,所以我使用了错误的变量命名。如果您重复使用以下内容,请更改它们并添加有关您要实现的目标的评论。
const isA = [
'ALL',
'',
].includes(this.accountnumber.value) || ele.accountnumber === this.accountnumber.value;
const isB = [
'ALL',
'',
].includes(this.description.value) || ele.description === this.description.value;
const isC = [
'ALL',
'',
].includes(this.country.value) || ele.country === this.country.value;
const isD = !this.entryDate || this.entryDate === dateEntry;
const isE = !this.editedDate || this.editedDate === dateEdited;
if (isA && isB && isC && isD && isE) {
return true;
}
扩展还使某些代码重复变得更加明显。
function checkCond(key, obj, arr = [
'ALL',
'',
]) {
return arr.includes(this[key].value) || obj[key] === this[key].value;
}
const isA = checkCond('accountnumber', ele);
const isB = checkCond('description', ele);
const isB = checkCond('country', ele);
const isD = !this.entryDate || this.entryDate === dateEntry;
const isE = !this.editedDate || this.editedDate === dateEdited;
if (isA && isB && isC && isD && isE) {
return true;
}
更进一步:
function checkCond(key, obj, arr = [
'ALL',
'',
]) {
return arr.includes(this[key].value) || obj[key] === this[key].value;
}
const conditions = [
checkCond('accountnumber', ele),
checkCond('description', ele),
checkCond('country', ele),
!this.entryDate || this.entryDate === dateEntry,
!this.editedDate || this.editedDate === dateEdited,
];
if (conditions.every(x => x)) {
return true;
}
if ((['ALL', ''].includes(this.accountnumber.value) ? true : ele.accountnumber === this.accountnumber.value) &&
(['ALL', ''].includes(this.description.value) ? true : ele.description === this.description.value) &&
(['ALL', ''].includes(this.country.value) ? true : ele.country === this.country.value) &&
(!this.entryDate ? true : (this.entryDate === dateEntry)) &&
(!this.editedDate ? true : (this.editedDate === dateEdited))) {
return true;
}
有时最好编写更多代码以便更好地理解正在发生的事情。
第一个快速更改是扩展您的 if
内容,例如:
请注意,三进制不是强制性的。一个简单的 ||
运算符将具有相同的效果。
PS:因为我没有考虑过你为什么做你正在做的事情,所以我使用了错误的变量命名。如果您重复使用以下内容,请更改它们并添加有关您要实现的目标的评论。
const isA = [
'ALL',
'',
].includes(this.accountnumber.value) || ele.accountnumber === this.accountnumber.value;
const isB = [
'ALL',
'',
].includes(this.description.value) || ele.description === this.description.value;
const isC = [
'ALL',
'',
].includes(this.country.value) || ele.country === this.country.value;
const isD = !this.entryDate || this.entryDate === dateEntry;
const isE = !this.editedDate || this.editedDate === dateEdited;
if (isA && isB && isC && isD && isE) {
return true;
}
扩展还使某些代码重复变得更加明显。
function checkCond(key, obj, arr = [
'ALL',
'',
]) {
return arr.includes(this[key].value) || obj[key] === this[key].value;
}
const isA = checkCond('accountnumber', ele);
const isB = checkCond('description', ele);
const isB = checkCond('country', ele);
const isD = !this.entryDate || this.entryDate === dateEntry;
const isE = !this.editedDate || this.editedDate === dateEdited;
if (isA && isB && isC && isD && isE) {
return true;
}
更进一步:
function checkCond(key, obj, arr = [
'ALL',
'',
]) {
return arr.includes(this[key].value) || obj[key] === this[key].value;
}
const conditions = [
checkCond('accountnumber', ele),
checkCond('description', ele),
checkCond('country', ele),
!this.entryDate || this.entryDate === dateEntry,
!this.editedDate || this.editedDate === dateEdited,
];
if (conditions.every(x => x)) {
return true;
}