this post was submitted on 06 Nov 2023
29 points (68.4% liked)

Programmer Humor

32000 readers
1744 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 41 points 10 months ago (1 children)

You want to create the date "31st February", but it's JavaScript that's cursed?

Write a less side-effecty function.

function getMonthName(monthNumber) {
    const date = new Date(2023, monthNumber - 1, 1);
    return date.toLocaleString([], { month: 'long' });
}
[–] [email protected] 0 points 10 months ago* (last edited 10 months ago) (1 children)

The point is that this scenario exists in Js in the first place. It's a completely unnecessary rake left around for people to step on. Also, the function isn't side effecty since it doesn't make implicit references outside its scope. The fact that the date is mutable is an internal concern there. You could just as easily do

function getMonthName(monthNumber) {
  const date = new Date();
  date.setDate(1);  
  date.setMonth(monthNumber - 1);

  return date.toLocaleString([], { month: 'long' });
}

The problem here isn't with side effects, but with having to know that you want to set your date to first day to get the next month reliably.

[–] [email protected] 14 points 10 months ago* (last edited 10 months ago) (1 children)

The rake has nothing to do with JS (which I agree is cursed, but for its own reasons, not this).

You have called a function in a way that does not give a consistent value (Date()). Such functions are hardly the preserve of JavaScript. You've failed to adequately deal with the range of values produced, with code that tries to insist that the "31st February" can be a meaningful date in February. You should accept that this is your mistake and learn to (better) avoid side effects where possible.

Also, the function isn't side effecty since it doesn't make implicit references outside its scope.

Edit responding to your edit:

Also, the function isn't side effecty since it doesn't make implicit references outside its scope.

The Date() function's output varies according to something other than its input (and even the rest of your program). Using its output without accounting for that variation means that your function, as originally written, also gives inconsistent return values, varying according to something other than its input, because it does, in fact, reference something outside the function. If it did not, the results would only depend on the monthNumber argument, and would always be consistent. I don't know what you call that, but I view it as a side effect.

As you have said, the rake is that months have different lengths, and you need to account for that. But that's not one of JavaScript's many issues.

[–] [email protected] -5 points 10 months ago (1 children)

The idea is to get the current data that will have the current year, month, day in it, and then to query this date for the previous month. A sane API would just throw an error when the date is out of range. A Js API will quitely give you nonsense instead. Again, side effects have absolutely nothing to do with anything here.

[–] [email protected] 6 points 10 months ago (1 children)

You've replied while I was editing, so see that regarding what I mean by side effects.

As far as throwing an error when you try to create "31st February", this wouldn't actually help much, since the error would still only occur on some days of the year, because your original code doesn't account for the range of outputs from Date() when called without arguments.

To perform correctly, your code needs to normalise the day of the month, or just create the date more explicitly to begin with, but this is a calendrical issue, not a JavaScript one.