ESLint reports an intolerably high plexity. I want to know why is it too plex? And what happens if I just split it into multiple functions - is that performant? As I know we always have to write minified code so if we split it into multiple functions it will consume more space (bits) and more execution time?
And what is the best practice to deal with this snippet.
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 };
}
};
ESLint reports an intolerably high plexity. I want to know why is it too plex? And what happens if I just split it into multiple functions - is that performant? As I know we always have to write minified code so if we split it into multiple functions it will consume more space (bits) and more execution time?
And what is the best practice to deal with this snippet.
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 };
}
};
Share
Improve this question
edited Mar 29, 2021 at 17:12
nicholaswmin
23.1k16 gold badges101 silver badges173 bronze badges
asked Mar 29, 2021 at 17:05
MansouriAlaMansouriAla
3451 gold badge3 silver badges17 bronze badges
3
-
6
You have to write readable code, not minified code. Minifying the code is the job of a minifier. Your code is written to be pact at the cost of being unreadable by another human being. This goes against best practices guidelines. You're aiming for the wrong target here. Also, this doesn't have anything to do with plexity theory. The tag you might want to use instead is
cyclomatic plexity
. – nicholaswmin Commented Mar 29, 2021 at 17:07 -
1
You call the same thing over and over again.....
y.diff(a, 'days')
why? I also hope you do not have to maintain this in the future with your choice of variable names. – epascarello Commented Mar 29, 2021 at 17:38 - 2 If the code works, and you want feedback on all aspects of the function, check their help center to see if the question is on topic for Code Review. – Heretic Monkey Commented Mar 29, 2021 at 18:03
1 Answer
Reset to default 6Two issues:
- Variable names are meaningless
- Several calls to
diff
are repeated
I don't really have an idea what the code is supposed to do, but here is a sketch of what it could look like:
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 };
}
};