3

ESLint reports an intolerably high complexity. I want to know why is it too complex? 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 };
  }
};
nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
MansouriAla
  • 177
  • 1
  • 1
  • 11
  • 6
    You have to write *readable* code, not *minified* code. Minifying the code is the job of a [minifier](https://en.wikipedia.org/wiki/Minification_(programming)). Your code is written to be compact 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 complexity theory. The tag you might want to use instead is `cyclomatic complexity`. – nicholaswmin Mar 29 '21 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 Mar 29 '21 at 17:38
  • 2
    If the code works, and you want feedback on all aspects of the function, check their [help center](https://codereview.stackexchange.com/help) to see if the question is on topic for [codereview.se]. – Heretic Monkey Mar 29 '21 at 18:03

1 Answers1

5

Two 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 };
  }
};
trincot
  • 317,000
  • 35
  • 244
  • 286