为了提高代码性能...ESLint 正在报告 "Arrow function has a complexity of 21. Maximum allowed is 20"
in the sake of increasing code performance... ESLint is reporting "Arrow function has a complexity of 21. Maximum allowed is 20"
ESLint 报告了无法忍受的高复杂性。我想知道为什么它太复杂了?如果我将它拆分成多个函数会发生什么——性能好吗?据我所知,我们总是必须编写精简代码,因此如果我们将其拆分为多个函数,它将消耗更多 space(位)和更多执行时间?
处理此代码段的最佳做法是什么。
const getWeekType = (f, d, e) => {
const y = moment(),
a = moment(f),
i = moment(d);
if (d && f && i.diff(a, 'days') <= 8 && y.diff(a, 'days') < 8 && y.diff(a, 'days') >= 0) {
return { weekNum: 0, dayNum: y.diff(a, 'days') };
}
if (f && y.diff(a, 'days') >= 0 && y.diff(a, 'days') < 8 && (!d || i.diff(a, 'days') > 8)) {
return { weekNum: 1, dayNum: y.diff(a, 'days') };
}
if (d && !f && i.diff(y, 'days') >= 0 && i.diff(y, 'days') < 8) {
return { weekNum: 2, dayNum: 6 - i.diff(y, 'days') };
}
if ((!f || y.diff(a, 'days') > 8) && (!d || i.diff(y, 'days') > 8)) {
let d = y.diff(f ? a : moment(e), 'days');
for (; d > 7; ) d -= 7;
return { weekNum: 3, dayNum: d };
}
};
两个问题:
- 变量名没有意义
- 多次调用
diff
我真的不知道代码应该做什么,但这是它可能的草图:
const getWeekType = (start, end, alternative) => {
const now = moment(),
startMoment = moment(start),
endMoment = moment(end),
days = endMoment.diff(startMoment, 'days'),
daysPast = now.diff(startMoment, 'days'),
daysFuture = endMoment.diff(now, 'days');
if (end && start && days <= 8 && daysPast >= 0 && daysPast < 8) {
return { weekNum: 0, dayNum: daysPast };
}
if (start && daysPast >= 0 && daysPast < 8 && (!end || days > 8)) {
return { weekNum: 1, dayNum: daysPast };
}
if (end && !start && daysFuture >= 0 && daysFuture < 8) {
return { weekNum: 2, dayNum: 6 - daysFuture };
}
if ((!start || daysPast > 8) && (!end || daysFuture > 8)) {
let dayNum = start ? daysPast : now.diff(moment(alternative), 'days');
return { weekNum: 3, dayNum: dayNum % 7 };
}
};
ESLint 报告了无法忍受的高复杂性。我想知道为什么它太复杂了?如果我将它拆分成多个函数会发生什么——性能好吗?据我所知,我们总是必须编写精简代码,因此如果我们将其拆分为多个函数,它将消耗更多 space(位)和更多执行时间?
处理此代码段的最佳做法是什么。
const getWeekType = (f, d, e) => {
const y = moment(),
a = moment(f),
i = moment(d);
if (d && f && i.diff(a, 'days') <= 8 && y.diff(a, 'days') < 8 && y.diff(a, 'days') >= 0) {
return { weekNum: 0, dayNum: y.diff(a, 'days') };
}
if (f && y.diff(a, 'days') >= 0 && y.diff(a, 'days') < 8 && (!d || i.diff(a, 'days') > 8)) {
return { weekNum: 1, dayNum: y.diff(a, 'days') };
}
if (d && !f && i.diff(y, 'days') >= 0 && i.diff(y, 'days') < 8) {
return { weekNum: 2, dayNum: 6 - i.diff(y, 'days') };
}
if ((!f || y.diff(a, 'days') > 8) && (!d || i.diff(y, 'days') > 8)) {
let d = y.diff(f ? a : moment(e), 'days');
for (; d > 7; ) d -= 7;
return { weekNum: 3, dayNum: d };
}
};
两个问题:
- 变量名没有意义
- 多次调用
diff
我真的不知道代码应该做什么,但这是它可能的草图:
const getWeekType = (start, end, alternative) => {
const now = moment(),
startMoment = moment(start),
endMoment = moment(end),
days = endMoment.diff(startMoment, 'days'),
daysPast = now.diff(startMoment, 'days'),
daysFuture = endMoment.diff(now, 'days');
if (end && start && days <= 8 && daysPast >= 0 && daysPast < 8) {
return { weekNum: 0, dayNum: daysPast };
}
if (start && daysPast >= 0 && daysPast < 8 && (!end || days > 8)) {
return { weekNum: 1, dayNum: daysPast };
}
if (end && !start && daysFuture >= 0 && daysFuture < 8) {
return { weekNum: 2, dayNum: 6 - daysFuture };
}
if ((!start || daysPast > 8) && (!end || daysFuture > 8)) {
let dayNum = start ? daysPast : now.diff(moment(alternative), 'days');
return { weekNum: 3, dayNum: dayNum % 7 };
}
};